script.on_init

Place to get help with not working mods / modding interface.
Qfa
Burner Inserter
Burner Inserter
Posts: 15
Joined: Tue May 31, 2016 7:29 pm
Contact:

script.on_init

Post 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
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: script.on_init

Post 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
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: script.on_init

Post 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.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
Qfa
Burner Inserter
Burner Inserter
Posts: 15
Joined: Tue May 31, 2016 7:29 pm
Contact:

Re: script.on_init

Post 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
Last edited by Qfa on Sat Sep 10, 2016 8:13 pm, edited 1 time in total.
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: script.on_init

Post by Adil »

Now this code while seeming correct, really looks overly complex.
Nexela wrote:
snip
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)
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: script.on_init

Post 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!
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Post Reply

Return to “Modding help”