Page 1 of 1

How to make a GUI?

Posted: Sun Oct 02, 2016 5:09 am
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?

Re: How to make a GUI?

Posted: Sun Oct 02, 2016 6:49 am
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

Re: How to make a GUI?

Posted: Sun Oct 02, 2016 10:29 am
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.

Re: How to make a GUI?

Posted: Sun Oct 02, 2016 10:43 am
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

Re: How to make a GUI?

Posted: Tue Oct 04, 2016 10:05 pm
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

Re: How to make a GUI?

Posted: Tue Oct 04, 2016 10:09 pm
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

Re: How to make a GUI?

Posted: Wed Oct 05, 2016 6:21 am
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?

Re: How to make a GUI?

Posted: Wed Oct 05, 2016 9:16 am
by prg

Code: Select all

for i = 1, 5 do
     local wantedchild = "labelfillup"..i
     local currentchild = maintable.children[wantedchild]
     print(currentchild.name)
end

Re: How to make a GUI?

Posted: Wed Oct 05, 2016 9:56 am
by LordLoki
thanks a lot for the help will test it when i am home tonight

Re: How to make a GUI?

Posted: Wed Oct 05, 2016 12:13 pm
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

Re: How to make a GUI?

Posted: Wed Oct 05, 2016 12:29 pm
by LordLoki
Thanks a ton aubergine thats exactly what i need

Re: How to make a GUI?

Posted: Wed Oct 05, 2016 8:12 pm
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

Re: How to make a GUI?

Posted: Wed Oct 05, 2016 8:39 pm
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

Re: How to make a GUI?

Posted: Wed Oct 05, 2016 8:41 pm
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]