Page 1 of 1

I can not register an event

Posted: Mon Sep 10, 2018 12:32 pm
by WIZ4
I created a file in which I wrote a script that I had to run in the event.on_player_created. In the control.lua I also already have this event. But I do not want to move this function from mid.lua to control.lua.
control.lua:

Code: Select all

require "mid"
script.on_event(defines.events.on_player_created, function(event)
functiont1(event)
functiont2(event)
functiont3(event)
end)
mid.lua:

Code: Select all

local function whatever(event)
  local player = game.players[event.player_index]
  player.insert{name="iron-plate", count=10}
end
Event.register(defines.events.on_player_created, whatever)
I'm getting the error that I did wrong?
Screenshot_5.jpg
Screenshot_5.jpg (42.31 KiB) Viewed 1760 times
control.lua:7: > require "mid"
mid.lua:6: > Event.register(defines.events.on_player_created, whatever)

Re: I can not register an event

Posted: Mon Sep 10, 2018 12:56 pm
by darkfrei

Code: Select all

functiont1(event)

Re: I can not register an event

Posted: Mon Sep 10, 2018 1:03 pm
by FreeER

Re: I can not register an event

Posted: Mon Sep 10, 2018 4:46 pm
by eradicator
"Event.register()" is a function of the factorio standard library and the error says that "Event" doesn't exist, i.e. you do not have stdlib installed/required.

Regardless of that though you should not use script.on_event() and Event.register() in the same mod/scenario, as they try to do the same thing and will conflict with each other if you use them for the same event type.
WIZ4 wrote: But I do not want to move this function from mid.lua to control.lua.
In that case you can also return the function to the requiring module, and thus only need one event.

control:

Code: Select all

local whatever = require 'mid' --this
script.on_event(defines.events.on_player_created, function(event)
functiont1(event)
functiont2(event)
functiont3(event)
whatever(event) --this
end)
mid:

Code: Select all

local function whatever(event)
  local player = game.players[event.player_index]
  player.insert{name="iron-plate", count=10}
end
return whatever --this