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
Best regards.