Page 1 of 1

Saving Static class

Posted: Thu Jun 06, 2024 2:32 pm
by squidgeroo
Hi there, I have a static class of GlobalVariables which I use for inventory, discovered scenes, current position etc. I'm only using the dialog and quests system.

I wondered if there was any examples of saving and loading a static class using the Pixel crushers dialog system. I tried something like this, but the ISaveable didn't exist.

Im looking for an example on how to serialize, save and load custom data.

Code: Select all

using UnityEngine;
using System.Collections.Generic;
using PixelCrushers;

public class GlobalVariablesSaver : MonoBehaviour, PixelCrushers.ISaveable
{
    [System.Serializable]
    public struct SaveDataStruct
    {
        public int CurrentLocationID;
        public int CurrentZoneID;
        public Vector3 newPlayerPosition;
        public int PlayerSceneIndex;
    }

    public string key = "globalVariables";

    public object SaveData()
    {
        return new SaveDataStruct
        {
            CurrentLocationID = GlobalVariables.CurrentLocationID,
            CurrentZoneID = GlobalVariables.CurrentZoneID,
            newPlayerPosition = GlobalVariables.newPlayerPosition,
            PlayerSceneIndex = GlobalVariables.PlayerSceneIndex,
        };
    }

    public void LoadData(object data)
    {
        var saveData = (SaveDataStruct)data;
        GlobalVariables.CurrentLocationID = saveData.CurrentLocationID;
        GlobalVariables.CurrentZoneID = saveData.CurrentZoneID;
        GlobalVariables.newPlayerPosition = saveData.newPlayerPosition;
        GlobalVariables.PlayerSceneIndex = saveData.PlayerSceneIndex;
    }

    void OnEnable()
    {
        SaveSystem.RegisterSaver(this);
    }

    void OnDisable()
    {
        SaveSystem.UnregisterSaver(this);
    }
}

Re: Saving Static class

Posted: Thu Jun 06, 2024 4:45 pm
by Tony Li
Hi,

You can write a custom saver (see: How To: Write Custom Savers) to save your static data. Since the saver component needs to be on a GameObject, you can put it on an empty GameObject or on the Dialogue Manager or Quest Machine GameObject.

Re: Saving Static class

Posted: Fri Jun 07, 2024 4:03 am
by squidgeroo
Great, thanks Tony, I think this is just what I'm after :) Let me try that and get back to you, thanks for your support and awesome software

Re: Saving Static class

Posted: Fri Jun 07, 2024 8:23 am
by Tony Li
Glad to help! If you have any questions about setting up the save system or writing your custom saver, let me know.