Persisting data between scenes: can't find an answer

I’ve read all the solutions for data persistence, and I understand them. The problem is I don’t know which one is the best for my game.

I have a RPG with party members, I need to keep the party members between scenes: to access their inventory, their stats, to spawn them in battles, etc. Sounds simple. I started implementing dontdestroy with a singleton but I find it very cumbersome: so I thought, why not write all this to JSON files and read from them when needed?

Ppl say this will be slow, because you’ll have to read from files every time you open the inventory, stats, go in battle, etc. On the other hand, this just saves headaches. So will this be too slow, or do you have a better solution?

2 Answers

2

Hi!

The usual mechanism is to have a gameobject (or objects) store the data and set that object to DontDestroyOnLoad.

Or you can use statics too, but then you need to be careful to clear the statics if the player loads. The advantage of DontDestroyOnLoad is that you can still destroy it manually if the player does a quickload or whatever. You can destroy, recreate from save, load the scene, and done!

Hope this helps

I don't think so. There are quite some Types out there which seem quite ambigous i guess. Just go with whatever feels like it is correct and works. If you have performance concerns then i can suggest to actually do a benchmark yourself. I can also recommend to check out the source code as Unity provides this so you can see what's going on below the deck. As you can see [here][1] the GenericPool class, which inherits the ObjectPool class, is internally also just based on a Stack. [1]: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/ObjectPool/ObjectPools.cs

You can also just save the data before the scene ends then, once the next scene loads, read and re-store the data on that scene. You’ll only need to read and write to the file once in each scene transitions and it won’t be too cumbersome on the game play itself!
Hope this helps

Yeah that’s what I thought too, ok, thanks. :)