Can I fix scripts in the saved file by migration?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
puzzletory
Manual Inserter
Manual Inserter
Posts: 3
Joined: Fri Jun 05, 2020 6:29 pm
Contact:

Can I fix scripts in the saved file by migration?

Post by puzzletory »

I'm developing a mod that contains only one scenario. All my code is in this scenario directories in several Lua scripts. I notice that if I fixed some issues in the scripts, the fix is used for a new game only. If I load the game from save that was created before the fix, the game still uses old code. I found this old code in the save file. I tried re-saved the game to another file, but It did not help. It is not a blocker for me now, but If I find some critical defect, I do not know how to fix it for players who have an unfinished game in save files. My scenario can take a long time. If my question is not clear, I can prepare an example mod that demonstrates this problem.

User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 883
Joined: Sun Mar 20, 2016 2:51 am
Contact:

Re: Can I fix scripts in the saved file by migration?

Post by Impatient »

As far as I understand your post, the piece of knowledge you are missing is, that if a scenario game is saved, all the scripts are saved with it. The savegame is independent from the scenario in the scenarios folder the game was started with.
So if you want to change the scenario scripts affecting your current game, just unpack the savegame - you will find all your scripts there, this game was started with. If you modfiy these script files, those modfications will only affect the specific game of that savegame. You can load the savegame after changing the scripts and they will immediately take effect in your game. It doesn't even need a factorio restart. :) Another savegame made after you loaded the first savegame with the modified scripts will contain those modified scripts. And so on.
The script files in the scenario folder being independent from the script files in each consecutive savegame also means, that you have to copy your updated scripts to the scenario folder, if you also want them to take effect in newly started games of that scenario.

In other words: Each savegame is its own scenario, because it has no outside script dependencies. Modfications to a savegame's scripts will immediately take effect when loading it.
In other other words: A scenario in the scenarios folder is nothing more than a savegame with a picture and an explanatory text slapped onto it.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Can I fix scripts in the saved file by migration?

Post by boskid »

It should be possible the same way as we did for 0.18.28 when we needed to update PvP and other scenario scripts due to style changes and script changes - simply provide migration in your mod that has "game.reload_script()" in it. As long player has your mod enabled and updated, scripts should get replaced during load. Look at data/base/migrations/2020-05-27_Factorio_0.18.28.lua from Factorio.

User avatar
puzzletory
Manual Inserter
Manual Inserter
Posts: 3
Joined: Fri Jun 05, 2020 6:29 pm
Contact:

Re: Can I fix scripts in the saved file by migration?

Post by puzzletory »

Thank you for both replies! I added a migration script with game.reload_script() to my mod, and now it works.

Post Reply

Return to “Modding help”