Save data duplication

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
timbecile
Posts: 110
Joined: Mon Mar 12, 2018 11:00 pm

Save data duplication

Post by timbecile »

Hey Tony,

I've got another issue I've been getting regarding scene changes:

I'm using the save system to save the items in my player's (and other's) inventory, and am getting an item duplication bug when I change scenes. (even if I don't have 'save across scene changes' checked).

I've tried deleting the player's inventory during the applydata method, but I end up losing stuff. Is there another approach I should be using? Like could I check in the applydata method to see if this is coming from an actual save game rather than coming from a scene change?
User avatar
Tony Li
Posts: 21070
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save data duplication

Post by Tony Li »

Hi,

First check that your RecordData method is accurately recording the inventory.

If it is, then in ApplyData you should be able to delete the contents of the inventory and repopulate it from the save data.

Is your inventory saver on a GameObject that survives scene changes? If so, you can override OnBeforeSceneChange to know that it's about to change scenes:

Code: Select all

private bool isChangingScenes;

public override void OnBeforeSceneChange()
{
    isChangingScenes = true;
}

public override void ApplyData(string s)
{
    if (isChangingScenes) { ... } // Something special for scene changes.
    etc...
}
However, if you're recording the inventory accurately in RecordData and then recreating it accurately in ApplyData, I don't think you should need to know whether ApplyData is being called by a scene change or loading a saved game.
timbecile
Posts: 110
Joined: Mon Mar 12, 2018 11:00 pm

Re: Save data duplication

Post by timbecile »

So is ApplySaveData() called during every scene load?
User avatar
Tony Li
Posts: 21070
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save data duplication

Post by Tony Li »

Yes. When you change scenes using SaveSystem.LoadScene() -- or any techniques that use SaveSystem.LoadScene(), such as a ScenePortal or SaveSystemMethods component, or LoadLevel() sequencer command -- it does this process:
  • Tells all savers in the original scene that the scene is about to be unloaded.
  • Tells all savers in the original scene to record their data in the save system's memory. (Calls their RecordData() methods.)
  • Changes the scene, using a Scene Transition Manager component if it exists (e.g., fade out/in, intermediate loading screen, etc).
  • Waits zero or more frames to allow components in the new scene to initialize themselves. (Set this in the Save System component's Frames To Wait Before Apply Data field.)
  • Tells all savers in the new scene to retrieve their states from the save system's memory. (Calls their ApplyData() methods.)
timbecile
Posts: 110
Joined: Mon Mar 12, 2018 11:00 pm

Re: Save data duplication

Post by timbecile »

I'm thinking that the best way to resolve my issues would be to have two types of saves/Load. So there's the saves that the player triggers which would save everything, and then the save/load that triggers during scene transitions which would only save level specific details (monster HPs, treasure contents and quest/dialog status).

What would be the best way to accomplish that? I've tried checking and unchecking save 'save across scene transitions' but the player data (which I don't need or want to save across scene transitions) still get saved.
User avatar
Tony Li
Posts: 21070
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save data duplication

Post by Tony Li »

You'll have to use that OnBeforeSceneChange() method I mentioned above.

The 'Save Across Scene Transitions' checkbox still does the save, but then after it enters the new scene it dumps any data for savers that have 'Save Across Scene Transitions' ticked and aren't in the new scene.
timbecile
Posts: 110
Joined: Mon Mar 12, 2018 11:00 pm

Re: Save data duplication

Post by timbecile »

ok. So just so I have my head wrapped around this:

During a Savegame load, the loader loads the scene that's listed in m_savedGameData.sceneName

Once the scene is loaded it calls ApplySavedGameData() on PlayerSaver (if you're using it) which places the player at either a spawnpoint or saved position

and ApplySavedGameData() also applies the data to all the other savers in the scene

Does that sound correct?
User avatar
Tony Li
Posts: 21070
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save data duplication

Post by Tony Li »

Yes, except you may be referring to PositionSaver, not PlayerSaver. When loading a saved game, the PositionSaver moves the player to the position it was in when the game was saved.

If the player has a PositionSaver component with 'Use Player Spawnpoint' ticked, it will use the specified spawnpoint only when changing scenes (and if a spawnpoint has been specified when changing scenes), not when loading a saved game.
timbecile
Posts: 110
Joined: Mon Mar 12, 2018 11:00 pm

Re: Save data duplication

Post by timbecile »

Thanks for all the help here Tony. I was able to dig into this to fix both my addressables issue and my double spawning issues.

For the double spawning issue, I ended up using the ApplyData() method to determine whether the treasure/shop inventory should be spawned or not, and that made all the difference.
User avatar
Tony Li
Posts: 21070
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save data duplication

Post by Tony Li »

Sounds good! Glad to help.
Post Reply