Widget Component - Get parent actor?

Can there be a link to the Parent Actor of which the Widget Component is sitting in? It would be easier to place these inside any Actor and show their unique distances between selves and the player.

***UMG Screen Space Widget - Bind Text - Distance ***

1 Like

No, afaik the widget does not know it’s sitting pretty inside a component.


As a rule of thumb perform any calculations outside of the widget and then push ready data into the widget. You can do it in the actor that owns the widget component and access the component’s 2d widget.

Actor → Get Widget Component → Get User Widget → Cast → Call a Custom Event or Set the fields directly.

If you’re going to update it often (every frame?), consider creating a direct reference to the embedded widget on Begin Play.

This approach has multiple significant advantages:

  • you no longer rely on the widget ticking (offscreen widgets do not tick)
  • you do not risk an accidentally orphaned widget trying to access a null parent
  • it’s much easier to access an actor that has calculated data than pull that data out of a field of a widget embedded in a component…
  • it makes it super easy to set up Event Dispatchers in case the widget needs to send data back to the actor owning the component
3 Likes

In the actor with the widget component:


If you must use a binding for the text, you can have a variable there and set its value instead of calling a function. There’s usually no need, unless you must apply some special formatting only that widget is aware of.

5 Likes

Ow, so if i have hundreds of items, objects, enemies, would i have to create these nodes manually for each thing? :confused: I would love to learn C++ just to get the Parent Actor of a widget. I mean, it should just check what its parent is. Wishful thinking? I can’t imagine it being that complex but i’m most likely wrong.

I couldn’t recreate your BP by the way :frowning: at least not in a way that I can set the text…

Ow, so if i have hundreds of items,
objects, enemies, would i have to
create these nodes manually for each
thing?

No, it’s enough to make it once in the base class. Children will inherit it automagically.

Parent Actor of a widget

Parent of the widget is the viewport, not the component. The component simply adds it to the viewport.

I mean, it should just check what its
parent is. Wishful thinking? I can’t
imagine it being that complex but i’m
most likely wrong.

It’s not. Describe what you want to achieve. Perhaps I can chip in or at least point you in the right direction. What I posted already gives your actor full & direct access to the widget. What more do you need?

couldn’t recreate your BP by the way
:frowning: at least not in a way that I can
set the text…*

Why not? On actor Tick, get Text Block from from My Widget → Set Text. Done. You do not even need a Custom Event like in my example. Simply flag the text block as variable at the top of its panel.


As I initially suggested, what you’re doing in your example is not the best approach, it will give you a headache sooner or later. Consider following what I suggested - have the actor do the math and push it to the widget.


On the other hand, If you absolutely must give the widget access to the actor and use function binding, create an actor reference in the widget, expose it on spawn and set the reference this way. From now on the widget can pull data from the actor.

But even with this method I wouldn’t trust the widget to calculate anything. It’s just bad karma. It might not be a bad idea to let the widget format their output text. In which case, I’d have a dedicated variable in the widget and let the bind format that.

You definitely do not need C++ for this.

Describe what you want to achieve.

Definitely less overthinking!- no, here’s a good old-fashioned sketch:

Basically as a floating UMG combat text over enemies, pickups, loot boxes in close proximity. Maybe health bars and their names. But it has to be 1) emissive and 2) universal. This was the other problem with UMG widgets that I heard its font style can’t be in emissive colors?

Otherwise i just remember that i tried to set the text in your example and it didn’t work because i couldn’t plug from the blue set node to a place that made sense. I couldn’t picture what to put instead of these wBeacon events. Thanks!

We somehow jumped from sending data to a widget to designing an entire system. I can’t justify that kind of time investment. But here’s a snippet based on what I was suggesting in my original post:

The player has a sphere collision, when it interacts with something, it tell it to show its widget:

The Loot object:

It calculates the distance to the player, and pushes the data into the widget.

Image from Gyazo


There are many, many ways of achieving the final results. You could keep a list of object in the player and interact with that instead. Not a bad solution, more complex initial setup but probably more performant.

The above super simplistic, of course. If you want to communicate with multiple unrelated classes, do look into [Interfaces][3] rather than casting. Ideally, in addition to, actually.

Otherwise i just remember that i tried
to set the text in your example and it
didn’t work because i couldn’t plug
from the blue set node to a place that
made sense.

Judging by what was discussed above, may I suggest you also look into blueprint communication: Direct, Event Dispatchers and the aforementioned Interfaces. It’s pretty much impossible to achieve any data flow without knowing how these work. And it’s a wee bit beyond the scope of Answer Hub.

Hope it helps a little bit at least.

I’ll just add that calculating distance can be achieved much easier than demoed above. The suggestion takes into account that you will want to, eventually, send more complex data to and from the interactive objects.

Definitely more info than expected, thanks. I initially just wanted a little node on the purple comment… :d

The .gif represents pretty much the perfected end result! The sphere can be expanded by collecting perks.

One burning question about this example: What is the “W Widget” reference? A widget component that has been added to the actor or a variable that has been made right on the spot?

The Widget Component is housing a regular widget. We extract it from the component and create a direct reference.

Remember this bit?

No, afaik the widget does not know
it’s sitting pretty inside a
component.

The opposite is true, though - the component knows which widget it has. Rather than Get User Widget → Cast every time we need access, we do it once at Begin Play and store the reference so it’s easily accessible from then on, according to this:

it’s much easier to access an actor
that has calculated data than pull
that data out of a field of a widget
embedded in a component…


One way to create a reference quickly: right clicking on the pins and Promote to Variable - this will create a reference variable of the correct type. You can now use that variable to access data of any object it points to. In this case - the widget that was embedded in the component.

Enumeration.

A flag you choose from a dropdown. Widgets’ visibility can have 5 different states, but widget components can be either visible or not.

Enumerators can act as neat filter or switches that aren’t prone to typos (unlike strings):

301927-annotation-2020-05-14-220616.jpg

There’s also great synergy between enumerators and bitmasks.


The select node is dynamic:

Image from Gyazo

You can also right click the pins and change type.


edit: in your example you might be still working with the widget component rather than the widget embedded inside, which might actually be fine if all you want is to show and hide it

We extract it from the component and create a direct reference.

Actually now it clicked. What is the green link between Select and Set Visibility? (I have a different boolean Select node)

Edit: This is in an Actor.

What type of Enum should i change the Return Value to?

Depends on what you want to do; you skipped the cast and still working with the widget component rather than the widget inside it. You will be able to show / hide it - drag the Return Value on the New Visibility pin. But you will not be able to send / pull any data from the widget.

edit: a zipped project from the demo above in case you want to take it apart

https://drive.google.com/open?id=1_ksGX-Zusqra_bFwOJ7DbfwXzsZVRnYN