Saving Custom Data: Advanced Method
This method is only required for very specific cases where data to store is not serializable.
Early Considerations
For most scenarios, the simple method is more than enough to successfully save custom data. However, there might be situations where you need to save data that cannot be serialized ( excluding Item_SOs as those can be processed by GameDataManager ).
For those situations, you´ll need to expand CustomSaveData to support custom serialization.
Supports Custom Serialization
Requires Coding
This method does NOT use Attributes to mark which variables are meant to be saved. [SaveField] as it relies on coding.
Saving Custom Data
Imagine you´ve created a script that contains non-serializable data that needs to be saved.
using UnityEngine;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
// Field that we need to save. Not Serializable
public Dictionary<string, int> dictionary = new Dictionary<string, int>();
}In this example scenario, since Dictionary<string, int> cannot be serialized, we´ll need to create a new class that inherits from CustomSaveData, where we can place the data types to be saved. Here´s a step-by-step guide:
Expanding Custom SaveData
Dictionaries cannot be serialized & stored into a Json file directly, so instead, we´ll store a string array and an int array separately. For that, we need to create a new class that inherits from CustomSaveData, so it can store our custom arrays that will act as if they were a Dictionary.
You can copy & paste this structure.
Make sure to modify CustomSaveDataName with the name of your new expanded CustomSaveData, and define the serializable data that you require.
Processing & Saving Fields
After our Serializable data is structured, we need to process the data from non-serializable to serializable so it can be saved. In this example, we´ll pass keys to a string array, and values to an int array.
In this example, we divide the dictionary into two serializable arrays and return an expanded version of CustomSaveData (named ExampleSaveData in this case) containing these arrays. Additionally, we include the SceneName, which is crucial. To accomplish this, make sure to implement the following library:
You can copy & paste this structure.
Make sure to modify CustomSaveDataName with the name of your expanded CustomSaveData, process the data and assign it to CustomSaveDataName .
Processing & Loading Fields
Loading is essentially the same process as Saving but instead of processing from Non-Serializable to Serializable, we need to convert the serializable data back to their original data types and formats. In this case, we´ll populate the dictionary based on the keys and values arrays.
You can copy & paste this structure.
Ensure you assign the original data when loading the fields.
Make sure to modify CustomSaveDataName with the name of your expanded CustomSaveData.
Notify GameDataManager
At this step of the guide, your CustomSaveData logic is fully set-up, we just need a way to tell the GameDataManager that this class needs to be saved. Luckily for us, this can be done the same way as in the Simple Method.
Simply run the following line whenever you want to notify GameDataManager of a data change ( Whenever any of your saveable fields change )
Remember that this method is only required for very specific cases where data to store is not serializable.
Here´s the full code for this example:
Checklist
You need to save non-serializable data types from a custom class
You implemented cowsins.SaveLoad library
Your class inherits from Identifiable
You expanded CustomSaveData
You are processing data into Serializable Fields in SaveFields()
You are loading data back to its original format in LoadFields()
You are using StoreData() somewhere in your code to ensure data is sent to the GameDataManager
What to do now? Loaded State.
Once you are done Saving & Loading data, you can run custom behaviour based on the loaded data. Whenever LoadFields is called, LoadedState() will also be called.
Check this guide for more Information.
Last updated