How can I refer to an GuiElement without listing all its child names?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

How can I refer to an GuiElement without listing all its child names?

Post by WIZ4 »

Instead of this:

Code: Select all

function on_click(event)
	local name = event.element.name

	for i, str in pairs(global.whatever) do
	if name == 'button'..i then 
	player.gui.center.frame_1.frame_2.frame_3.flow['button'..i].enabled = false
	end
	end
end
Something like that:

Code: Select all

function on_click(event)
	local name = event.element.name

	for i, str in pairs(global.whatever) do
	if name == 'button'..i then 
	event.element.name.enabled = false
	end
	end
end
I saw that there is "children" and "children_names", but I do not know how to use them.
My native language is russian. Sorry if my messages are difficult to read.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: How can I refer to an GuiElement without listing all its child names?

Post by DaveMcW »

Code: Select all

function on_click(event)
  event.element.enabled = false
end

User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

Re: How can I refer to an GuiElement without listing all its child names?

Post by WIZ4 »

DaveMcW wrote:
Tue Nov 06, 2018 3:35 pm

Code: Select all

function on_click(event)
  event.element.enabled = false
end
Thanks!
My native language is russian. Sorry if my messages are difficult to read.

User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

Re: How can I refer to an GuiElement without listing all its child names?

Post by WIZ4 »

I would like to ask again. Is there a simplified method for calling other elements if they are in another function?
Such as how to reduce it?

Code: Select all

game.print(player.gui.center.frame_1.frame_2.frame_3.flow1.radiobutton.state)
My native language is russian. Sorry if my messages are difficult to read.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: How can I refer to an GuiElement without listing all its child names?

Post by eradicator »

Store the references of frequently used elements:

Code: Select all

global.blahblah = {radiobutton = player.gui.center.frame_1.frame_2.frame_3.flow1.radiobutton}
game.print(global.blahblah.radiobutton.state)
Can also be compared:

Code: Select all

local function on_event(event)
  if event.element == global.blahblah.radiobutton then
    game.print(event.element.state)
    end
  end
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”