Get Blueprint Class in C++

I’m trying to implement a simple HUD, but I can’t assign it within my custom GameMode class. The HUD is a UMG UserWidget, this is how I try to assign it:

	static ConstructorHelpers::FClassFinder <UBlueprint> YourBlueprintName(TEXT("/Game/UserInterface/MainHUD.MainHUD"));
	HUDClass = (UClass*)YourBlueprintName.Class;

I always get an error that it couldn’t be found. I already tried many different combinations and classes, but it doesn’t work. Anyone got an idea?

Hi maxim6394,

The HUDClass property of GameMode is TSubclassOf HUDClass. So, if you whant to create a custom HUD class you need to create a C++ Class or Blueprint that extend from AHUD class override the DrawHUD() method and there put your custom HUD logic there.

On the other hand, if you whant to use a Widget (UMG object) for your HUD, you dont need to modify your HUD class, you can create your widget and in your PlayerController, for example, on the Begin Play create a instance of your widget and add to the player viewport. See the attached image.

https://docs.unrealengine.com/latest/images/Engine/UMG/UserGuide/CreatingWidgets/CreateWidgetNode.jpg

Best regards

Also you can check the following tutorial for some examples using two options, HUD class and Widgets.

http://blog.spissa.com/en/2015/03/24/introduccion-al-hud-en-unreal-engine-4-parte-1/

The tutorial is in spanish but with the images and code samples i think that will be easy to follow. Best regards

But I have my GameMode and my PlayerController in C++, only the HUD was made with the blueprint system.
The classfinder just doesn’t find the HUD class: Screenshot - d26035dc61b32e4557966b4c8c1d4789 - Gyazo

Ah ! ok ok . . . in that case you can create a property in your Character class, for example, to store a “Subclass” of your Widget:

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "HUD")
TSubclassOf<class UUserWidget> HUDWidget;

Then, in the BeginPlay create a Widget using that property:

//Your CharacterClass.cpp

#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/Blueprint/WidgetBlueprintLibrary.h"

//...

void AMainCharacter::BeginPlay()
{
	Super::BeginPlay();

	APlayerController* PlayerController = Cast<APlayerController>(GetController());

	if (HUDWidget && PlayerController)
	{ 
		UUserWidget* UserWidget = UWidgetBlueprintLibrary::Create(GetWorld(), HUDWidget, PlayerController);
		UserWidget->AddToViewport();
	}
	
}

One important thing, you need to add in your Build.cs files the UMG modules.

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "AIModule", "UMG", "Slate", "SlateCore" });

The last step is compile, open your project, open your Character blueprint and in the Details Panel you will have the HUDWidget property that you create before. select your widget here, compile an run.

Well … to be honest, the best place to put this logic is inside your PlayerController class, using the BeginPlay of your controller. But you can do it by your own :wink:

Best regards.

this approach finally seems to work, I tried a few hours to get some basic HUD on the screen. Still don’t know if there’s one straight solution to have full control from C++ code over the UI.

Well, with any of this options (using HUD Class or UMG with Widgets) you can have full control using C++. the good thing with UMG, you can prepare all your HUD elements basically with drag and drop. If you want to interact with this components from C++, you can create a custom UserWidget class.

Anyway, If my answer solves your problem, I’d appreciate you mark the answer as correct answer.

Best regards

There’s another problem left: the binding for the health bar doesn’t seem to work. This is my blueprint: Screenshot - d5ed65ab8f6271360e165619edd843d8 - Gyazo
I get the only player there is and divide the current hp with the maximum hp, but the bar is always at 0.

I am not sure, but try to get the player reference using:

Get Owning Player->GetControlledPlayer

and Cast to you custom player class. The rest is fine.

Best regards