Best way to store and save dynamic and static data

Hi, I’m in a bit of a conundrum trying to figure out the best way to solve this problem. Trying to save myself trouble later on doing the right thing from the start. I will do my best to break down my problem into multiple smaller questions.

Question 1:
Simply put I want to store dynamic data which will be changed multiple times during runtime of the game and static data that won’t change at all.
My initial idea how to solve this was to store all data in a scriptable object (SO) but after learning they are not good at storing dynamic data and have problem being saved between sessions I had to scrap this idea.
Second idea was to store all variables in a good old fashioned C# class but is this the best way to solve this problem?

Question 2:
Now with this collection of data, lets call it a class, i want to be able to save X amount of that class as unique instances. As a simple example lets say its a car class and the player can have X amount of cars. They need to be unique because the cars might be of different brands, models and colour. In this example the cars would also have a corresponding 3D model depending on what car it is. Which would also be true in my case.
On top of that the cars needs to be saved (serialize) so if the player quits the game and continues later he dosen’t lose all of his cars, which isn’t fun.
My first thought was to save it in a List or a XML/JSON file but once again I don’t know which solution is the best.

Please ask if anything is unclear.

Thanks.

You need persistent data, which means simply saving something in a C# class or a list isn’t good enough. And a ScriptableObject is good for defining data when you compile that can be accessed at runtime, but can’t save persistent data.

Your thought to use XML or JSON is on the right track, and between these, JSON is the clear preferred standard. There are many ways to use JSON (I personally like Json.NET for this, which is available in convenient Unity Package form.) This package will allow you to create a data-holding class in C#, add [System.Serializable] to this class, and serialize it to a file with one line of code, and deserialize it with another when you wish to load it.

3 Likes

Hi, thanks for the answer!

I installed Json.NET and it works great. I managed to save some data to a json file, following the documentation, without any issues. Guess it wasn’t actually as hard as I thought.

However this works great with saving simple stuff like strings or integers but what is the easiest way to save more complex things like 3D models? Following my earlier example the car class would have strings like car brand, model and description but depending on what type of car it is, it would have a different 3D model attached to it. How would you store this in a json file? Is the easiest way to simply have a integer reference or is there another easier way?

If you need to download and save an actual 3D model, then that’s a whole other ball of wax and we can get into it if you want, but it doesn’t sound like you need to do that. If you have the 3D model in your game and just need to reference which one, then an integer reference to an array, or a string reference to the model’s name to lookup via a dictionary would be best.

3 Likes