Need help with error in game.oninit

Place to get help with not working mods / modding interface.
Post Reply
User avatar
IngoKnieto
Fast Inserter
Fast Inserter
Posts: 106
Joined: Mon Oct 03, 2016 9:29 am
Contact:

Need help with error in game.oninit

Post by IngoKnieto »

Hi everybody,

I am quite new to LUA and I am having trouble with a simple (?) starting script. So far I have successfully created a mod which adds a few new items and recipes and also modifies existing ones. The existing recipes I modified work in a new game, but not in a loaded game, and the Modding FAQ tells me, to add two lines to my control.lua (see: https://wiki.factorio.com/index.php?title=Modding_FAQ) to fix this.

So this is my control.lua now, but when I load or start a game now, it throws the error: __FactorioVanillaExtended__/control.lua:4: unexpected symbol near ')'

Code: Select all

game.oninit(function()
for i, player in ipairs(game.players) do player.force.reset_recipes()
for i, player in ipairs(game.players) do player.force.reset_technologies()
end)
I don't see any syntax error here, but as I said I just started working with LUA. Can anyone more experienced please help me?
Thanks in advance...

Edit: Forgot to mention: I am testing on version 0.14.9
Last edited by IngoKnieto on Mon Oct 03, 2016 10:21 am, edited 1 time in total.

User avatar
IngoKnieto
Fast Inserter
Fast Inserter
Posts: 106
Joined: Mon Oct 03, 2016 9:29 am
Contact:

Re: Need help with error in game.oninit

Post by IngoKnieto »

Nevermind, I found it, somehow the END to the for-loops got deleted when I copied the two comands :oops:

However now the script is now throwing the error: attempt to index global 'game' (a nil value)
Is this related to the changes in version 0.12.11? But script.oninit doesn't work either. I am a little stuck here, if anyone could point me in the right direction how to reset modified recipes in save games I'll appreciate it...

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Need help with error in game.oninit

Post by Nexela »

A couple of issues

#1 the Wiki is horribly out of date

#2 game.oninit should be script.on_init

#3 need to use pairs and not ipairs when iterating through most of the factorio objects

#4 on_init is only ever run once. When the mod is first installed
#4b Keep in mind: in a brand new game there will not be any players in game.players at this point.

#5 "I think meaning I heard it on the forum" if you upgrade your mods version number (or add/remove/upgrade any mods or factorio) reset_recipes/technologies is automatically run but I haven't tested this. Won't error but could be redundant

Code: Select all

script.on_int(function() 

for _, force in pairs(game.forces) do
force.reset_recipes()
force.reset_tehnologies()
end

end)
http://lua-api.factorio.com/

User avatar
IngoKnieto
Fast Inserter
Fast Inserter
Posts: 106
Joined: Mon Oct 03, 2016 9:29 am
Contact:

Re: Need help with error in game.oninit

Post by IngoKnieto »

Your code example works when I run it in-game from the console. That definitely helped me a lot for testing my mod, thank you!

However you are right, on_init is not called if I load a game where the mod was already enabled. I was going through the LUA API, but it seems like there is no "on_load" event, and for example on_player_created is also just called when you start a new game...

What is the best practice, if you want to run some commands, when a game is loaded?

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Need help with error in game.oninit

Post by Adil »

Quick test shows that on_init is run the first time the map is loaded with your mod. If that is the loading of the savegame, then for that time the on_load event is ignored.
The following code was put in mod control.lua in order to test this:

Code: Select all

script.on_init(function() print('\n\noninit\n\n')  end)
script.on_load(function() print('\n\nonload\n\n')  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
IngoKnieto
Fast Inserter
Fast Inserter
Posts: 106
Joined: Mon Oct 03, 2016 9:29 am
Contact:

Re: Need help with error in game.oninit

Post by IngoKnieto »

Thank you for the replies, I found the on_load function now.

However I cannot use it directly, because game.forces doesn't exist at that point. I ended up with a workaround using the on_tick event, see this post: viewtopic.php?f=25&t=32882&p=212737#p212737

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Need help with error in game.oninit

Post by Adil »

Why exactly do you need the forces during the onload? You should define everything needed about them during the `on_init`, and then add some handlers for `on_force_created`\`on_forces_merging`.
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
IngoKnieto
Fast Inserter
Fast Inserter
Posts: 106
Joined: Mon Oct 03, 2016 9:29 am
Contact:

Re: Need help with error in game.oninit

Post by IngoKnieto »

I was just looking for an easy way, to see the effect of changed recipes in an already built up factory. And just during the test phase of a mod.
For example if I double the iron cost of steel, I want to see how my usual production chain behaves. Maybe there's an easier way to do those kind of balance testings, I don't know, I am quite new to modding. And I haven't worked with on_force_created`\`on_forces_merging yet, I'm gonna check what they do 8-)

Post Reply

Return to “Modding help”