Saving Static class

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
squidgeroo
Posts: 2
Joined: Thu Jun 06, 2024 2:24 pm

Saving Static class

Post 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);
    }
}
User avatar
Tony Li
Posts: 21676
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving Static class

Post 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.
squidgeroo
Posts: 2
Joined: Thu Jun 06, 2024 2:24 pm

Re: Saving Static class

Post 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
User avatar
Tony Li
Posts: 21676
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving Static class

Post 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.
Post Reply