Keeping track off bools

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Aldurroth
Posts: 43
Joined: Wed Jul 20, 2022 8:44 pm

Keeping track off bools

Post by Aldurroth »

Hello! I have this grass object thing that has a "isCut" bool that I would like to keep track of between scenes. There will be many instances of this thing and I would like to keep track of all of them. Is there a "save" system that could do this or will I need to write a custom script using the Dialogue system as a base?

Thank you!
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Keeping track off bools

Post by Tony Li »

Hi,

You can use the save system and write a custom saver:

How To: Write Custom Savers
Aldurroth
Posts: 43
Joined: Wed Jul 20, 2022 8:44 pm

Re: Keeping track off bools

Post by Aldurroth »

Sounds good, thank you!
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Keeping track off bools

Post by Tony Li »

Glad to help!
Aldurroth
Posts: 43
Joined: Wed Jul 20, 2022 8:44 pm

Re: Keeping track off bools

Post by Aldurroth »

Sorry to call you back but i'm having trouble getting the script to work. Here is a vid to better show off the problem. Basically the grass is not remembering what state I left it in between scenes. I got it to work once and that was it. After trying it out on the actual game levels everything stoped working. The grass object is a "PREFAB" so I have no idea if that is what is causing some of the issues.

Here is the vid:
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Keeping track off bools

Post by Tony Li »

Hi,

You're close! Here are my recommendations:

1. In your CuttableObject script, move your "if (isCut) {...} if (!isCut) {...}" code to a separate public method -- for example, called SetAppearance(). Then change your Start() method to:

Code: Select all

private void Start90
{
    if (col == null && isCut == false)
    {
        col = GetComponent<Collider>();
    }
    SetAppearance();
}
2. In your CuttableObjectSaver's ApplyData() method, call the CuttableObject's SetAppearance():

Code: Select all

public override void ApplyData(string s)
{
    if (string.IsNullOrEmpty(s)) return; // If we didn't receive any save data, exit immediately.
    var cuttableObject = GetComponent<CuttableObject>();
    cuttableObject.isCut = (s == "1");
    cuttableObject.SetAppearance();
}
---

That should get it working. The big downside is that you'll need a separate CuttableObjectSaver for each CuttableObject, and you'll need to assign a unique Key value in the inspector to each one.

You might find it nicer to work with a single saver component that can save the states of all of the CuttableObjects in the scene. To do that, change your CuttableObjectSaver script into a CuttableObjectsSaver script that might look something like:

Code: Select all

// No longer need [RequireComponent(typeof(CuttableObject))]

public class CuttableObjectsSaver : Saver
{
    [System.Serializable]
    public class Data
    {
        public bool[] isCut;
    }
    
    public CuttableObject[] cuttableObjects; // Assign CuttableObjects to this list in the inspector.
    
    public override string RecordData()
    {
        Data data = new Data();
        data.isCut = new bool[cuttableObjects.Length];
        for (int i = 0; i < cuttableObjects.Length; i++)
        {
            if (cuttableObjects[i] == null) continue; // Gracefully handle unassigned elements in list.
            data.isCut[i] = cuttableObjects[i].isCut;
        }
        return SaveSystem.Serialize(data);
    }
    
    public override void ApplyData(string s)
    {
        if (string.IsNullOrEmpty(s)) return; // If we didn't receive any save data, exit immediately.
        Data data = SaveSystem.Deserialize<Data>(s);
        if (data == null) return;
        for (int i = 0; i < cuttableObjects.Length; i++)
        {
            if (cuttableObjects[i] == null) continue; // Gracefully handle unassigned elements in list.
            cuttableObjects[i].isCut= data.isCut[i];
            cuttableObjects[i].SetAppearance();
        }
    }
}
Aldurroth
Posts: 43
Joined: Wed Jul 20, 2022 8:44 pm

Re: Keeping track off bools

Post by Aldurroth »

Ok, it looks like that worked. Once again thank you.
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Keeping track off bools

Post by Tony Li »

Glad I could help!
Post Reply