Widget creates a nested widget

When a widget creates a child widget inside it. What is the best way for that child to get a reference to it? The “Get parent” doesn’t seem to work.

I am trying to avoid get all actors of class or interface, etc. I just want to create a reference at the point I create the child.

Is there something like a ‘get reference to the thing that created me?’

Thanks in advance.

One way to do it is to create a variable of the parent type in the child and then setting that variable when the parent creates it.

Not sure if it’s the best way to do it, but hope it helps!

When a widget creates a child widget
inside it. What is the best way for
that child to get a reference to it?

Sure you can expose a reference and push in the widget as value.

But a better question is why would you need to do it? The fewer hard references there are, the better. It’s a win-win if you can avoid creating them.

Pros:

  • no issue with garbage collection
  • no null pointers
  • you can (un)bind as many as you need
  • copying signatures speeds things up dramatically

Cons:

  • not everyone likes red wires

If you need the child talk back to the parent, create an Event Dispatcher in the child and:

Above, the parent (and whoever else who has registered!) will hear about it and can interpret the call any way they want. Below, the child calls the dispatcher:

323762-screenshot-5.jpg

If you ever manually add a child to a parent, you can also:

It’s the most natural way of communication for a parent-child widget relationship. You can see it everywhere in blueprints, often without realising what it is; like a component in an actor, for example:

323753-screenshot-4.jpg


If you absolutely must do it, create a parent reference in the child, flag it as Instance Editable and Expose it on Spawn

323763-screenshot-6.jpg

Meh. Avoid it if you can.

And if you really, really, really dislike red wires, this also works fine:

What’s more, this will even work if you wrap it in a function… And that’s without wiring delegates as attributes. So you can keep it all nice and clean.

Whoa. Didn’t even know about the create event node. Excellent post!

Thanks for the detailed answer. And in response to your query of Why I was looking to get a reference to the parent widget from the child was because the child was a slot widget that on mouse over would pass its slot struct details back up to the Parent inventory Screen so it would display the ‘details’ item description, etc. that was currently living in the parent. Regards.

Worked like a charm.