x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Passing data into HUD - C++

I'm currently trying to display the players health using UE 4.9 into my HUD class. (referencing through this tutorial https://www.youtube.com/watch?v=yCqsbXS9yRg)

I'm having trouble passing in data into my HUD class. My _health variable is private, so i created a get method for another class to access its value. Even though the code compiles it seems like no data is being passed in, and im not sure why. I tried to print out the health value into my console through this function in my character source code but it seems like this method is never called at all.

Any help would be greatly appreciated.

TestFPSCharacter.cpp

 /**
   *    _health is private and set to 50.f; in the constructor, so return its value through a public get method
   */
 float TestFPSCharacter::GetHealth()
 {
     FString _tmp     = FString::SanitizeFloat(_health);
     GEngine -> AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("health is : ") + _tmp);    
     return _health;
 }

My HUD header file:

 #pragma once
 #include "GameFramework/HUD.h"
 #include "CharacterHUD.generated.h"

 /**
  * 
  */
 UCLASS()
 class EPSILONFPS_API ACharacterHUD : public AHUD
 {
      GENERATED_BODY()
  public:
      ACharacterHUD();
  
     UPROPERTY()
     UFont*             _HUDFont;
 
     virtual void     DrawHUD() override;
 
 private:
     UTexture2D*     _crosshairTex;
     void             PlayerHealth();
 };

My HUD implementation file:

     #include "EpsilonFPS.h"
     #include "CharacterHUD.h"
     #include "Engine/Canvas.h"
     #include "Engine/Font.h"
     #include "TestFPSCharacter.h"
     #include "Engine.h"
     #include "kismet/GameplayStatics.h"

     /**
      *    constructor
      */
     ACharacterHUD::ACharacterHUD()
     {
         static ConstructorHelpers::FObjectFinder<UTexture2D> CrosshairTexObj(TEXT("Texture2D'/Game/crosshair.crosshair'"));
         static ConstructorHelpers::FObjectFinder<UFont> HUDFontOb(TEXT("/Engine/EngineFonts/RobotoDistanceField"));

         _crosshairTex = CrosshairTexObj.Object;
         _HUDFont = HUDFontOb.Object;
     }


     /**
      *    Draw the HUD by overriding DrawHUD
      */
     void ACharacterHUD::DrawHUD()
     {
         Super::DrawHUD();
         ACharacterHUD::PlayerHealth();

         const FVector2D Center(    Canvas -> ClipX * 0.5f, Canvas -> ClipY * 0.5f);
         const FVector2D CrosshairDrawPosition((Center.X - (_crosshairTex->GetSurfaceWidth() * 0.5)), (Center.Y - (_crosshairTex->GetSurfaceHeight() * 0.5f)));

         FCanvasTileItem TileItem(CrosshairDrawPosition, _crosshairTex -> Resource, FLinearColor::White);
         TileItem.BlendMode = SE_BLEND_Translucent;
         Canvas -> DrawItem(TileItem);
     }


     /**
      *    Get players health to display in the HUD
      */
     void ACharacterHUD::PlayerHealth()
     {

         // Get the players health
         ATestFPSCharacter* _myCharacter = Cast<TestFPSCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
         FString _healthString =    FString::Printf(TEXT("%10.0f"), FMath::Abs(_myCharacter -> GetHealth()));
         DrawText(_healthString, FColor::White, 50, 50, _HUDFont);

     }

Thanks!

Ben

Product Version: UE 4.9
Tags:
more ▼

asked Sep 23 '15 at 11:25 AM in C++ Programming

avatar image

hairspray011
8 6 5 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

In my game mode class I had this line to call the HUD class:

 // use our custom HUD class
 HUDClass = ATestFPSHUD::StaticClass();

I had made 2 classes for separate HUDs instead of one. One was for displaying a crosshair, the other was to display health. Changing the above line to:

 // use our custom HUD class
 HUDClass = ACharacterHUD::StaticClass();

gets rid of my issue and my HUD finally displays text. Can't believe I didn't notice this earlier ><

more ▼

answered Sep 28 '15 at 02:34 AM

avatar image

hairspray011
8 6 5 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

So first in your DrawHUD() function you call PlayerHealth() as a static one, it should be something like this:

 /**
  *    Draw the HUD by overriding DrawHUD
  */
 void ACharacterHUD::DrawHUD()
 {
     Super::DrawHUD();
     PlayerHealth();
 
     [...]
 }

Your PlayerHealth() is also a bit dangerous, you are not checking if the Pawn is valid and so you might get some crashes because you will be accessing eventually a null pointer. You are also just getting the first player pawn, this will not ensure you that's the player of the HUDin some cases such as split-screen, the good news are that the HUD as a method to get it's linked pawn which is GetOwningPawn(). The method would be like this:

 /**
  *    Get players health to display in the HUD
  */
 void ACharacterHUD::PlayerHealth()
 {
     // Get the players health
     AEpsilonFPSCharacter* _myCharacter = Cast<AEpsilonFPSCharacter>(GetOwningPawn());
     if (_myCharacter)
     {
         FString _healthString =    FString::Printf(TEXT("%10.0f"), FMath::Abs(_myCharacter->GetHealth()));
         DrawText(_healthString, FColor::White, 50, 50, _HUDFont);
     }
 }

You could also just pass NULL where you are passing _HUDFont so the engine will use it's default one.

more ▼

answered Sep 24 '15 at 06:52 AM

avatar image

Moss
6.4k 127 98 606

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question