find_gui_filtered() for LuaPlayer, LuaGui, and LuaGuiElement

Things that we aren't going to implement
Post Reply
credomane
Filter Inserter
Filter Inserter
Posts: 278
Joined: Tue Apr 12, 2016 6:21 pm
Contact:

find_gui_filtered() for LuaPlayer, LuaGui, and LuaGuiElement

Post by credomane »

We have a LuaSurface.find_entities_filtered. Would it be possible to also have a find_gui_filtered that works the same way?
Then attach that function to LuaPlayer, LuaGUI and LuaGuiElement? Below is my working proof-of-concept that such a function is already very much possible.

I'm experimenting with creating a GUI lib for my mods and one such experiment was a Jquery-ish selector for the GUI!
It made things a lot easier (to me) when working on GUI stuff; no more player.gui.top.myframe.some_flow.some_other_flow.my_element. That you have to remember to update in a hundred places anytime you rearrange your GUI. Sure the direct route is always faster but ease-of-use is awesome too.

The bad news with my implementation is I had to abuse the new 0.15 "help()" function to make it work exactly the way I wanted. This is slow-n-ugly but doesn't seem to cause any noticeable issues in my limited testing.
Initially I tried a non-hack way but ran into issues with Factorio throwing errors no matter what I tried. :(


-------------------------------------------
gui.get
Now you can just do gui.get() and get back a table of matched elements or the single matched element directly or nil if no matches.

All of the following work and return the same LuaGuiElement:

Code: Select all

gui.get(player, "my_element")
gui.get(player.gui, "my_element")
gui.get(player.gui.top, "my_element")
gui.get(player.gui.top.myframe, "my_element")

--Even these will work
gui.get(player.gui.top.myframe, "my_elem*")
gui.get(player.gui.top.myframe, "*_elem*")
gui.get(player.gui.top.myframe, "*_element")

--If you want to get real fancy use Lua Patterns.
[edit]
Figured out my previous issue with pcall. I forgot the anonymous "wrapper" function. Too much coding and not enough sleep. Updated code block with new snippet

Rseding91
Factorio Staff
Factorio Staff
Posts: 13232
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: find_gui_filtered() for LuaPlayer, LuaGui, and LuaGuiElement

Post by Rseding91 »

Why not simply store references to the GUI elements as they're created so you can just reference them directly when desired in your code?

Then you don't care in what flow/section it ends up - you just reference it as desired and it works.

Additionally all events that happen in response to user interaction related to GUIs pass the GUI element itself for easy modification.
If you want to get ahold of me I'm almost always on Discord.

credomane
Filter Inserter
Filter Inserter
Posts: 278
Joined: Tue Apr 12, 2016 6:21 pm
Contact:

Re: find_gui_filtered() for LuaPlayer, LuaGui, and LuaGuiElement

Post by credomane »

Let's say I have a GUI setup with over 30 interactive elements for every player. Some elements might only exist for some players and not others. A interaction with one element (it got clicked for example) might cause several others to need updating/created/removed. I would have to maintain a special table just to hold all those element references per player and the lua code to manage their existence or non-existence. That became a migraine fast. Especially making sure I had all the element.valid checks in all the right places. I had an entire non-reuseable file dedicated to managing this behind-the-scene GUI. Now this tiny function replaced an entire overly cumbersome non-reuseable gui "management" system with a portable function that does the same job only better.

Post Reply

Return to “Won't implement”