Download

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!