Pass exposed-on-spawn parameter to child Widget

I have a widget ChildWidget that is used in another widget ParentWidget

In ChildWidget, I have exposed on spawn a variable Text of type Text, that I am using in the Pre Construct with a Text widget.

In ParentWidget, I have added ChildWidget to the hierarchy. I set its Text parameter value to “(parent default)”. I also exposed on spawn another variable Text of type Text. In the Pre Construct event, I assign ChildWidget’s Text variable with the value from ParentWidget’s Text.

In the level blueprint, I create a ParentWidget with “Level value” as Text_parent’s value.

However when I hit play, the value used in ChildWidget is not the value passed from the level blueprint (“Level value”), but the value set in ParentWidget (“Parent default”), because ChildWidget’s Pre Construct is executed before ParentWidget’s

How can I achieve that ? The idea is to have a ChildWidget with some configurable content, thanks to an public variable, and initalized in Pre Construct or Contruct. Here I used a simple text here, but it could be anything more complex. And use it in a parent widget also in a configurable manner when I create the widget.

Like in pseudo-c++ code, if I called ChildWidget’s “constructor” from ParentWidget’s "constructor, while forwarding the parameters

class ChildWidget : Widget
{
public:
    ChildWidget(Text text);
}

class ParentWidget : Widget
{
public:
    ParentWidget(Text text):
private:
    ChildWidget m_childWidget;
}

ParentWidget::ParentWidget(Text text) : m_childWidget(text)
{
}

void LevelBlueprint()
{
    auto widget = ParentWidget("Level value");
}

Hi m.leber

The easiest way would be to pass a reference from the parent widget to the child widget. This way in the child can access all the properties from the parent directly from the binding tab.

This is a screenshot from another answer where I showed how to bind an output to a variable in the player state. However it works the same.

Hope this helps.

Alex

To your screenshot regarding the Pre Construct. If you change a variable in the parent widget on pre construct / construct. You will need to call some kind to function to refresh the child widget (if the data is not directly bound).

Hi, thank you for your answer.
Regarding the reference to the parent into the child, I would like to design the child to be generic, composable and agnostic of its use, ie to be able to be used inside any widget or even without a parent widget (created and added to the viewport directly), like any other widget

And regarding the binding, the text was just and example, I would just like to be able to use a param in the construct script

In this case, the child widget has to be constructed first with some arbitrary parameter (the unused value set in the parent), and the, changed with an updated value when the parent is constructed ?

You have a couple of options.

  1. Store a reference to the child in the parent. When the parent updates, add logic to manually push that data to the child widget. Event Dispatchers could be extremely useful here depending on the complexity and frequency of the data updating.

  2. Use a base class.
    Create a base class called “WBP_Master” or something similar and add a variable “Parent” of the same type with expose on spawn checked. Make sure you remove all child widgets from the Master’s designer view. Reparent your other widgets to this class.

Now your widgets will have the option to set a parent if required and then you just add the logic if that parent is valid (obviously the variable will need to be set when it’s spawned). From there, you can cast to the type of the parent widget and access its data. However, if you want it to remain completely universal, you can add further variables in the WBP_Master so there is no need for any further casting.

  1. You could also push the data to some universal space when it’s updated on the parent and then access it from the child without any relationship with the parent required.

Hope these options help.

Regarding your comment:

In this case, the child widget has to
be constructed first with some
arbitrary parameter (the unused value
set in the parent), and the, changed
with an updated value when the parent
is constructed ?

The child wouldn’t need to be constructed first, just the reference needs to be set at some point before the binding will update.

Good luck

Alex