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 - YouTube)

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!

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.

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 ><