[solved] oninit not working properly maybe?

Place to get help with not working mods / modding interface.
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

[solved] oninit not working properly maybe?

Post by FreeER »

Making a cloning mod and initially I had an issue with trying to index a table (easy_death) which crashed Factorio when trying to start a new game, and yet (based on everything I know) it should have worked perfectly fine. I have 'fixed' this by moving the code into oninit, where it should be anyway, but I still have no idea why I was getting an error with it.

here is the relevant code:

Code: Select all

if glob.easy_death.print then
  return 
elseif game.difficulty == defines.difficulty.easy then glob.easy_death.print = true
else glob.easy_death.print = false
end

if glob.easy_death.remains then
  return
elseif game.difficulty == defines.difficulty.easy or game.difficulty == defines.difficulty.normal then glob.easy_death.remains = true
else glob.easy_death.remains = false
end
In the mod below this code is within oninit, when I originally wrote this (and had issues) it was right underneath oninit. It should have worked either way (though outside it gets run multiple times, it should NOT have crashed factorio)

fixed and working mod (though incomplete):
clone.zip
(62.94 KiB) Downloaded 144 times
edit: first this has been explained as caused by the calling order of Factorio script loading (Thanks slpwnd).
1) the global scope is executed
2) oninit method is called

second, I was tired when I posted (it was 3am and I'd been up for a long time) and I mistakenly wrote that it crashed Factorio. It actually just caused an attempt to index nil (easy_death), though I did experience several crashes when I was rewriting the code to allow for it to use the difficulty setting so that is probably why lol.
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net
slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: oninit not working properly maybe?

Post by slpwnd »

As discussed on the IRC, the order of execution:
1) The global scope
2) Any function hooks (i.e. the oninit, onload)
Post Reply

Return to “Modding help”