Makinom allows you to add custom save data to the save game system.
Add custom save data (e.g. 3rd party product data) to Makinom save games by registering a class (e.g. a component) to the save game system. The class must implement the ISaveData interface.
The ISaveData interface #
This interface has two functions that need to be implemented into your class:
public virtual DataObject SaveGame()
This function is called when the game is saved. It returns a DataObject, which holds all the data that you want to save.
public virtual void LoadGame(DataObject data)
This function is called when a save game is loaded. The DataObject passed as parameter contains the data that is saved. It’s crucial to make a check for null on the data object before using it, since it could be null if no data was found for the registered name – in that case, you can use the load game function to do default initialization.
Registering a custom save data #
You need to register your class that implements the ISaveData interface with Makinom’s save game handler.
To register a class, call the following function:
Maki.SaveGame.RegisterCustomData(string name, ISaveData saveData, bool beforeSceneLoad);
The parameters of the function:
- string name
The name used to register the custom save data.
You can only register one custom save data for per name.
Registering the same name again will replace the previously registered save data and pring a warning in the Unity console. - ISaveData saveData
An instance of the class that implements the ISaveData interface. - bool beforeSceneLoad
This flag decides if the custom save data will be loaded before (true) or after (false) the scene has been loaded when loading the save game.
If you want to unregister a class from the save game handler, you can call the following function:
ORK.SaveGame.UnregisterCustomData(string name, bool beforeSceneLoad);
Please note that only registered save data can be saved or loaded, i.e. you have to register your classes before saving or loading the game. This is best done in the scene you also initialize Makinom via a game starter.
Code Example #
Here’s a small code example on how the custom save data functionality can be used. The example uses a component (MonoBehaviour), but you can also use similar code in other classes.
It’s crucial that the class you register to the save game handler must be alive throughout the entire game, and it’s best if you register it in the same scene where Makinom is first initialized with a game starter. Don’t register custom save data in a running game, this will lead to the classes not being registered if you play the game at a later time and want to load a save game.
using UnityEngine; using GamingIsLove.Makinom; public class CustomSaveTest : MonoBehaviour, ISaveData { public bool toggle = false; public float number = 0; public string text = ""; // register to the Makinom save game handler protected virtual void Start() { Maki.SaveGame.RegisterCustomData("test", this, false); // keeps the component's game object alive GameObject.DontDestroyOnLoad(this.gameObject); } // called when a game is saved public virtual DataObject SaveGame() { DataObject data = new DataObject(); data.Set("toggle", this.toggle); data.Set("number", this.number); data.Set("text", this.text); return data; } // called when a game is loaded // depending on the 'beforeSceneLoad' parameter of the registration, // the function will be called either before or after loading the scene. public virtual void LoadGame(DataObject data) { if(data != null) { data.Get("toggle", ref this.toggle); data.Get("number", ref this.number); data.Get("text", ref this.text); } } }
Class within a Class #
You can also save the data from classes and class arrays within your custom save game data by implementing the ISaveData interface in the class. This allows creating complex save game structures.
Naturally you can also save classes within classes, within classes … there is no depth limit.
Saving single classes #
To add the data of a class to your custom save data:
data.Set("subClass", this.subClass.SaveGame());
To load the data of a class from your custom save data:
this.subClass.LoadGame(data.GetFile("subClass"));
Saving class arrays #
To add the data of a class array to your custom save data:
DataObject[] arrayData = new DataObject[this.classArray.Length]; for(int i=0; i<this.classArray.Length; i++) { arrayData[i] = this.classArray[i].SaveGame(); } data.Set("subClassArray", arrayData);
To load the data of a class array from your custom save data:
DataObject[] arrayData = data.GetFileArray("subClassArray"); if(arrayData != null) { this.classArray = new YourClass[arrayData.Length]; for(int i = 0; i < arrayData.Length; i++) { this.classArray[i] = new YourClass(); this.classArray[i].LoadGame(arrayData[i]); } }