Page 1 of 1

[solved] oninit not working properly maybe?

Posted: Sun Oct 13, 2013 7:53 am
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 161 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.

Re: oninit not working properly maybe?

Posted: Sun Oct 13, 2013 10:21 pm
by slpwnd
As discussed on the IRC, the order of execution:
1) The global scope
2) Any function hooks (i.e. the oninit, onload)