Is there a better way to do this? (Drawing dynamic text to screen with Widget)

I’m new to UE and I have struggled to draw some simple text on the screen. Honestly I’m amazed how much of a struggle it has been to manage such a seeming simple task.

My goal was to simply print the values of some variables as a temporary HUD. This is what I have accomplished so far:

Following a tutorial I have created a widget blueprint and inside it added some text fields on a canvas. I created a HUD blueprint and on BeginPlay instantiate the widget object and then Add to Viewport.

So far so good. This draws the text to the screen at playtime. But I need to set the text dynamically.

In the widget I created variables corresponding to each text field, set them to editable and Expose on Spawn. I bind the text fields to these variables. Now I just need to access and set those variables during play.

At this point I proceeded to spend hours trying every combination of nodes, googling, reading old threads, and watching long tutorials about casting trying to successfully cast to my HUD object and access the variables in the widget created by my HUD object from my player pawn object. Finally, I arrived at this solution:

After creating the widget inside of my HUD object I stored the reference to it in a variable and then I was able to access that as you see here. I’ve seen that idea pop up on a couple forum posts. But the point of failure was getting access to the HUD itself. I could not figure out any working way to do that using casting.

As of now this is working and it looks clean and simple, but using the Get Actor node seems like a questionable practice. It is also being called every frame which the program specifically warns against.

What else can I do? Should I be using casting but I just didn’t figure out how to do it correctly? Is casting not a thing that makes sense here? Should I be using another approach entirely? I would appreciate any input on this.

Hi!

Player controller have function GetHUD, you can try and use that instead of GetActorOfClass:
image

And after that you can cast HUD to your class MyHUD and access HUDWidgetReference

Hello, thank you for this, it works well! Someone else suggested to me to have the HUD access and pull the value from the pawn instead of the other way around, using the Get Owning Pawn node.

Apparently the HUD is owned by the Player Controller. That’s good to know.

I realized I don’t understand the underlying hierarchy of things in UE. I’m also impressed by how opaque that is. But that’s a subject for another thread.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.