GUI Buttons

Place to get help with not working mods / modding interface.
Turtle
Fast Inserter
Fast Inserter
Posts: 240
Joined: Sat May 31, 2014 9:45 pm
Contact:

GUI Buttons

Post by Turtle »

Hi all! I've tried looking at other mods to see how buttons work, but they're kinda complicated and I can't seem to follow. I just want some buttons to simply click and perform one action. For example: click one button and it gives me one copper plate, click another and it gives me one iron plate. I'd appreciate it if anyone can point me in the right direction!
Devcod
Burner Inserter
Burner Inserter
Posts: 14
Joined: Fri Aug 08, 2014 8:14 pm
Contact:

Re: GUI Buttons

Post by Devcod »

Code: Select all

game.onevent(defines.events.onguiclick, function(event)
  if event.element.name == "your-button-name" then
    -- example: heal player
    game.player.character.health = 100
    -- example: add 10 iron plate
    game.player.insert({name="iron-plate", count="10"})
  end
end)
Not tested, but it does look like that.
Turtle
Fast Inserter
Fast Inserter
Posts: 240
Joined: Sat May 31, 2014 9:45 pm
Contact:

Re: GUI Buttons

Post by Turtle »

Devcod wrote:

Code: Select all

game.onevent(defines.events.onguiclick, function(event)
  if event.element.name == "your-button-name" then
    -- example: heal player
    game.player.character.health = 100
    -- example: add 10 iron plate
    game.player.insert({name="iron-plate", count="10"})
  end
end)
Not tested, but it does look like that.
Thanks! That helped me understand how it works.

I got it to work once, but now clicking the buttons does nothing at all. Here's my control.lua:

Code: Select all

require "defines"

game.onevent(defines.events.ontick, function(event)
	if game.player.vehicle and game.player.vehicle.valid and game.player.vehicle.type == "car" then
		if game.player.gui.left.frameDA ~= nil then game.player.gui.left.frameDA.destroy() end
		frameDA = game.player.gui.left.add{type = "frame", name = "frameDA", direction = "horizontal"}
		local tab = frameDA.add{type ="table", name = "tableDA", colspan = 3}
		tab.add{type = "label", name = "DAblank1", caption = " "}
		tab.add{type = "button", name = "buttonDANorth", caption = "N"}
		tab.add{type = "label", name = "DAblank2", caption = " "}
		tab.add{type = "button", name = "buttonDAWest", caption = "W"}
		tab.add{type = "button", name = "buttonDASouth", caption = "S"}
		tab.add{type = "button", name = "buttonDAEast", caption = "E"}
	elseif game.player.gui.left.frameDA then
		game.player.gui.left.frameDA.destroy()
	end
end)

game.onevent(defines.events.onguiclick, function(event)
	if event.element.name == "buttonDANorth" then
		game.player.vehicle.orientation = 0.0
		game.player.print("North")
  elseif event.element.name == "buttonDAWest" then
		game.player.vehicle.orientation = 0.75
		game.player.print("West")
	elseif event.element.name == "buttonDASouth" then
		game.player.vehicle.orientation = 0.5
		game.player.print("South")
	elseif event.element.name == "buttonDAEast" then
		game.player.vehicle.orientation = 0.25
		game.player.print("East")
  end
end)
I just want to make buttons to point the car in a straight line in the direction clicked to make it easier to drive around in bases. I was able to make the GUI appear when the player is in the car and disappears when the player leaves the car. I'm using the prints to test the buttons, but nothing is printing when I click the buttons.
Devcod
Burner Inserter
Burner Inserter
Posts: 14
Joined: Fri Aug 08, 2014 8:14 pm
Contact:

Re: GUI Buttons

Post by Devcod »

You can add this little code to your if elseif statement in onguiclick event, before end:

Code: Select all

    else
        game.player.print(event.element.name or "Element missing name!")
It should print you a name of gui element clicked or "Element name missing!" in case it doesn't exists (== nil). It should never be nil, so it will give you name of clicked element - this is first step to see where is your problem.

