C++ with UMG HUD

Hi, so I’m new to Unreal Engine and come from a C++ background and I’m creating my first project at the moment (a simple game).

So far I’ve only been using C++, and I think I will keep it that way for the most part, but there are some elements I want to try with visual design/scripting, in this case the HUD, with UMG. My problem is understanding how to connect the UI design with the C++ data.

Say for example, I have a player score stored in my GameMode class (actually, stored in another class which the GameMode has an instance of) and I want to feed that to the UI. How do I do this?

So I think I would want to create a Widget Blueprint and add a text box to it, and somehow link it to the variable in C++, but I don’t know how I can do this.
I might like to also add logic to change the colour of the text based on the score, and also to show or hide it. Where would this kind of logic go?

Any advice would be appreciated, thanks!

Here’s the tutorial on how to extends widget class in C++: https://wiki.unrealengine.com/Extend_UserWidget_for_UMG_Widgets
After you have created your own widget class, you will need to construct it and add it to viewport. It can be done in Blueprint or in C++. C++ tutorial: https://forums.unrealengine.com/showthread.php?52773-Tutorial-Snippet-Creating-a-UMG-Widget-in-C-and-delegate-example

Then, in your class you create functions that allow your widget to access certain variables. In example:
In widget class header:



UFUNCTION(BlueprintCallable, Category = "MyWidgetVariables")
int32 GetScore();


In widget class source:



int32 UMyWidget::GetScore()
{
      if(AMyGameMode* GameMode = Cast<AMyGameMode>(GetWorld()->GetAuthGameMode()))
      {
            return GameMode->GetScore();
      }

      return 0.0f;
}


Then in your widget blueprint you need to create a new function that will be assigned to “Text” property of a text box and in that function return the result of your function implemented in C++.

I hope it’s understandable. If not, let me know and I’ll try to make it more clear. :smiley:

Thanks for your help Ogniok. I think I mostly understand, but I don’t have it working. Extending the widget class in C++ was fine, but I’m unable to add an instance to the viewport following that second tutorial thread.

Similar to someone else in that thread, my class is NULL. The next post in that thread suggests changing something in the player controller blueprint, but as my player controller is pure C++, I couldn’t do that. So instead I did it the C++ way with MyClass = MyCustomClass::StaticClass() in the PlayerController constructor. This caused the game to crash when calling CreateWidget later on, so following something else I read, I tried LoadClass instead to set MyClass, but then I get “Failed to find object”, so I may be misusing the function or using the wrong syntax for the reference.

Incidentally, I think I have the next part setup ok to change the “Text” property of the text box, but obviously can’t know for sure until I can actually add the widget to the viewport.

You need to put a reference to your widget class in InventoryUIClass. You can’t use UYourWidget::StaticClass(), but you must reference the blueprint that derives from that class(created in the editor). This can be done in two ways. The easier one is to extend your player controller class in Blueprint and then just simply set proper widget class for your InventoryUIClass property. If you really don’t want to do this that way, you can reference your blueprint directly from C++.

Thanks, I was trying to do exactly that (the C++ way), but wasn’t referencing the blueprint properly!

I was used to the “AssetType’/Path/To/Asset/Asset.Asset’” format but in this case I needed “/Path/To/Asset/Asset.Asset_C” (I used ConstructorHelpers::FClassFinder)

I have it working now, thanks again!