Initial wallotext Im little ashamed of
3) Just allow handler for a given element be defined in the definition of the element itself.Easy gui_element identification
Easy gui_element identification
tldr: on_gui_click event handlers are basically lookup functions written by different people with different degree of quality coding and which sole purpose is to find which ui button from an unknown multitude of those has been pressed. Instead of that: (see the text at the bottom of post)
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
I also update mods, some of them even work.
Recently I did a mod tutorial.
Re: Easy gui_element identification
You can compare elements directly:
if (event.element == game.get_player(event.player_index).gui.top.my_gui)
Also, element.name is unique in the element collection it resides (unique in the element.parent.children_names set).
Also also: element.parent points to the element that owns the element which can easily be used to crawl up the parent chain to the root gui element.
if (event.element == game.get_player(event.player_index).gui.top.my_gui)
Also, element.name is unique in the element collection it resides (unique in the element.parent.children_names set).
Also also: element.parent points to the element that owns the element which can easily be used to crawl up the parent chain to the root gui element.
If you want to get ahold of me I'm almost always on Discord.
Re: Easy gui_element identification
That would actually require a number of nested ifs:Rseding91 wrote:You can compare elements directly:
if (event.element == game.get_player(event.player_index).gui.top.my_gui)
Code: Select all
if ( game.get_player(event.player_index).gui.top.my_gui~=nil) then
if event.element == game.get_player(event.player_index).gui.top.my_gui.button1 then
elseif game.get_player(event.player_index).gui.top.my_gui.subgroup1~=nil then
if event.element == game.get_player(event.player_index).gui.top.my_gui.button2 then
end
end
end
Yes, but you don't know in which collection it resides. Only the full set of (name, parent_name, parent_parent_name,...,your_gui_name (and maybe top/left/center). And as forRseding91 wrote:Also, element.name is unique in the element collection it resides
you don't know the size of the set or the length of the chain for the element stored in event.element. Yes, a function can be written to determine the structure of the chain and the top parent, but once again, that's something that would get reimplemented by each modder, and would be implemented with various degree of correctness.Rseding91 wrote:Also also: element.parent points to the element that owns the element which can easily be used to crawl up the parent chain
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
I also update mods, some of them even work.
Recently I did a mod tutorial.
Re: Easy gui_element identification
Adil wrote:That would actually require a number of nested ifs:Rseding91 wrote:You can compare elements directly:
if (event.element == game.get_player(event.player_index).gui.top.my_gui)Certainly doable, but not very intuitive or motivating to do it this way. The suggestion is about making doing the right thing easier.Code: Select all
if ( game.get_player(event.player_index).gui.top.my_gui~=nil) then if event.element == game.get_player(event.player_index).gui.top.my_gui.button1 then elseif game.get_player(event.player_index).gui.top.my_gui.subgroup1~=nil then if event.element == game.get_player(event.player_index).gui.top.my_gui.button2 then end end end
Yes, but you don't know in which collection it resides. Only the full set of (name, parent_name, parent_parent_name,...,your_gui_name (and maybe top/left/center). And as forRseding91 wrote:Also, element.name is unique in the element collection it residesyou don't know the size of the set or the length of the chain for the element stored in event.element. Yes, a function can be written to determine the structure of the chain and the top parent, but once again, that's something that would get reimplemented by each modder, and would be implemented with various degree of correctness.Rseding91 wrote:Also also: element.parent points to the element that owns the element which can easily be used to crawl up the parent chain
Code: Select all
local myHandlers={}
local function myCreateElement(element_def,player_index,side,handler)
game.players[player_index].gui[side].add(element_def)
myHandlers[element_def.name]=handler
end
local myGuiHandler(event)
local handler=myHandlers[event.element]
if handler then
handler(event)
end
end
function onClickHi(event)
game.players[event.player_index).print("Hello to you, too.")
end
game.on_event(on_player_created, function(event)
myAddElement({type="button" caption="Hi!",name="hi"},event.player_index,"left",handler)
end)
You can make it as organized as you want. The api is low-level, and there's actually nothing wrong with if...elseif blocks. You're welcome to not like them, and lua actually makes it pretty esay to avoid them if you want. And as for "different people with different degree of quality coding," well, honestly, if doing some "if...elseif" blocks is taxing their coding skills, well, they're kind of screwed.
My Mods:
Nixie Tubes - numeric displays for your circuit networks!
Logistic Combinators - use logistics values in circuit logic! -
Autowire - automate red/green wire connections
Nixie Tubes - numeric displays for your circuit networks!
Logistic Combinators - use logistics values in circuit logic! -
Autowire - automate red/green wire connections
Re: Easy gui_element identification
Gui elements are persistent (saved and restored on load) the current Factorio Lua event system is not. Every time the game is exited and loaded every mod re-registers the event handlers it wants to use with the game.
If Gui elements did the same there net effect wouldn't be any different: you'd still need to know which elements are yours so you could register the handlers. I've seen very complex mods and never seen one have issues with naming the elements or handling events for the elements.
If Gui elements did the same there net effect wouldn't be any different: you'd still need to know which elements are yours so you could register the handlers. I've seen very complex mods and never seen one have issues with naming the elements or handling events for the elements.
If you want to get ahold of me I'm almost always on Discord.