Losing my mind: UMG, HUDs, etc, from c++ code

Ok, I spent the whole day trying to figure this out, and I have finally given up. If I wasn’t required to do this project on Unreal, I would have gotten back to Unity already. I don’t mean to sound rude, but man…

Anyway, my question is really simple: How do I display a small text on the screen? Shouldn’t be too hard, but I read tons of pages of tutorials, and so far, nothing. I would think the HUD I created with the UMG editor could do the job, but how do I get it to show on the screen (from c++ code, of course)?

How I think it should be (seems the most direct and obvious way):

  1. Create full HUD on UMG Editor (Done, this is easy);
  2. Reference this HUD on a C++ class (???);
  3. Show HUD on the screen (???);
  4. Access any element in it, change the texts, values, bars, etc! This would be heaven!

If someone could point me to the right direction on figuring this items out, I would really appreciate it!

If there is no way, and I simply HAVE to use blueprints, how do I do to make the hud blueprint access a variable on a c++ class? For example, I have a “coins” property stored on a pawn class. If I want the Text block I created to access this property, how do I do? Because I followed the “Creating Health/Energy and Ammo indicators” tutorial, but there isn’t anything I can bind it to! And still, how would I do to show the hud? Because so far, I haven’t even been able to do this… :’(

Additional info: I’m using custom pawn/actor classes.

Thank you all in advance, and whoever is able to help me with this deserve a beer!
And sorry if my text is confusing, I haven’t been sleeping much…

Personally i would store all the values to be displayed in my HUD in the GameState class.

Create a C++ GameState derived class in your project, put all of your HUD-accessible values there such as health, energy, ammo etc.

Then, modify those as you wish in C++, make sure they have the correct UPROPERTY() definition so they’re blueprint accessible (to be honest, BlueprintReadonly should be more than enough) then use the tick and update etc methods of the HUD blueprint to draw your HUD based upon the properties from the gamestate.

If you later find you can’t do something you want, simply add more GameState so you can.

GameState is great for this as it is accessible everywhere in the game, blueprint, C++ or otherwise. You can even hook delegates to it and use it for messaging.

Hope this helps!

More suitable for health/energy/ammo would be class deriving from APlayerState.

Yes this is true. APlayerState is managed on a per-player basis, whilst GameState is global to everything, overarching the player state. Both can and should be used for this depending upon what you want to store.

Thank you so much! After a good night’s sleep and reading your suggestion, it makes so much sense that I feel so dumb right now… I even started trying to implement a Slate UI, but that’s not simple at all! I’ll try what you suggested and see what happens!!

Just to give anyone who’s having the same problem a simple step-by-step to what I did:

  1. Create HUD on the UMG Editor;

  2. Create a custom GameState class;

  3. Set that GameState class as the one to be used, in the GameMode.cpp (remember to include your custom gamestate.h!):

    GameStateClass = APacGameState::StaticClass();

  4. Do stuff on your code, reference and set values to the game state. This is how I referenced it (not sure if you need all that, but it ensures nothing is null):

    APacGameState* gameState = GetWorld() != NULL ? GetWorld()->GetGameState() : NULL;

  5. Create a new blueprint (yeah, there’s no escaping those) to exclusively call your HUD, and place it on your level (and set it hidden in game). This is how I did it:

  1. Create bindings to the values you want to change on your hud. An example:

That’s it! It worked beautifully for me! And I have to admit, after taking a small time to actually learn a little about bluprints… Well, they’re kind of sexy, can’t say I still hate them!

Thank you so much braindigitalis!