Unity Save/Load System with slots

zexusssss

New Member
Jun 9, 2020
8
2
Hello !

I'm creating a 3D game and currently Im stuck in the Save/load part I really dont know how to start cause im still new in game dev. , But im looking for a help if anyone knows a good and easy way to create a functioning Menu that when i hit save button it throws all the informations to a desired Save Slot
thanks ! <3
 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
998
It depends on your system, or rather your game engine. If your using simpler engines that doo everything, such as renpy or rpgmaker, you need not do anything, the engine does it for you. But if you want a more technical answer, then I'll assume you're justing coding things raw.

Say for example, unity.
In unity, to have data move across scene to scene, you need to make an object class that is a 'singleton,' something that can only exist one at a time, so once it is made, it move from scene to scene, holding all the data. This is able to track things like, if you want to have a character inventory, it always remember what it has as different scenes are loaded. This is just because unity has a scene to scene mentality, and I find it annoying. But what one of these singletons can be is your save data. an object that keeps track of things to remember as the player moves scene to scene.

One of the things you want to do is also make this a [Serializable] class. What you can do is open a data stream (aka create a file of any kind, such as a "Save01.mygamesave" file type), and what you can do is you have your save data as a object class of some type, with a pointer to it you can serialize it. Serializing is when you take a 'object class' and convert it from running code to straight up just a picture of the object and all of its data at any given moment, a picture of pure binary data. What you can then do is do a binary write to the data stream (to the file), and that file (whatever name you give it) is litterally just a picture of your save data object at any possible state.

to load a save, you would have open a data stream again, pick the file you want, read it as a binary file, and then convert the binary into working code by deserializing back into the save data type class object again, which the code can then figure out how to set the game based on the state in the save data class.

but that may sound complicated depending on if your dealing with raw code or not, more information about your situation would help.
 

zexusssss

New Member
Jun 9, 2020
8
2
Thank you very much for your help bro , i've heared about fungus of unity and Ive found a script thats in it that takes care of saving and loading but im pretty much new to it so i can't really understand it and how to implement it in my game
Here are the links if you're interested

 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
998
So it does come with an example scene, a basic save menu. What I tried to do is find the save button, see what action is called when you click save, and see what files are used, and inside of those find the called methods, check to see what they do. They used an object variable named unwrittenSaves which is a list of the type GameSaveData, which is just an abstraction layer for other SaveDatatItem object, which just contains a discription and the data to be save as a string.

Now I would try to break it down into how to use it, and try to make it easy to understand... but for over 10 years now the unity namespace system has been broken, each version of unity handles it slightly differently, and it appears that my version of unity is basically brain dead and it can't detect where any of the files are, so none of my files are locating eachother so it's going to take a while before i can get things up and running to see what is going on.
 

fakehb

Member
Jun 30, 2020
119
146
In unity, to have data move across scene to scene, you need to make an object class that is a 'singleton,' something that can only exist one at a time, so once it is made, it move from scene to scene, holding all the data.
This is a bit light on details.

-You can use static classes and (non-MonoBehaviour) singletons to store global data. I'd only use a singleton over a static class if you have a reason to (eg. polymorphism).

-Never, ever, ever, use the singleton pattern with MonoBehaviours. It becomes an anti-pattern because you can't call the constructor. You're relying on something like Awake(), which has all sorts of ways it can fail (eg. script execution order, not being active in hierarchy), and nothing stops you from adding multiple copies to a scene.

-DontDestroyOnLoad() will persist a MonoBehaviour between scenes. If you need to persist MonoBehaviours between scenes, you should probably use additive scene loading though (there are a whole host of reasons you should be making liberal use of additive scene loading).

One of the things you want to do is also make this a [Serializable] class.
Do this if you need to assign classes or structs as values in the inspector (or use a ScriptableObject), but don't use this to save files if you can at all avoid it. Big temptation because it's quick to implement, but it's also slow and totally inflexible. Change any of the members and you've just broken save compatibility.

Nothing is stopping you from serializing data yourself (eg. creating ToString() and FromString() methods on classes that store data). Also check out , it's well worth the money.