Page 1 of 1

SaveSystem: ApplyDataImmediate called even when not loading a save game?

Posted: Fri Feb 04, 2022 2:28 pm
by NotVeryProfessional
I have noticed that ApplyDataImmediate() is called even when I am using the SaveSystem to load a new scene, but not load a saved game.

Is that intended behaviour?

How to check if I am actually loading a savegame or not?

Re: SaveSystem: ApplyDataImmediate called even when not loading a save game?

Posted: Fri Feb 04, 2022 3:12 pm
by Tony Li
Yes, that's intended behavior, but I'm open to feedback on it.

Here's the rationale: You load a new scene either by changing scenes (SaveSystem.LoadScene) or loading a saved game with 'Save Current Scene' ticked. In either of these cases, there may be some data that you want to restore immediately as soon as the scene loaded. However, in general, you want to wait 0 or more frames (the 'Frames To Wait Before Apply Data' value) to allow other scripts in the newly-loaded scene to initialize themselves before accepting save data.

If you're loading a saved game without 'Save Current Scene' ticked, you haven't loaded a new scene, so you don't need to wait. ApplyData() is called right away.

Re: SaveSystem: ApplyDataImmediate called even when not loading a save game?

Posted: Tue Feb 08, 2022 9:08 am
by NotVeryProfessional
I'm struggling with this a bit.

As I see it, using SaveSystem.LoadScene() will ALWAYS apply save data, correct? I should be using ResetGame() if I want to ensure that no save game data is loaded. That's how I understand what I see in the code.

I'm asking because I may be abusing the save system a bit. In my setup, there is exactly one scene per game - it's an RTS-like game. I auto-save to a fixed savegame slot, one per level. Players can continue a previous game or start a new one, but they can't have multiple save games for the same level.

Re: SaveSystem: ApplyDataImmediate called even when not loading a save game?

Posted: Tue Feb 08, 2022 9:30 am
by Tony Li
Hi,

That's correct. SaveSystem.LoadScene() does this:
  • Tell savers in the outgoing scene to record their states into memory (SaveSystem.currentSavedGameData object).
  • Change scenes.
  • Tell savers in the newly-loaded scene to retrieve their states from SaveSystem.currentSavedGameData if data exists.
RestartGame() will wipe SaveSystem.currentSavedGameData first:
  • Clear SaveSystem.currentSavedGameData.
  • Change scenes.
  • Tell savers in the newly-loaded scene to retrieve their states from SaveSystem.currentSavedGameData if data exists (but there is none).
Note that the last step of RestartGame() above will still call ApplyDataImmediate() and ApplyData(), but those methods will generally do nothing since there's no save data for them.

Re: SaveSystem: ApplyDataImmediate called even when not loading a save game?

Posted: Tue Feb 08, 2022 11:10 am
by NotVeryProfessional
Got it.

The part I don't get is how it gets currentSavedGameData.

I have one bug where I start a new game, but it loads save game data - and from a different scene. This is probably because I'm messing around with the SaveSystem, but I'm trying to understand what I'm doing wrong.

Re: SaveSystem: ApplyDataImmediate called even when not loading a save game?

Posted: Tue Feb 08, 2022 12:05 pm
by Tony Li
SaveSystem.currentSavedGameData will stick around in memory until you clear it using SaveSystem.ResetGameState() or SaveSystem.RestartGame(), or if you overwrite it by loading a saved game using SaveSystem.LoadFromSlot().

If you're starting a new game, make sure SaveSystem.ResetGameState() or RestartGame() is getting called.

Re: SaveSystem: ApplyDataImmediate called even when not loading a save game?

Posted: Wed Feb 09, 2022 2:58 am
by NotVeryProfessional
Tony Li wrote: Tue Feb 08, 2022 12:05 pm SaveSystem.currentSavedGameData will stick around in memory
THAT is the part I totally didn't expect and caught me by surprise. Thanks, that explains everything.

If it wasn't my fault for missing it in the documentation, I think it should be marked there as an important info.

Re: SaveSystem: ApplyDataImmediate called even when not loading a save game?

Posted: Wed Feb 09, 2022 10:06 am
by Tony Li
Got it. I'll make that clearer in the documentation.