Using onload

Place to get help with not working mods / modding interface.
Post Reply
Holy-Fire
Fast Inserter
Fast Inserter
Posts: 169
Joined: Sun Apr 14, 2013 9:15 am
Contact:

Using onload

Post by Holy-Fire »

I'm writing a mod that, among other things, modifies some existing technologies and recipes, and adds a new item (unlocked by one of the existing technologies).

If I enable the mod and load a saved game created without the mod, the technologies and recipes remain with the original prototype, and the recipe for the new item is disabled despite the unlocking technology having been researched already.

If I write the following commands in the in-game console, everything is fine:

Code: Select all

game.player.force.resettechnologies()
game.player.force.resetrecipes()
if game.player.force.technologies["robotics"].researched then 
game.player.force.recipes["hyper-inserter"].enabled=true 
end
However, I want it to work without having to type commands in-game. So I added to the mod a file control.lua with the following:

Code: Select all

game.onload(function()
game.player.force.resettechnologies()
game.player.force.resetrecipes()
if game.player.force.technologies["robotics"].researched then 
game.player.force.recipes["hyper-inserter"].enabled=true 
end
end
)
I figured onload would be triggered when I load the saved game and the commands will be executed. However, this does not happen. Instead, the commands are only run a minute or so after the game was loaded. After that everything works, however, I also get the message "Can't save map, entity list of entities staged for update of links is not empty."

So, why is this not working? Is this a correct usage of "onload"? Is there a better way to achieve the same effect? And what is the meaning of this cryptic message?

Rahjital
Filter Inserter
Filter Inserter
Posts: 435
Joined: Thu May 29, 2014 10:44 am
Contact:

Re: Using onload

Post by Rahjital »

What you want to use is the oninit function. Oninit triggers whenever the mod is first loaded in the map, so when you load a map created without the mod, the game recognizes that the mod was added and runs its oninit function instead of onload.

The reason it happens after several minutes is because of the autosave function.

Holy-Fire
Fast Inserter
Fast Inserter
Posts: 169
Joined: Sun Apr 14, 2013 9:15 am
Contact:

Re: Using onload

Post by Holy-Fire »

Rahjital wrote:What you want to use is the oninit function. Oninit triggers whenever the mod is first loaded in the map, so when you load a map created without the mod, the game recognizes that the mod was added and runs its oninit function instead of onload.

The reason it happens after several minutes is because of the autosave function.
Ok, thanks!

Post Reply

Return to “Modding help”