I can not register an event

Place to get help with not working mods / modding interface.
Post Reply
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 206
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

I can not register an event

Post 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 835 times
control.lua:7: > require "mid"
mid.lua:6: > Event.register(defines.events.on_player_created, whatever)
Last edited by WIZ4 on Mon Sep 10, 2018 12:58 pm, edited 1 time in total.
My native language is russian. Sorry if my messages are difficult to read.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2883
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: I can not register an event

Post by darkfrei »

Code: Select all

functiont1(event)

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: I can not register an event

Post by FreeER »


User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5201
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: I can not register an event

Post 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
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”