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.

Pros
Cons

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.

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.

1

Add cowsins.SaveLoad library

using cowsins.SaveLoad;
2

Mark Saveable Variables with [SaveField]

In this example:

[SaveField] private int counter = 0;
3

Store Value in GameDataManager

StoreData();
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

  1. Your custom script has data that can be serialized and saved.

  2. Your custom script implements cowsins.SaveLoad library.

  3. Your custom script inherits from Identifiable.

  4. 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