If it doesn't print anything then your event is not fired when it should be. In this case I do not know anything that may help you.

PS. You should not check if player entered car every tick. It will be checked 60 times a second (and if player is in vehicle then all of your code inside will be done 60 times a second). You can do it every 30 or 60 ticks (0.5 or 1 second). To do so, add one of these to your ontick event:

Code: Select all

if math.mod(event.tick, 30) == 0 then

Code: Select all

if math.mod(event.tick, 60) == 0 then
It need to be new if statement (starting before if game.player.vehicle ... and ending after end in your statement). It is not important as long as you are only testing, but it is good to do it anyway.
Turtle
Fast Inserter
Fast Inserter
Posts: 240
Joined: Sat May 31, 2014 9:45 pm
Contact:

Re: GUI Buttons

Post by Turtle »

Thanks for pointing me in the right direction Devcod. I got it working now. Something seemed very off about the way things were coded and didn't make sense to me, but I changed some of the code and now understand it. I'm using

Code: Select all

if game.tick%20 == 0 then
instead of what you suggested because it was throwing me an error about invalid mod. Not sure if I was supposed to change "mod" to the actual mod name, but regardless, got it working. I'm using tick%20 because tick%60 (1 second I believe) was too slow.

I was also using a line I copied from another mod to resolve a crashing issue, but I've deleted that line and added my own way of handling it. It hasn't crashed and works as it should in my initial testing, but I'm going to play with it some more to check for other bugs. If all goes well, I'll release it in the next couple of days. :D

For reference for others having similar issues:
original code I copied from another mod:

Code: Select all

if game.player.gui.left.frameDA ~= nil then game.player.gui.left.frameDA.destroy() end
translated to English: if frameDA exists, then destroy it. Then create the frame again in the next part of the code. That didn't make sense to me, but it was preventing a crash. I've changed it to:

Code: Select all

if game.player.gui.left.frameDA == nil then
translated to English: if frameDA doesn't exist then... create frame
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: GUI Buttons

Post by darkfrei »

I'm learning GUI now, but it's not enough easy examples. So, I have fixed the one.

Same code, but for 0.14.22:

Code: Select all

script.on_event(defines.events.on_tick, function(event)
	local player = game.players[1] -- bad coded for single player
	if player.vehicle and player.vehicle.valid and player.vehicle.type == "car" then
		if player.gui.left.frameDA ~= nil then 
			player.gui.left.frameDA.destroy() 
		end
		frameDA = player.gui.left.add{type = "frame", name = "frameDA", direction = "horizontal"}
		local tab = frameDA.add{type ="table", name = "tableDA", colspan = 3}
		tab.add{type = "label", name = "DAblank1", caption = " "}
		tab.add{type = "button", name = "buttonDANorth", caption = "N"}
		tab.add{type = "label", name = "DAblank2", caption = " "}
		tab.add{type = "button", name = "buttonDAWest", caption = "W"}
		tab.add{type = "button", name = "buttonDASouth", caption = "S"}
		tab.add{type = "button", name = "buttonDAEast", caption = "E"}
	elseif player.gui.left.frameDA then
		player.gui.left.frameDA.destroy()
	end
end)

script.on_event(defines.events.on_gui_click, function(event)
	local player = game.players[event.player_index]
	if event.element.name == "buttonDANorth" then
		player.vehicle.orientation = 0.0
		player.print("North")
	elseif event.element.name == "buttonDAWest" then
		player.vehicle.orientation = 0.75
		player.print("West")
	elseif event.element.name == "buttonDASouth" then
		player.vehicle.orientation = 0.5
		player.print("South")
	elseif event.element.name == "buttonDAEast" then
		player.vehicle.orientation = 0.25
		player.print("East")
	end
end)
But all buttons do nothing.
Attachments
gui_002.png
gui_002.png (1.57 KiB) Viewed 2919 times
Post Reply

Return to “Modding help”