GetAllWidgets how does it work?

Hi, I’m trying to get child widgets using GetAllWidgets. It works under debugger but not when I run PIE. Is there some kind of delayed construction?

TArray <UWidget *> widgets;
userWidget->WidgetTree->GetAllWidgets (widgets);

for (UWidget * childWidget : widgets)
// log GetDisplayName (childWidget) , works under debugger if I put breakpoint. PIE, returns 0 child.

What context do you try to access the widgets from, and which event do you use to do so? Usually in my experience, the PIE has been the friendly one, as certain things come preloaded in the editor. Could be that Debug has even more preloaded.

Anyways, assuming this is in a UUserWidget, OnInitialize() is going to initialize the child widgets in parallel. This effectively means that there is no guarantee that the widgets exist yet. Construct() will run after OnInitialize(), so everything should be initialized at that point.

Use OnInitialize() to process things that only involve itself only. Construct() can effectively process things that involve other widgets .

Technically, PreConstruct also exists, though I haven’t used it a lot. I understand that it is meant to handle statically placed widgets so that you can see them in the editor. The event runs before the game starts, but after some things have loaded.

Hi, I’m trying to get child widgets using GetAllWidgets. It works under debugger but not when I run PIE. Is there some kind of delayed construction?

TArray <UWidget *> widgets;userWidget->WidgetTree->GetAllWidgets (widgets);
for (UWidget * childWidget : widgets)// log GetDisplayName (childWidget) , works under debugger if I put breakpoint. PIE, returns 0 child.

It is a user widget, but I call it on a OnClick button. That’s why it is confusing to me. It should already be initialized since the button is there for me to click

Do you spawn the userWidget dynamically from the button, or is it in the viewport already at this point? After reading about this, the debugger can access objects that are not yet fully registered or pending destruction. It sounds like you’re trying to access the widgets either too early or too late. If it’s not in the viewport yet, debugger might also be able to access the widgets, where the PIE might throw them to the garbage collector.

Try using IsInViewPort() to ensure that the userWidget is fully registered.

The button is already in userwidget and added viewport at level beginplay. It can’t be too early becos I had to click on the button to call the function GetallWidgets. The function is EnableWidgetButtons.