Page 1 of 1

Control.lua

Posted: Sun Jul 05, 2015 6:09 pm
by kiba
Hi, I tried to add game.onevent stuff to another file outside control.lua and require said file, but nothing happens.

Same thing when I tried game.player.print("blah").

For everything else, like having the gui created and destroyed, works just fine. It just had to be called from inside control.lua.

Any idea why it works like that or what I am doing wrong?

Re: Control.lua

Posted: Sun Jul 05, 2015 11:38 pm
by ljdp
I think you can only hook onto an event once. What I do is hook onto all the events in control.lua and then forward any events on to functions defined in other files.

Re: Control.lua

Posted: Mon Jul 06, 2015 1:36 am
by kiba
ljdp wrote:I think you can only hook onto an event once. What I do is hook onto all the events in control.lua and then forward any events on to functions defined in other files.
That isn't what I did. I hooked up the event only once in files other than the control.lua.

Re: Control.lua

Posted: Mon Jul 06, 2015 10:03 am
by Choumiko
Posting the relevant parts of control.lua and the other files would be helpful.
I know that Alphamod does what you want, it requires its scripts/EventHandler.lua in control.lua and registers/handles the events there.

Re: Control.lua

Posted: Mon Jul 06, 2015 3:35 pm
by kiba
Choumiko wrote:Posting the relevant parts of control.lua and the other files would be helpful.
I know that Alphamod does what you want, it requires its scripts/EventHandler.lua in control.lua and registers/handles the events there.
The trouble is...I already moved on. Might try again later and see if it's just me.

Re: Control.lua

Posted: Tue Jul 07, 2015 12:25 am
by kiba
Ok, let try this:

I moved this block of code to a file called gui.lua:

Code: Select all

game.onevent(defines.events.onguiclick, function(event)
  local element = event.element
  log("Clicked element: "..element.name)
  if element.name == "repair_button" then
    initializeRepairStatus()
  elseif element.name == "close" then
    destroyWindow()
  end
end)
But then the event stuff stopped working.

This is how I load gui.lua:

Code: Select all

require("gui")

Re: Control.lua

Posted: Wed Jul 08, 2015 11:37 pm
by DaveMcW
You still didn't post the relevant functions.

Try this, no extra functions at all:

Code: Select all

game.onevent(defines.events.onguiclick, function(event)
  local element = event.element
  game.players[1].print("Clicked element: "..element.name)
end)