How to make a GUI?

Place to get help with not working mods / modding interface.
Post Reply
LordLoki
Inserter
Inserter
Posts: 25
Joined: Tue Apr 12, 2016 7:17 am
Contact:

How to make a GUI?

Post by LordLoki »

Hi there,

I would like to have a GUI to make some settings in my Mod but i cant find any good tutorials on how to do it.
I was looking at other mods but i can not get it to work somehow.
Does someone have a good tutorial on that or can give some tips?

also i have a question for custome events
For example in the advanced logistics Mod there is a custom event

Code: Select all

script.on_event("ls-toggle-gui", function(event)

	local index = event.player_index
	local player = game.players[index]
	local visible = global.guiVisible[index]

	--- reset player position if in location view mode
	local locationFlow = player.gui.center.locationFlow
	if locationFlow ~= nil then
		resetPosition(player, index)
	end

	if visible == 0 then
		getLogisticNetworks(player.force)
		showGUI(player, index)
	else
		hideGUI(player, index)
	end
end)
but when i use that event and change the name to "lc-toggle-gui" for example i get an error why is that?

LordLoki
Inserter
Inserter
Posts: 25
Joined: Tue Apr 12, 2016 7:17 am
Contact:

Re: How to make a GUI?

Post by LordLoki »

QUick update i figured out how to get the custom function going. and i was able to add gui elements like a button etc.
Now i just need to find out how i change sizes and positions etc.

We will get there :D

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: How to make a GUI?

Post by Adil »

Did you see the api-documentation? Gui elements have a set of parameters that can be specified on their creation some of them define their looks. There's also `style` field in it, which governs the details. You can predefine the style and then apply it during the addition of element, or it seem that you can manipulate it directly in the element created.
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.

LordLoki
Inserter
Inserter
Posts: 25
Joined: Tue Apr 12, 2016 7:17 am
Contact:

Re: How to make a GUI?

Post by LordLoki »

Hi Adil,

Thanks for pointing me to the API. Was already too late for me Yesterday.
Found the LuaStyle Section that should answer all my Questions for now :)
Will check out later what i can get done if i am stuck i will come back

Greets and Big Thanks Loki

LordLoki
Inserter
Inserter
Posts: 25
Joined: Tue Apr 12, 2016 7:17 am
Contact:

Re: How to make a GUI?

Post by LordLoki »

Hi Folks,

Another Question about gui elements.

Is there a way to refer to an gui element by variable?
lets say i have this element created in a loop

Code: Select all

gui.maintable.add({type = "label", name = "labelfillup" .. k, caption = "Fillup Amount: "})
and i now want to get the one with labelfillup5 is that possible anf if so how?

thanks and greets
Loki

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: How to make a GUI?

Post by aubergine18 »

If you have a pointer to the parent gui element, you can access it's children like so:

Code: Select all

local labelfillup5 = maintable.children.labelfillup5
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

LordLoki
Inserter
Inserter
Posts: 25
Joined: Tue Apr 12, 2016 7:17 am
Contact:

Re: How to make a GUI?

Post by LordLoki »

ah sorry i think my question was not phrased properly.

I know i could access the variable like that but i can not access it like this

Code: Select all

for i = 1 to 5 do
     local wantedchild = "labelfillup"..i
     local currentchild = maintable.children.wantedchild
    print  currentchild.name
end
is there a way to do that?

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: How to make a GUI?

Post by prg »

Code: Select all

for i = 1, 5 do
     local wantedchild = "labelfillup"..i
     local currentchild = maintable.children[wantedchild]
     print(currentchild.name)
end
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

LordLoki
Inserter
Inserter
Posts: 25
Joined: Tue Apr 12, 2016 7:17 am
Contact:

Re: How to make a GUI?

Post by LordLoki »

thanks a lot for the help will test it when i am home tonight

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: How to make a GUI?

Post by aubergine18 »

Code: Select all

for elementName, guiElement in pairs( parentGuiElement ) do
  -- elementName = name of child element
  -- guiElement = the actual gui element (guiElement.name == elementName)
end
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

LordLoki
Inserter
Inserter
Posts: 25
Joined: Tue Apr 12, 2016 7:17 am
Contact:

Re: How to make a GUI?

Post by LordLoki »

Thanks a ton aubergine thats exactly what i need

LordLoki
Inserter
Inserter
Posts: 25
Joined: Tue Apr 12, 2016 7:17 am
Contact:

Re: How to make a GUI?

Post by LordLoki »

Hi again,

the solution prg gave me works excellent

the loop with pairs does not work

i have this gui

Code: Select all

local gui = player.gui.center.add({type = "frame", name = "LokiChest_gui", direction = "vertical"})
	gui.add({type = "table", name = "title", colspan = 2, style = "lokis-table"})
	gui.title.add({type = "label", name = "entity_name", caption = "entity-name:" .. entity.name})
	gui.title.add({type = "label", name = "entity_position", caption =  "  " .. entity.position.x .. ", " .. entity.position.y})
	gui.add({type = "button", name = "LokiChest_gui_close", caption = "close", style = "lokis-closebutton"})
	
	for k, slot in pairs(chestinformation) do
		gui.add({type = "flow", name ="row-"..k,direction="horizontal"})
		local currentstring = "row-"..k
		currentchild = gui[currentstring]
		currentchild.add({type = "sprite-button", name ="itemslot-"..k, sprite="item/"..slot.name,style="side_menu_button_style"})
		currentchild.add({type = "label", name = "labelmax" .. k, caption = "Fillup Amount: "})
		currentchild.add({type = "textfield", name = "max-"..k, text=slot.count,style="lokis-textfield"})
		currentchild.add({type = "label", name = "labelmin-"..k, caption = "Start Refill at: "})
		currentchild.add({type = "textfield", name = "min-"..k, text=slot.minrequest,style="lokis-textfield"})
	end 
	
	gui.add({type = "button", name = "savebutton", caption = "Save"})
this function

Code: Select all

function gui_update_requests(entity,gui)
message_all("save button pressed")
	for elementName, guiElement in pairs(gui) do
		message_all(elementName)
	end
end
and i call it like this

Code: Select all

function gui_update_requests(entity, player.gui.center)
the save button pressed message comes through the second one returns __Self when i message elementName and crashes when i try it with guielement.name
what am i doing wrong

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: How to make a GUI?

Post by aubergine18 »

Ah, my bad... needs .children_names like so:

Code: Select all

for _, childName in pairs(gui.children_names) do
   local childElement = gui[childName]
  game.print( childElement.name )
  -- etc
end
Example; https://github.com/Helfima/helmod/blob/ ... n.lua#L462
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

LordLoki
Inserter
Inserter
Posts: 25
Joined: Tue Apr 12, 2016 7:17 am
Contact:

Re: How to make a GUI?

Post by LordLoki »

Hehe cool that was my workaround so far anyway :D

Code: Select all

function gui_update_requests(entity,mygui)
	message_all("save button pressed")
		for k, elementName in pairs(mygui.children_names) do
			message_all(elementName)
			local guiElement = mygui[elementName]

Post Reply

Return to “Modding help”