Page 1 of 1

Event.register(defines.events.EVENT_NAME, FUNCTION_NAME)

Posted: Sat Jun 03, 2017 10:09 am
by darkfrei
Hi all.

I've found one nice solution how to make compilation of scripts.

in the control.lua must be

Code: Select all

require "script1"
require "script2"
...
and in script1.lua you are need something like

Code: Select all

local function script1_on_gui_click(event)
   -- here you code
end

local function script1_create_GUI(event)
   local player = game.players[event.player_index]

   if player.gui.top.script1_GUI == nil then
      local button = player.gui.top.add({ type = "sprite-button", name = "script1_GUI", sprite = "item/raw-fish" })		
      button.style.minimal_height = 38
      button.style.minimal_width = 38
      button.style.top_padding = 2
      button.style.left_padding = 4
      button.style.right_padding = 4
      button.style.bottom_padding = 2
   end
end

Event.register(defines.events.on_gui_click, script1_on_gui_click)
Event.register(defines.events.on_player_joined_game, script1_create_GUI)
Maybe it was obvious, but I did not know it.

Re: Event.register(defines.events.EVENT_NAME, FUNCTION_NAME)

Posted: Sat Jun 03, 2017 10:19 am
by Klonan
darkfrei wrote:Hi all.

I've found one nice solution how to make compilation of scripts.

in the control.lua must be

Code: Select all

require "script1"
require "script2"
...
and in script1.lua you are need something like

Code: Select all

local function script1_on_gui_click(event)
   -- here you code
end

Event.register(defines.events.on_gui_click, script1_on_gui_click)
Maybe it was obvious, but I did not know it.
This isn't some base game feature, but it added by a LuaLibrary, so it will not work if you do not include this library

Re: Event.register(defines.events.EVENT_NAME, FUNCTION_NAME)

Posted: Mon Jun 05, 2017 3:09 am
by Mooncat
And

Code: Select all

Event.register(defines.events.on_gui_click, script1_on_gui_click)
Event.register(defines.events.on_player_joined_game, script1_create_GUI)
is just the same as

Code: Select all

script.on_event(defines.events.on_gui_click, script1_on_gui_click)
script.on_event(defines.events.on_player_joined_game, script1_create_GUI)
which is the official way to add event handlers.


hm...
I think your point is you can do

Code: Select all

function script1_on_gui_click(event)
    ...
end

script.on_event(defines.events.on_gui_click, script1_on_gui_click)
instead of using anonymous function

Code: Select all

script.on_event(defines.events.on_gui_click, function(event)
    ...
end)
In that case, the require statements are not necessary if the function is in the same file.
"require" is like "load that file for me so I can access the things inside it".

Re: Event.register(defines.events.EVENT_NAME, FUNCTION_NAME)

Posted: Mon Jun 05, 2017 5:26 am
by Nexela
If you are using factorio stdlib events make sure to take advantage of the gui system

Code: Select all

Event.register(defines.events.on_gui_click, script1_on_gui_click)

Code: Select all

Gui.on_click("element_name_or_pattern", script1_on_gui_click)