Page 1 of 1

Add GUI elements in the middle of a parent

Posted: Sat Sep 26, 2020 4:58 am
by raiguard
THE REQUEST:
Add a method for adding a new child to a LuaGuiElement somewhere other than the end. Perhaps add an index key to the element's table that specifies the index in the children list it should be inserted?

THE REASON:
I can guarantee that I'm not the first one to ask for this, but I couldn't find any threads on it, so here I am. Right now, if you need to insert something entirely new in the middle of a GUI, the only way to go about that is to destroy everything after it and re-add them after adding your new bit. This imposes severe limitations on the optimizations we can do, and since creating GUIs is just about the slowest thing you can do in Factorio, it adds up quickly.

The motivation for my asking this now is that I am working on tooling to make GUIs easier. My current work is live in the form of flib's GUI module, but while that code makes creating a GUI a lot easier, updating an already created GUI is still very difficult. In fact, it gets even more difficult with this tooling, because most elements don't get names, so you are forced to either save every element to global or navigate by child index.

If we could insert GUI elements in the middle of other ones, instead of at the end, then I believe I could write sufficiently good tooling for both creating and updating GUIs with one structure. If not, then I'll have to again resort to destroying and recreating everything if something new needs to be added in the middle, absolutely slaughtering performance.

I will hope for the best!

Re: Add GUI elements in the middle of a parent

Posted: Sat Sep 26, 2020 8:27 am
by Therenas
Now this whole request does seem a bit unlikely to happen, but if it did, it would be cool if we could replace elements by index too. I can see this being useful for tables especially.

Re: Add GUI elements in the middle of a parent

Posted: Sat Sep 26, 2020 8:41 am
by Qon
This is the most obvious weirdly missing thing when I started doing mods with GUI. You can abuse flow elements to sort of get insertion, but then you need to make ugly hacks that make the element tree a big workaround that makes every insertion operation add even more elements (for arbitrary insertions). That will just tank performance eventually and make the GUI much more complicated to develop. +1

Re: Add GUI elements in the middle of a parent

Posted: Tue Sep 29, 2020 2:30 am
by eradicator
Raiguard wrote:
Sat Sep 26, 2020 4:58 am
I can guarantee that I'm not the first one to ask for this,
Weird, i thought i requested moving of LuaGuiElement years ago, but i can't find any old threads about it.
+0.5 because creation without moving is insufficient. But if either should happen the other is probably trivial to implement anyway.

Re: Add GUI elements in the middle of a parent

Posted: Sun Nov 29, 2020 9:49 pm
by Therenas
Pretty sure this was implemented with 1.1

Re: Add GUI elements in the middle of a parent

Posted: Thu Dec 03, 2020 11:49 am
by Bilka
Yep, just to make this clear for future readers: the index property in LuaGuiElement::add that was added in 1.1 allows you to choose where your new element is inserted.