How can I reference a Widget Class in C++

I’m following one of Epic’s C++ tutorials on using UMG: https://docs.unrealengine.com/en-us/Programming/Tutorials/UMG

On the third page of the tutorial, we create a Widget Blueprint in the editor with a parent class of UMG.UserWidget. We then make a button and hook up blueprint scripting for the On Clicked event like so:

https://forums.unrealengine.com/filedata/fetch?filedataid=165252&type=thumb

The function Change Menu Widget is created in C++ as part of the tutorial and is defined as follows:

// Pasted from the header
/** The widget instance that we are using as our menu. / UPROPERTY() UUserWidget CurrentWidget; // End paste from header
void AHowTo_UMGGameMode::ChangeMenuWidget(TSubclassOf<UUserWidget> NewWidgetClass) { if (CurrentWidget != nullptr) { CurrentWidget->RemoveFromViewport(); CurrentWidget = nullptr; } if (NewWidgetClass != nullptr) { CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), NewWidgetClass); if (CurrentWidget != nullptr) { CurrentWidget->AddToViewport(); } } }

What I would like to do is hook up the On Clicked blueprint scripting event to a single C++ function that does what all of the blueprint scripting accomplishes. If I’m not mistaken, the issue I’m having is finding out how to reference a Blueprint Widget through C++. Is this possible? And if not, is it possible to handle all functionality from a blueprint event by hooking it up to a single C++ fucntion?

Thanks!

Did you find a solution.

I have the same problem.
If you find a solution please share how you did it.

Thank you very much.

In Epic’s tutorial the UMG is using Game Mode’s reference to use its function.

What you could do is create a C++ class that inherit from UserWidget:

In this class you create a public UFUNCTION.

Then you create an UMG, open this UMG and change it to use your C++ class so you can use your UFUNCTIONs:

Hello, the above answers aren’t addressing the question of “How do I reference a Widget Class in C++”? To clarify this answer references a widget class and not an instanced widget.

To do this, you need to include the “UMG” module in your <projectname>.Build.cs.

Then, in the C++ class create a blueprint readable property for your widget.

UPROPERTY(EditAnywhere,BlueprintReadWrite)
TSubclassOf<UUserWidget> LobbyWidgetClass;

UUserWidget is the parent class for all widget blueprints that are user made in Unreal.

TSubClassOf<ClassName> is a reference to any object that is a subclass of “ClassName”. We need this because whatever widget you have will be a subclass of “UUserWidget”

After this, you will need to assign this property somehow. In my use case, I made a child blueprint class of my C++ class and therefore the property shows up in the details panel of that class like this:
image

Now that it’s a property I can use it in C++ or blueprint functions. Hope that helped someone!
image

1 Like