//On my HUD class I have a pointer to a widget: UMyWidgetClass * MyWidgetPointer:
void AMyHUD::AddUMGWidgetToScreen()
{
if (UMyWidgetClass == nullptr)
{
UClass * MyWidgetLoadedClass; // Creates a class
FString WidgetBPAddress = "/Game/Somedir/Somedir/WIDGETBP.WIDGETBP";
UBlueprint * WidgetBP;
WidgetBP = Cast<UBlueprint>(StaticLoadObject(UBlueprint::StaticClass(), NULL, *WidgetBPAddress, NULL, LOAD_None, NULL));
check(WidgetBP)
{
MyWidgetLoadedClass = WidgetBP->GeneratedClass;
MyWidgetPointer = CreateWidget<UMyWidgetClass>(GetWorld()->GetFirstPlayerController(), MyWidgetClass);
MyWidgetPointer->AddToViewport();
}
}
}
Additional Info:
This method just works on PIE and with an offset from mouse coordinates (this widget should store a custom mouse cursor).
On Standalone launchs the widget doesn’t show at all, I’ve checked the Player0 Viewport Size and it’s zeroed XY.
I’m calling the method from AGameMode::StartPlay(), what I thought is the same as make the creation and call from the Game Blueprint.
Don’t load a widget class/asset in that manner. The same goes for anything blueprint based, the blueprints are an editor time only concept, that’s why the WidgetBlueprint class is in an editor module - the UBlueprint will likely be soon as well. You should expose a TSubclassOf<UUserWidget> UProperty on your HUD, set the value in the editor, and it will be the class you want in native code. No need to muck around with hardcoded paths and loading blueprint assets.
Great… Just now I’ve seen the “+” button at side from the Default GameMode Classes to create “NonActor” ClassBlueprints. Other than UMG elements and the Level BP I thought that all Blueprints would just be used as the U3 Archetypes, meaning, things that need be placed on levels.
Summed with what told me on Answerhub my tools pallete grew considerably now (too much options I’ll probably get lost LOL).