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_SO
s 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.
[System.Serializable]
public class ExampleSaveData: CustomSaveData
{
public string[] dictionaryKeys;
public int[] dictionaryValues;
}
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.
public override CustomSaveData SaveFields()
{
string[] keys = new string[dictionary.Count];
int[] values = new int[dictionary.Count];
dictionary.Keys.CopyTo(keys, 0);
dictionary.Values.CopyTo(values, 0);
return new ExampleSaveData
{
dictionaryKeys = keys,
dictionaryValues = values,
SceneName = SceneManager.GetActiveScene().name
};
}
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:
using UnityEngine.SceneManagement;
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.
public override void LoadFields(object data)
{
if(!(data is ExampleSaveData customData)) return;
dictionary.Clear(); // Ensure the dictionary starts empty
// Re-Populate Dictionary
if (saveData.dictionaryKeys != null && saveData.dictionaryValues != null &&
saveData.dictionaryKeys.Length == saveData.dictionaryValues.Length)
{
for (int i = 0; i < saveData.dictionaryKeys.Length; i++)
{
dictionary[saveData.dictionaryKeys[i]] = saveData.dictionaryValues[i];
}
}
base.LoadFields(data);
}
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 )
StoreData();
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:
using System.Collections.Generic;
using cowsins;
using cowsins.SaveLoad;
using UnityEngine;
using UnityEngine.SceneManagement;
public class NonSerializableExample : Identifiable
{
public Dictionary<string, int> dictionary = new Dictionary<string, int>();
[System.Serializable]
public class ExampleSaveData : CustomSaveData
{
public string[] dictionaryKeys;
public int[] dictionaryValues;
}
public override CustomSaveData SaveFields()
{
string[] keys = new string[dictionary.Count];
int[] values = new int[dictionary.Count];
dictionary.Keys.CopyTo(keys, 0);
dictionary.Values.CopyTo(values, 0);
return new ExampleSaveData
{
dictionaryKeys = keys,
dictionaryValues = values,
SceneName = SceneManager.GetActiveScene().name
};
}
public override void LoadFields(object data)
{
if (!(data is ExampleSaveData customData)) return;
dictionary.Clear();
if(customData.dictionaryKeys != null && customData.dictionaryValues != null && customData.dictionaryKeys.Length == customData.dictionaryValues.Length)
{
for(int i = 0; i < customData.dictionaryKeys.Length; i++)
{
dictionary[customData.dictionaryKeys[i]] = customData.dictionaryValues[i];
}
}
base.LoadFields(data);
}
public override void LoadedState()
{
}
}
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