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!
Keeping track off bools
Re: Keeping track off bools
Sounds good, thank you!
Re: Keeping track off bools
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:
Here is the vid:
Re: Keeping track off bools
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:
2. In your CuttableObjectSaver's ApplyData() method, call the CuttableObject's 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:
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();
}
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();
}
}
}
Re: Keeping track off bools
Ok, it looks like that worked. Once again thank you.
Re: Keeping track off bools
Glad I could help!