Page 1 of 1
script.on_init
Posted: Sat Sep 10, 2016 5:00 pm
by Qfa
Hi, I'm currently working on a mod where I need to call a function once right after the creation of a world. I tried to use script.on_init to do that but i always get the same error
attempt to index global 'script' (a nil value)
While i've often be able to solve problems like that alone I'm blocking on this one...simply because I don't even see where my code can be wrong.
here's the code I'm doing my tests with
Code: Select all
if foo then
script.on_init(function()
game.player.disable_all_custom_prototypes()
local recipelist = game.player.force.recipes
recipelist["iron-plate"].enable()
recipelist["iron-stick"].enable()
recipelist["iron-axe"].enable()
end)
end
(directly taken from
https://wiki.factorio.com/index.php?tit ... pt.on_init)
Any help would be welcomed, I'm just stuck there...the worst being that I'm sure the solution is simple but I just don't see it
Re: script.on_init
Posted: Sat Sep 10, 2016 7:22 pm
by Nexela
I see a few problems already
1. I am not sure if on_init, on_load, or on_configuration changed can be used from inside an if block (and I can't think of a reason why you would want too)
2. In a new world there is no player until after on_player_created event
3. game.player is a single player command
4. there is no disable_all_custom_prototypes() for player
5. recipe does not have .enable()
Code: Select all
script.on_init(function ()
global.enabled = global.enabled or false
if not global.enabled then
script.on_event(defines.events.on_player_created, function (event) -- since we only want to fire once, register this event in init then nil it when finished
local player = game.players[event.player_index]
player.force.disable_all_prototypes()
local recipelist = player.force.recipes
recipelist["iron-plate"].enabled = true
recipelist["iron-stick"].enabled = true
recipelist["iron-axe"].enabled = true
global.enabled=true
script.on_event(defines.events.on_player_created, nil)
end)
end
end)
untested code, but the theory is sound, should be mp compatible
Re: script.on_init
Posted: Sat Sep 10, 2016 7:59 pm
by Adil
Nexela wrote:
1. I am not sure if on_init, on_load, or on_configuration changed can be used from inside an if block (and I can't think of a reason why you would want too)
script.on_something() are just functions, they take a pointer to a function and don't know anything of where they are. All that matters is what have they registered to a particular event by the time of it happening. In case of on_init and on_load, this is indeed useless to put them deep in the blocks, since those events aren't risen after the session starts. (Except for debug testing.)
The original error is indeed weird. It's as if the command is executed not from the control.lua environment.
Re: script.on_init
Posted: Sat Sep 10, 2016 8:11 pm
by Qfa
I've just solved my problem.
Don't even ask me why, but I had a 'require("control")' in my data.lua. I didn't realized it was a mistake some times ago when I wrote it, and all went well because I only had basic stuff written in control.lua
Code: Select all
if foo then
data.raw["item"][itemName].subgroup = subgroup
data.raw["item"][itemName].order = order
end
Nothing using script or game events.
When I did some changes in the file, and added a 'script.on_init', factorio simply crash when trying to start. Which is now perfectly comprehensible.
I removed the line from data.lua, reorganized my code and everything's working smoothly
As usual, the problem was between the chair and the desk
Thank you for you help tho, I'll make sure to remember your advices
Re: script.on_init
Posted: Sat Sep 10, 2016 8:12 pm
by Adil
Now this code while seeming correct, really looks overly complex.
The main thing is that you don't really need the player in it at all. Unless something cardinally changes, the very first player will always spawn in the 'player' force anyway.
So apart from non-existant methods, the structure of the original code doesn't need much change:
Code: Select all
script.on_init(function ()
local force=game.forces['player']
force.disable_all_prototypes()
local recipelist = force.recipes
recipelist["iron-plate"].enabled = true
recipelist["iron-stick"].enabled = true
recipelist["iron-axe"].enabled = true
end)
Re: script.on_init
Posted: Sat Sep 10, 2016 10:00 pm
by aubergine18
Qfa wrote:As usual, the problem was between the chair and the desk
Don't worry, we've all had that PEBCAK moment
Glad you got it working!