I need a "simple" save system for my game

(I don’t know what tags to use, so I just put what I thought would fit. And also the Unity version that I’m using is 2018.3)

Some important information before we start, I’m very new to Unity, but I’m already learning a bit. I’m currently using 2018.3.something, but I’ll be updating the project soon to Unity 2023 or 2024 version (because I plan to make my game for Windows AND Android).

I won’t go into too much detail, but I would just like a “simple” save system that could save, load, and delete saves. (preferably not JSON, but I might reconsider)

In my game, the protagonist (player) can interact with NPCs and make friends, so I would like to be able to save who he has already made friends with, if the protagonist already knows all the information about that NPC, etc. Oh, and also if any (and how many) NPCs saw the protagonist doing something suspicious or if they saw the player killing someone (it’s a game that contains a bit of violence) and also save whoever is dead, of course.

I already made an inventory system, but I would like to implement it in this save system, so I can save the collected items.

I also need a day and week system on the save (I already implemented day and night through player prefs, but I was thinking about changing that too)

If anyone could send me a link to a tutorial or documentation that could help me, I would be very happy.

BinaryWriter

First of all, save systems are never really simple, sorry haha. But if you want simplest, then json would be that. The JsonUtility class that comes with Unity can serialize custom serializable classes that you use to store data that needs to be persistent, and then you can save the serialized json string to a file. When loading the same save file, you can deserialize it back using JsonUtility again.

Thanks, I’ll take a look soon.

Load/Save steps:

An excellent discussion of loading/saving in Unity3D by Xarbrough:

And another excellent set of notes and considerations by karliss_coldwild:

Loading/Saving ScriptableObjects by a proxy identifier such as name:

When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. Save data needs to be all entirely plain C# data with no Unity objects in it.

The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object, so you end up with a defective “dead” Unity object.

Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.

If you want to use PlayerPrefs to save your game, it’s always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

A good summary / survey of the problem space:

Just FYI every save system needs versioning. If you make updates, and change the save format (new values, values removed, values renamed or type changed) then you need to know that you’re loading version 3 and need to properly handle the absent, renamed, type-changed values and ignore the removed ones.

Also save/load should be considered from the start of the project, it’s not something that you can add in later if you want it to be “easy” because adding a save system to an existing game is often quite challenging.

Two rules can make saving much simpler:

  • not in combat
  • checkpoints

This eliminates all kinds of extraneous things you might need to save otherwise.

Json is probably the easiest. If you don’t want to use json you could also look at MemoryPack by Cysharp (they also made UniTask). This has features like versioning. Unity also has a general purpose serialization library but I’ve never used it. It looks like it can currently save to json or binary (only in unsafe mode). There’s also Protocol Buffers by google, but I’m not sure how much support they have on Unity. If your game has a lot of flag values then it might be worth storing them in a BitArray.