Page 1 of 1

Using onload

Posted: Sun Jun 15, 2014 6:17 pm
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?

Re: Using onload

Posted: Sun Jun 15, 2014 7:10 pm
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.

Re: Using onload

Posted: Sun Jun 15, 2014 7:38 pm
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!