How do I call a blueprint widget from C++

I have a HUD coded in C++ and a widget created in BP, how do I use the BP widget in my C++ HUD?

Easiest way (by that i mean without hardcoded refrences to uassets) is to make

TSubclassOf<UUserWidget> 

exposed blueprint property (Widget Blueprint is subclass of UUserWidget)UUserWidget | Unreal Engine Documentation) in your HUD, it will ofcorse be UClass* type in which you will need to spawn using UWidgetBlueprintLibrary::Create (https://docs.unrealengine.com/latest/INT/API/Runtime/UMG/Blueprint/UWidgetBlueprintLibrary/Create/index.htm) and then you can operate it as this function return pointer to it. Create HUD blueprint from you C++ HUD and then you can set any blueprint widget in property you made.

You will also need to extend UUserWidget class to make C++ accessable varables and events so your C++ code can cooperate with Blueprint code

l

Great post, i’ve been trying to figure out how to do this. I did this on my character:

APlayerController * PlayerController = this->Controller->CastToPlayerController();

		UUserWidget * Widget = UWidgetBlueprintLibrary::Create((), WidgetType, PlayerController);
		UWidgetBlueprintLibrary::SetInputMode_UIOnly(PlayerController, Widget, true);
		Widget->AddToViewport();

Where WidgetType is:

UPROPERTY(EditDefaultsOnly, Category = "Character")
	TSubclassOf< class UUserWidget > WidgetType;

But he giving errors:

'Z_Construct_UClass_UUserWidget': identifier not found
'UWidgetBlueprintLibrary' : is not a class or namespace name
'Create': identifier not found

and so on. Do i have to include a header to use this?

I added the UMG, Slate and Slate core module and put the includes and he now recognizes UUserWidget, but he’s giving me linking errors on the UWidgetBlueprintLibrary methods i use (Create and SetInputMode_UIOnly). Any ideas why? Do i need to define something or add another module for him to work?

Hi,
It seems that UWidgetLayoutLibrary as well as UWidgetBlueprintLibrary are not part of the public API of the UMG Module.
You can access their headers and use the type, but it will fail on Link time because the implementation is not available.
I did a workaround and copied and modified the implementation of the one needed function of UWidgetLayoutLibrary in my own class. It’s dirty, but at least it works. I’d be happy to know a better way anyway. In my case I needed the current Viewport Scale in C++ (as I move some functionality from Widget Blueprint into code)

ciao

Add this header:

#include "Runtime/UMG/Public/Blueprint/WidgetLayoutLibrary.h"

Now you can instantiate it like:

UWidgetLayoutLibrary* WidgetLayoutLibrary;
WidgetLayoutLibrary = NewObject<UWidgetLayoutLibrary>(UWidgetLayoutLibrary::StaticClass());