find_gui_filtered() for LuaPlayer, LuaGui, and LuaGuiElement
Posted: Wed May 31, 2017 5:05 am
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.
-------------------------------------------
All of the following work and return the same LuaGuiElement:
[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
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.
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.
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