Download

Please: How to call a Widget function inside FirstPersonCharacter.cpp (C++)

Hello guys.

I have this Widget that I put on the viewport through a GameMode C++ class

This Widget has this function thanks to which I can change the TextBlock inside the Widget itself

Now, I use this function to update a TextBlock which indicates the Pickup taken (is it a weapon, a shells pack or anything like that).
So for example, if I pickup a Pistol the TextBlock text will be “Pistol”.

The problem is that I want to call the ChangeText function inside my FirstPersonCharacter.cpp, BECAUSE IT’S THERE that I have the AddWeapon(), AddHealth(), AddArmor() function.

Create a C++ base class inheriting UUserWidget, then reparent your UMG blueprint to that base class.

Add a ChangeText UFUNCTION( BlueprintImplementableEvent ) in this C++ base class. You might have to reimplement the function in your blueprint.

Then, you should be able to instantiate your widget in C++ and call the ChangeText function on it wherever you need to.

-Camille

Camille’s solution is spot on.
If you want complete control from the FirstPersonCharacter.cpp, you can instantiate the widget C++ base class from the FirstPersonCharacter with a this-pointer as outer. That way you always have a reference to your widget and can call whichever functions you open up.