Download

Adding HUD elements through script

Does anyone know how to add, remove, and manipulate elements in a HUD through blueprint?

Use Create Widget node to dynamically spawn an UI element; add it to the viewport or add it to an existing container.

Removing is usually handled by the Remove From Parent node or by adding an element to another container.

To manipulate them, create direct references, expose UI elements as variables, use dispatchers and events - this is a very broad topic. Widgets can be animated, too.

More info:

What I am trying to do is say, add and remove individual elements, such as text or images within the same HUD. The elements would not be preexisting in the HUD. Would this work for that.

Yes, but you’d need to wrap them in user widgets first.

Essentially, create a widget with a TextBlock only, for example - expose whatever functionality you’d require from it and then create those widgets on the fly - as a bonus this allows you to maintain code better.

So the text block already needs to be there, there’s no add text block to widget “****” node or anything like that ?

No, it does not need to be there. You create a user widget and then dynamically add those widgets during run-time.

In bluerprints this is handled by user widgets. One cannot add *native *elements directly.

Ok so let me make sure I’m getting this right. So I create the widget blueprint, then adding no elements in that specific blueprint, I move into my players blueprint. Then I use the create widget node,select my widget and then store the widget in a variable. So there is a way to use that variable to add and remove things like text blocks and images?

  • create a widget that will represent a list of items the player owns - the list is empty
  • create a widget that will represent an item in the list - a User Widget with just an image
  • during gameplay, you can populate the list with the above-mentioned User Widgets

What you contain within that user widget is up to you - it can be another super complex menu or a piece of text, or a single image.

I like to make animations in my hud widget for the elements (buttons, text, etc…)

You can keep a button off screen and invisible, Move it on screen and change visibility, size whatever. Pretty easy to learn the track editor, they are simple anims.

Then you just play an anim from wherever. Your map bp with a trigger box, player bp (if they take dmg)… Whatever the easiest trigger is can send to the widget and play the anim.

for example:
This is in my menu widget (but could be player hud)
This is checked on tick (I need to change that).
If my var in my game instance reaches a number a map is unlocked. But really all it does is play an animation that moves a button onto the screen that the player can use when they get back to the menu. That button could be a progress bar or text…