The reason why people are asking over and over again how to display score or energy or any other text on screen, is because the logic is over-complicated. WHY can I not just reference a text field from a widget blueprint? I get a value from one object, and I set the text property of the text field of the widget. How difficult can that be?
Doing such a simple and very common thing gives me headaches every time. And it’s just as complicated in Unity, so developers seem to overlook this.
By the way, big fan of Unreal, just suggesting that you can really stick out when you simplify displaying text on screen.
You choose the right tool for the job. It all depends on how / how often updates need to happen and who is responsible for the updates. It’s up to you how to organise it. There is no one best way; albeit Event Driven approach is desired in most cases.
@Everynone, thanks for your reply, but you’re oversimplifying what needs to be done. I compare it to let’s say Visual Basic in the stone age. I just create a text field or a label in my viewport, let’s name it txtScore. The only thing I need to do to get the text changed is txtScore.text = 1234.
The way I see this in blueprint is create a reference to the text field: so create a variable of type HUD, put that HUD on the blueprint, drag a line from the blue pin, and choose “Set txtScore” or “Get txtScore”, nothing more or less. This method is valid for any scenario.
Why I need to create the widget once more in the level blueprint is something that is not intuitive in my opinion. I already created it on screen, didn’t I?
I compare it to let’s say Visual Basic
in the stone age.
UE4 has moved past that; you’ll either conform and adapt or keep chipping at rocks.
Why I need to create the widget once
more in the level blueprint
You do not. This is created in the character, as seen in the pic. One actor, one widget. You can even use a widget component that will create a widget for you automagically. So you just add a component to any entity in the world. And you can even extend an existing component class…
The only thing I need to do to get the
text changed is txtScore.text = 1234.
That’s precisely what my second example does - I am pointing a text field at a variable in the player.
textblock.text = player.health
It pulls data from the the player - this polling is done every frame. It’s 3 clicks away and probably the easiest way to use it. Also, widgets can pull data from structs. So if the player’s values change, the UI updates in real time.
The way I see this in blueprint is
create a reference to the text field:
so create a variable of type HUD, put
that HUD on the blueprint, drag a line
from the blue pin, and choose “Set
txtScore” or “Get txtScore”, nothing
more or less. This method is valid for
any scenario.
And that’s precisely what my first example does. I’m setting a value in a text field of a widget.
But this approach is very limited in scope and scales poorly. I cannot recommend it unless we’re talking about just a handful of variables. One should at least consider creating a Custom Event in the widget and feed it data - the widget then uses this data to populate its fields without exposing anything.
UE4 is very [OOP][2]. It’s pretty much a must to work this way these days.