Building the save/load system for our game

In order to integrate the save/load system, we need to create a static class that will take care of saving and loading for us. Here, we need to take an important decision: whether this class will retrieve the information that it requires to save (such as level, score, and so on), or whether we pass them as parameters and rely on other scripts to do that.

In our case, we have a simple game so we can choose either. As a rule of thumb, you want to decouple the save system as much as possible. However, you should still have a central script that manages to do the saving, such as the GameManager, which is responsible for retrieving all the data needed for saving. Thus, we will go in passing as parameters the data to our save/load script, even if the system we are going to build is not generic but specific to our game. In particular, we will create a struct within the save/load script, which will hold the data to save. As a result, it will be easier to pass data as well as return it back, and you will have a chance to learn something new.

Furthermore, since we want our save/load system to support different save slots, we can add in front of any key (a prefix) the name of the slot in which the player wants to save. If you let the player decide the slot name, then you should also include the name of the slot in the save data. Otherwise, if the player can choose between just a finite set of slots, such as three, then the GameManager can handle this information based on which slot the player has clicked on the UI. Of course, this part will be left as an exercise since this chapter will focus only on the save/load system. However, later in the chapter, some suggestions are given to enhance the gameplay of this last game. Lastly, some hits are provided on where to use this system.

Let’s start by creating a new C# Script and name it SaveLoadSystem, and you can remove the inheritance from MonoBehaviour and set is as static class since it will contain only static methods. First of all, we need to create the struct, so below the last }, we need to add the following struct:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class SaveLoadSystem {
//[...]
}

public struct SavingData {

public int level;
public float positionX, positionY;
public int score;
public float timeElapsed;
public string playerName;

}

A struct is not much different than a class in C#, and this struct doesn't have any methods, just the data fields we have seen in the previous table.

Although it's good practice to have a class and a struct per file, here, due to the very simple structure and the dependency with the main class of the file, we chose to keep it within the same file. 

Now, we can start writing our script. In particular, we need two functions: one to save and another one to load. Let's start with the first one.

The Save() function will receive a SavingData structure as a parameter, as well as the string denoting the save slot. The function goes through all the data in the structure, builds new keys based on the save slot string, and sets all the data within PlayerPrefs. At the end, it calls the Save() function to actually save all the data on the permanent memory (for example, the hard drive):

public static void Save(string slotKey, SavingData data) {
//Save into PlayerPrefs for each data item within the SavingData structure
PlayerPrefs.SetInt(slotKey + "_level", data.level);
PlayerPrefs.SetFloat(slotKey + "_positionX", data.positionX);
PlayerPrefs.SetFloat(slotKey + "_positionY", data.positionY);
PlayerPrefs.SetInt(slotKey + "_score", data.score);
PlayerPrefs.SetFloat(slotKey + "_time", data.timeElapsed);
PlayerPrefs.SetString(slotKey + "_playerName", data.playerName);

//Save into permanent memory
PlayerPrefs.Save();
}

On the other hand, we have the Load() function, which gives back a SavingData structure and takes as input the save slot key. As a result, the function is able to query the PlayerPrefs with the get-functions and store all the data within a newly created SavingData structure. At the end, it just returns the structure:

public static SavingData Load(string slotKey) {
//Create a new SavingData structure
SavingData data = new SavingData();

//Load from memory each item to fill up the data structure
data.level = PlayerPrefs.GetInt(slotKey + "_level");
data.positionX = PlayerPrefs.GetFloat(slotKey + "_positionX");
data.positionY = PlayerPrefs.GetFloat(slotKey + "_positionY");
data.score = PlayerPrefs.GetInt(slotKey + "_score");
data.timeElapsed = PlayerPrefs.GetFloat(slotKey + "_time");
data.playerName = PlayerPrefs.GetString(slotKey + "_playerName");

//return the data structure
return data;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset