Saving Custom Data: Simple Method
Early Considerations
For most scenarios, the simple method is more than enough to successfully save custom data. Except specific cases from data that needs to be saved from the Inventory Pro Add-On, the rest of the data in the Save & Load Add-On is Saved by implementing this method.
Simple & Fast to Implement
Only supports Serializable Data Types ( int, floats, bools, etc... )
Attribute Based [SaveField]
No-Code Required
Saving Custom Data
Imagine you’ve created a custom script that holds custom data that needs to be saved. For this example, let’s say you have a counter that increments an int
every 5 seconds, and you want to store the value of that int
whenever the game is saved.
IMPORTANT: The DataPersistenceManager
must exist in the scene for data to be saved.
You don’t need to manually add the DataPersistenceManager
to each scene, as it is marked with [DontDestroyOnLoad]
. Simply place the DataPersistenceManager
in your Main Menu scene. When starting the game from a gameplay scene, data won’t be saved. However, when starting from the Main Menu, the DataPersistenceManager
will persist across different scenes, ensuring data can be saved.
Below, you can take a look at the example code:
using System.Collections;
using UnityEngine;
public class CounterSave : MonoBehaviour
{
// The counter value that we want to save
public int counter = 0;
// Time interval (in seconds) to increment the counter
private float timer = 0f;
private float interval = 5f; // Increment every 5 seconds
private void Update()
{
// Increment the counter every 5 seconds
timer += Time.deltaTime;
if (timer >= interval)
{
counter++;
timer = 0f;
}
}
}
Now, we need to tell the Save & Load system which variable we want to save.
using System.Collections;
using UnityEngine;
using cowsins.SaveLoad; // Make sure to include this to work with Save & Load Add-On
// Ensure your Script inherits from Identifiable
public class CounterSave : Identifiable
{
// Saveable fields must implement [SaveField]. It must be a serializable field.
[SaveField] public int counter = 0;
private float timer = 0f;
private float interval = 5f;
private void Update()
{
timer += Time.deltaTime;
if (timer >= interval)
{
counter++;
timer = 0f;
// Send the new value to GameDataManager to store it.
StoreData();
}
}
}
Just like that, value will be serialized as a CustomSaveData into the data.json file.
You can access this value from the Save&Load section in the Cowsins Manager, at the Custom Objects Dropdown.
Checklist
Your custom script has data that can be serialized and saved.
Your custom script implements cowsins.SaveLoad library.
Your custom script inherits from Identifiable.
You update the value to be saved and notify GameDataManager through StoreData() method.
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