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

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

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

Post 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.
Last edited by darkfrei on Sat Jun 03, 2017 10:25 am, edited 1 time in total.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

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

Post 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

User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1190
Joined: Wed May 18, 2016 4:55 pm
Contact:

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

Post 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".

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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)

Post Reply

Return to “Modding discussion”