How to run code on exit save

Place to get help with not working mods / modding interface.
ProfoundDisputes
Inserter
Inserter
Posts: 22
Joined: Mon Jun 27, 2016 4:20 pm
Contact:

How to run code on exit save

Post by ProfoundDisputes »

Is there a way to run code to change the player's data before quitting the game? I would like to access the game.players[1].get_inventory(defines.inventory.player_armor) and edit it before quitting.
Bilka
Factorio Staff
Factorio Staff
Posts: 3310
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: How to run code on exit save

Post by Bilka »

No, because that would not be deterministic.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5304
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: How to run code on exit save

Post by Klonan »

In Multiplayer there is:

Code: Select all

on_pre_player_left_game
https://lua-api.factorio.com/latest/eve ... _left_game

However this does not work in single player
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: How to run code on exit save

Post by darkfrei »

Klonan wrote: Sat Dec 29, 2018 12:36 pm In Multiplayer there is:

Code: Select all

on_pre_player_left_game
https://lua-api.factorio.com/latest/eve ... _left_game

However this does not work in single player
Hey, we have solution for multiplayer, but here is no solution for single player while it is not detereministic and makes desync?
posila
Factorio Staff
Factorio Staff
Posts: 5408
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: How to run code on exit save

Post by posila »

I mean ... what is the point of changing inventory before the player quits singleplayer since the game is literally just about to wipe out the entire game state?
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: How to run code on exit save

Post by darkfrei »

posila wrote: Sat Dec 29, 2018 7:02 pm I mean ... what is the point of changing inventory before the player quits singleplayer since the game is literally just about to wipe out the entire game state?
It's nice to get greetings on start or statistics about game session on the game ending today.
ProfoundDisputes
Inserter
Inserter
Posts: 22
Joined: Mon Jun 27, 2016 4:20 pm
Contact:

Re: How to run code on exit save

Post by ProfoundDisputes »

posila wrote: Sat Dec 29, 2018 7:02 pm I mean ... what is the point of changing inventory before the player quits singleplayer since the game is literally just about to wipe out the entire game state?
I was going to do some clean up so that if the player removes the mod it doesn't cause equipment in armor to disappear. My mod mostly changes the equipment in armor dynamically with custom equipement with small modifications. So if the player exited the game I would just swap out the custom equipment with the vanilla equipment. When they load the game swap them back in. That way they won't have missing equipment in armor.
posila
Factorio Staff
Factorio Staff
Posts: 5408
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: How to run code on exit save

Post by posila »

So you'd need to run script before saving starts and after saving finishes + after load. That's not possible in the current API.
ProfoundDisputes
Inserter
Inserter
Posts: 22
Joined: Mon Jun 27, 2016 4:20 pm
Contact:

Re: How to run code on exit save

Post by ProfoundDisputes »

posila wrote: Sat Dec 29, 2018 10:27 pm So you'd need to run script before saving starts and after saving finishes + after load. That's not possible in the current API.
*facepalm* Oops. Ya that would be what I need to do. New to modding games here... It would be what you state here.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: How to run code on exit save

Post by darkfrei »

ProfoundDisputes wrote: Sat Dec 29, 2018 10:03 pm I was going to do some clean up so that if the player removes the mod it doesn't cause equipment in armor to disappear. My mod mostly changes the equipment in armor dynamically with custom equipement with small modifications. So if the player exited the game I would just swap out the custom equipment with the vanilla equipment. When they load the game swap them back in. That way they won't have missing equipment in armor.
Now we have migration script that will be running one when the mod is installed. Here was another suggestion - make uninstall migration script, where you write what must be changed after mod was uninstalled.
Post Reply

Return to “Modding help”