Incorrect Saving when Scene Changes

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
Jason
Posts: 7
Joined: Wed Apr 16, 2014 6:17 am

Incorrect Saving when Scene Changes

Post by Jason »

Hi,

I'm having a strange issue and I'm not entirely sure if it's on my end or something related to Easy Save. I figured I'd ask to see if there's anything I may be failing to consider.

I have a GameObject that is meant to carry some data between scenes, and hence uses DontDestroyOnLoad(). It has a dictionary that is meant to store the worlds the player has created. It pairs a string, the world name, to a boolean, which marks whether the world is one of two types.

On the main menu, the player can create a world. Doing so will add it to this dictionary. Now, on quitting the game (using OnApplicationQuit()) I save this dictionary using a simple ES2.Save() call, and in the Start() function I load this data back using ES2.Load().

If I create a world on the main menu, and then quit the game on the main menu, this world is saved to file correctly in the dictionary. When I load it back up, it works fine.

The strange thing is if I make the world, then actually load the game scene (note that this class with the dictionary carries over because of DontDestroyOnLoad), it runs the save code just fine on quitting. However, it seems to not save the dictionary data correctly. When I load it back up, my world can't be found. It's not saving it to file at all or is saving it wrongly.

I have checked to ensure that the proper data is still in the dictionary before saving it in the new scene, and it is. The problem is happening on the ES2.Save() call.

So I'm not sure if other code I've written could interfere with this (I do have other parts of the code saving in the game scene, including use of ES2Writer). Without having all my code, it would probably be hard to tell. But I'm only wondering if there's something you can think of related to DontDestroyOnLoad or something else that may cause saving to not work properly, since I can't find a problem on my end.

Thanks in advance!
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: Incorrect Saving when Scene Changes

Post by Joel »

Hi Jason,

I would make sure that none of your other code is interfering with the file, especially if you're mixing ES2Writer code with ES2.Save code. The easiest way to do this would be to save the Dictionary to it's own file and see if that fixes the problem.

I've also heard of some people encountering a bug where Start() gets called again when you change the level with scripts on GameObjects marked as DontDestroyOnLoad. To check that this isn't happening, put a Debug.Log("RAN"); line before your ES2.Load code. If "RAN" gets logged to console when you change the level, it means Start() is getting called again and it could be loading old data. The way around this would be to Save the Dictionary whenever you change the level too.

If this doesn't fix your problem, send me your Start() and OnApplicationQuit() methods with your Save and Load code in, and I'll see if I can see anything odd.

All the best,
Joel
Jason
Posts: 7
Joined: Wed Apr 16, 2014 6:17 am

Re: Incorrect Saving when Scene Changes

Post by Jason »

Turns out that bug was the problem, where Start was being called again on the loading of the new scene. It's working now.

Thanks!
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: Incorrect Saving when Scene Changes

Post by Joel »

Great to hear! If you run into any other problems, let me know.
- Joel
Locked