I did this by using a custom AHUD class and a widget blueprint. The custom AHUD class is specified in my custom GameMode as
HUDClass = AMasterRobotHUD::StaticClass();
The widget blueprint is created using the blueprint editor, so I can use the Unreal interface editor to create buttons etc.
To link it together, in my AMasterRobotHUD class, .h file:
class UClass * hudWidgetClass;
class UUserWidget * hudWidget;
virtual void BeginPlay () override;
Then in the constructor:
static ConstructorHelpers::FClassFinder<UUserWidget> hudWidgetObj (TEXT ("/Game/umg/GamePlayHUD_blueprint"));
if (hudWidgetObj.Succeeded ()) {
hudWidgetClass = hudWidgetObj.Class;
} else {
// hudWidgetObj not found
hudWidgetClass = nullptr;
}
You copy the bath to your blue print in the Unreal editor, by right-clicking and “Copy Reference”. Don’t forget to delete the beginning and the end of the reference string to get something similar to what I have above.
Also, add a function into your custom HUD class for when the game starts:
void AMasterRobotHUD::BeginPlay () {
Super::BeginPlay ();
if (hudWidgetClass) {
// the player controller should be constructed by now so we can get a reference to it
hudWidget = CreateWidget<UUserWidget> (this->GetOwningPlayerController (), this->hudWidgetClass);
hudWidget->AddToViewport ();
}
}
I copied the solution from elsewhere, I can’t remember where though anymore! Anyway, also remember that the HUD will ONLY show when you Launch, i.e., it doesn’t show when you Play via the Unreal editor.