Download

HUD class

I have created a new class (called myHUD) that derives from HUD, how do I get this class to get called though? I have made a blueprint that derives from myHUD

I have tried this in Character.h:



	static ConstructorHelpers::FObjectFinder<UBlueprint> HUDBlueprint(TEXT("Blueprint'/Game/Blueprints/UI/Bp_HUD.Bp_HUD'"));
	if (HUDBlueprint.Succeeded())
	{
		myHUD = (UClass*)HUDBlueprint.Object->GeneratedClass;
	}

I thought this would call my class functions, however this function in myHUD never get calls. The breakpoint never hits.


void AmyHUD::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);

	AGrenade* Grenade = Cast<AGrenade>(MyPawn->CurrentInventorySlot);
	AWeapon* Weapon = Cast<AGrenade>(MyPawn->CurrentInventorySlot);

	int32 localAmmo = MyPawn->Weapon[MyPawn->CurrentInventorySlot->InventoryConfig.Priority]->CurrentAmmo;
	FString ammo = FString::FromInt(localAmmo);
	 
	 DrawText(ammo, WhiteColor, 10.0f, 10.0f, NULL, 1.0f, true); 
}


2 things:

  1. Don’t use Blueprint references in game code. They are an editor concept and will likely be removed from game modules according to Epic devs.

Here’s how I construct my blueprinted HUD instead:


	static ConstructorHelpers::FObjectFinder<UClass> HUDClassFinder( TEXT("Class'/Game/Blueprints/Game/MyGameHUD.MyGameHUD_C'") );
	HUDClass = HUDClassFinder.Object;

  1. You can’t call draw functions from tick. The draw functions are basically used in a special phase of the game loop where the renderer is ready to receive such commands. Instead, override DrawHUD() and call your drawing methods in there:


void AmyHUD::DrawHUD()
{
	Super::DrawHUD();

	AGrenade* Grenade = Cast<AGrenade>(MyPawn->CurrentInventorySlot);
	AWeapon* Weapon = Cast<**AWeapon**>(MyPawn->CurrentInventorySlot);

	int32 localAmmo = MyPawn->Weapon[MyPawn->CurrentInventorySlot->InventoryConfig.Priority]->CurrentAmmo;
	FString ammo = FString::FromInt(localAmmo);
	 
	 DrawText(ammo, WhiteColor, 10.0f, 10.0f, NULL, 1.0f, true); 
}


-Camille

Thanks for answer.

I have done all that and it compiles and runs, however it still doesn’t go into the myHUD class.


virtual void DrawHUD() override;


static ConstructorHelpers::FObjectFinder<UClass> HUDClassFinder(TEXT("Class'/Game/Blueprints/UI/Bp_HUD.Bp_HUD_C'"));
	myHUD = HUDClassFinder.Object;


void AmyHUD::DrawHUD()
{
	Super::DrawHUD();

	GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("DRAWWW"));

	AGrenade* Grenade = Cast<AGrenade>(MyPawn->CurrentInventorySlot);
	AWeapon* Weapon = Cast<AGrenade>(MyPawn->CurrentInventorySlot);

	int32 localAmmo = MyPawn->Weapon[MyPawn->CurrentInventorySlot->InventoryConfig.Priority]->CurrentAmmo;
	FString ammo = FString::FromInt(localAmmo);

	DrawText(ammo, WhiteColor, 10.0f, 10.0f, NULL, 1.0f, true);
}

Any ideas why?

You need to ensure that your game mode is actually referencing your HUD class. Otherwise some other HUD class is probably being used.

Yeah. I overlooked that in the OP, but you shouldn’t be assigning this class in your Character, as the character has no notion of the HUD being used. The sample I posted assigns the hud class to HUDClass, which is a property of AGameMode. Create yourself a GameMode, set the class there, and you should be all set.

Might be worth chipping in, you can access a players Hud through the Player Controller too. Doing so allows you to get the Pawn, so:



	AGrenade* Grenade = Cast<AGrenade>(GetController()->GetPawn()->CurrentInventorySlot);
	AWeapon* Weapon = Cast<AGrenade>(GetController()->GetPawn()->CurrentInventorySlot);


(I think GetController() is the function name…)

It also might be that your cast is failing, you don’t have a handler for that atm. Just add a check for it:



if (Grenade == NULL)
{
          GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("Grenade Cast Failed")));
}


Thanks. This all works great now.