# How can I get a reference to my Widget?

**URL:** <https://forums.unrealengine.com/t/how-can-i-get-a-reference-to-my-widget/354297>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [April 15, 2016, 1:14pm UTC](https://forums.unrealengine.com/t/how-can-i-get-a-reference-to-my-widget/354297 "2016-04-15T13:14:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moynzy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Moynzy](https://forums.unrealengine.com/u/Moynzy)\
**Post date:** [April 15, 2016, 1:14pm UTC](https://forums.unrealengine.com/t/how-can-i-get-a-reference-to-my-widget/354297/1 "2016-04-15T13:14:28Z")

</div>

Hi,

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.

Cheers.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [April 15, 2016, 4:43pm UTC](https://forums.unrealengine.com/t/how-can-i-get-a-reference-to-my-widget/354297/2 "2016-04-15T16:43:04Z")

</div>

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()

> **[UBlueprint::GetBlueprintClass](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UBlueprint/GetBlueprintClass/)**
>
> Gets the class generated when this blueprint is compiled.

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:

> **[UUserWidget::AddToViewport](https://docs.unrealengine.com/5.1/en-US/API/Runtime/UMG/Blueprint/UUserWidget/AddToViewport/)**
>
> Adds it to the game's viewport and fills the entire screen, unless SetDesiredSizeInViewport is called to explicitly set the size.

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

```
TSubclassOf<UUserWidget*> 

```

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:

> **[Slate UI Programming](https://docs.unrealengine.com/5.1/en-US/slate-user-interface-programming-framework-for-unreal-engine/)**
>
> Program user interfaces with Unreal Engine's Slate framework.

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

---

<div class="post-metadata">

**Author:** ![Moynzy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Moynzy](https://forums.unrealengine.com/u/Moynzy)\
**Post date:** [April 17, 2016, 9:33pm UTC](https://forums.unrealengine.com/t/how-can-i-get-a-reference-to-my-widget/354297/3 "2016-04-17T21:33:23Z")

</div>

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/
