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!
Re: Keeping track off bools
Posted: Thu Sep 08, 2022 6:56 pm
by Tony Li
Hi,
You can use the save system and write a custom saver:
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:
Re: Keeping track off bools
Posted: Tue Sep 13, 2022 5:13 pm
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:
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:
// 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();
}
}
}
Re: Keeping track off bools
Posted: Wed Sep 14, 2022 3:06 am
by Aldurroth
Ok, it looks like that worked. Once again thank you.