Save Game – Introduction

Save Game is a critical part of most games, but not all, for example, a game like Diep.io does not need a data persistent, they only need data persistent for storing achievements and such.
But in most cases, Save system is a critical part in all games, like Leveled games, such as Candy Crush, or bigger PC games.

So, now how the save and load works? Simply, the runtime data serialized and formatted to a data that can be stored as a file or data on database.
In most cases, the games are using Binary format for serialization, and sometimes they use JSON or XML for data interchange between game and user plus other services too.

In Unity, you can use BinaryFormatter for data serialization in binary format, but there are some issues with, the BinaryFormatter does not supports versioning that means, if you serialize a class with 3 fields, and then add a new field and then try to deserialize it, the serialization will fail, But you can use XML or JSON to solve this issue or you can make your Binary Serializer to handle versioning, by using BinaryWriter and BinaryReader classes.

And in case of cross platform compatibility, the serialization needs some adjustments, for example, some platforms does not support file storage and access, in this case you need to use the provided API from the platform for data persistent, Unity WebGL platform does not supports File storage and access, so you need to use a Web Storage API like IndexedDB, Session storage or cache or something else that is provided by the Web Browser.

But there is other way that works on all platforms, but there is a limitation, and that is the internet connection, you can save the user data to your own cloud storage and sync it across user devices, but the user must be connected to internet to do so. Now it is the best choice for cross platform games to use Cloud saving and loading.

But other games like Call of Duty and Battlefield, don’t need to do so, they are storing the data on the user device as a file and there is no need for data synchronization across devices.
And same for some mobile games, but almost all mobile games are using cloud storage, because there are many benefits to do so, like data synchronization, access to user data and the ability to modify it as need, and cross platform compatibility.

Google provides Google Play Games Services for Android games to handle most of the stuff for games, like Save games, leaderboards and achievements, but there is a limit of 3 MB size for save games, and it is good for small games, but it is too low for bigger games.
In the case of data limitation, you need to setup a cloud storage for your own and use that for data persistent.

Save Game in Unity

Saving and Loading in Unity is easy, you just need to know the storage and have a serialization library like BinaryFormatter or JSON.Net or you can make your own.
Some cases for storing data are:

  • The levels progress (completed, unlocked, …)
  • The players state (position, health, inventory, …)

And now, for handling these stuff, you need a Data model for each of them, like we have a Level data model:

Now we have a collection of our levels:

Here is how we are going to save the levels:

The levels collection is now serialized as binary to the savedGame.dat file at current working directory.

A Save System can handle the above instructions by a simple API calls like Save and Load methods.
There are some ready to use Save systems for Unity and each one has it’s own cons and pros, but if you don’t like to use any pre-made Save system you can make one yourself for your need.
Anyway, having a look at them is not a bad idea, maybe they fit your needs:

Thanks.

Hasan Bayat

Awesome Developer.

Leave a Comment

Your email address will not be published. Required fields are marked *