How can I get a reference to my Widget?


in C++ how can I create a new HUD?

So for example, in blue print., I create a new HUD called, myHUD and this will show player info.

In the player controller, I want to create and cache that hud, and then show it to screen.

Now i’m not using Blue Prints in this project, I want to learn how I can find a Widget via code.

I can find mesh by

	static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));

just need the widget.

So In the game mode, post login method, server gets player, tells player to posses a vehicle.

Now at this point, will it be a client function? So will the server do a “run on owning client” function to show the hud?

Please let me know how I can get Widget.


Ah so you want to find a widget blueprint. First of all i need to tell you that blueprints are classes same as C++ classes, they inherent the same way, i explain that in this video

Now widget blueprint is standard blueprint, blueprint it self is asset which contain blueprint code which is used to generate the class added to hierarchy together with C++ classes.

So first get widget blueprint class is UWidgetBlueprint and oyu get it same way as that mesh and to get UClass of widget from blueprint asset you use GetBlueprintClass()

But UClass is just class identifier, you need to spawn widget:

UClass* ClassFromBlueprint = WidgetAsset->GetBlueprintClass();
UUserWidget* Widget = NewObject<UUserWidget>(this,ClassFromBlueprint);

And then you just add it to viewport with this function liek yiou do in blueprints:

There simpler way to do this, make UPROPERTY settable in blueprint with


type, in class like AGameMode, then you create GameMode blueprint based from that class and you set that varbale, then you spawn widget based of that varable (note TSubclassOf is UClass* which limits class selection in editor). This is more nice looking solution and safe from any potential refrence issues in packageing

Also importent node, UMG is blueprint frontend for Slate (UI framework original made for editor, but it is used also in UE4 gameplay, UMG let it use it in blueprints), Slate in raw form is a lot nicer to use then UMG in C++, so if you want to define UI in C++, Slate is better:

Sorry, but you have confused me lol.

	//FStringClassReference MyWidgetClassRef(TEXT("/Game/Blueprints/HUD.HUD_C"));

	static ConstructorHelpers::FObjectFinder<UUserWidget> MyWidgetClassRef(TEXT("/Game/Blueprints/HUD.HUD"));

	if (MyWidgetClassRef.Succeeded)

		UClass* ClassFromBlueprint = MyWidgetClassRef->GetBlueprintClass();
		 UUserWidget* Widget = NewObject<UUserWidget>(this, ClassFromBlueprint);


not working/