For each loop widget

I am working through a project and in the level blueprint after the get all widgets of class node there is a foreachloop node. Why has a foreachloop node been added in this particular case?

It gets all widgets (the things you used like buttons,layers,textfields all sorts of thing) from this particular widget class and returns an array(see in your widget editor on the left where all your widgets are-sort of an array list. For every object it gets it set the variable main widget. Following the node i see that something is done wirh the widgets For Every Loop.

It’s sort of a sloppy / lazy way of doing things. It should be safe to just Get element 0 from the array since it’s the MainWidget and only 1 instance should ever exist…

Ideally you’d have a proper widget reference stored in one of the globally accessible classes like Game Mode or Game Instance and cast to obtain it. Or even better, use a dedicated widget manager object that keeps tracks of what’s valid and not - this is invaluable as the project grows in size.

In case more than 1 instance of MainMenu ever gets created that loop becomes a potential game breaker as you can no longer be certain which MainMenu widget is assigned to that variable.

If you’re 100% sure you create the widget once only, it should be safe to use the code as is. Still, feels sloppy.

Thanks for your response. I am looking to create another mainwidget class so hopefully this setup won’t interfere with that.