Hello there.
I want to serialize game data for an Android game. I had to change some things about my first script that was working, but now I have problems with that script.
I am sure there is a much smoother way of doing this:
- I want to store my data in a “DataHandleScript” at runtime.
- I want to store my data in serialized binary between game sessions.
I tried to strip this whole thing down into one script, but I had some problems. I wanted to be able to call all variables without reference (-> static variables would be easy), but they are supposed to be serializable (so static is not possible).
Example:
SaveValues.highscore = x; //or SaveValues.Instance.highscore
SaveValues.Save();
I’ve tried following multiple tutorials, but they didn’t guide me to the solution I wanted, therefore I tried changing their suggestions a little. Only problem is that if I load another scene or reopen the app, the values have randomly increased a lot (e.g. from 115 scored points to 22.465).
This is the main part of my Saving Script:
[System.Serializable]
public static class SavingValues
{
public static SaveValues Instance;
void Awake(){
Instance = this;
}
public void Load()
{
if (File.Exists(Application.streamingAssetsPath+"/SaveFile.bfp"))
{
SaveValues.Instance.data = SaveData.Load(Application.persistentDataPath+"/"+SaveValues.Instance.fileName+".bfp");
SaveValues.Instance.highscore = SaveValues.Instance.data.GetValue<int>("Highscore");
SaveValues.Instance.collectedPoints = SaveValues.Instance.data.GetValue<int>("CollectedPoints");
SaveValues.Instance.shopUnlocked = SaveValues.Instance.data.GetValue<bool>("ShopUnlocked");
SaveValues.Instance.lifetimeScore = SaveValues.Instance.data.GetValue<int>("LifetimeScore");
}
}
public void Save()
{
SaveValues.Instance.data = new SaveData(SaveValues.Instance.fileName);
SaveValues.Instance.data["Highscore"] = SaveValues.Instance.highscore;
SaveValues.Instance.data["CollectedPoints"] = SaveValues.Instance.collectedPoints;
SaveValues.Instance.data["ShopUnlocked"] = SaveValues.Instance.shopUnlocked;
SaveValues.Instance.data["LifetimeScore"] = SaveValues.Instance.lifetimeScore;
SaveValues.Instance.data.Save();
}
public void Reset(bool firstStartDone)
{
SaveValues.Instance.data = new SaveData(SaveValues.Instance.fileName);
SaveValues.Instance.data["Highscore"] = 0;
SaveValues.Instance.data["CollectedPoints"] = 0;
SaveValues.Instance.data["ShopUnlocked"] = false;
SaveValues.Instance.data["LifetimeScore"] = 0;
SaveValues.Instance.data["FirstStartDone"] = firstStartDone;
SaveValues.Instance.data.Save();
}
And this is my DataHandleClass:
[System.Serializable]
public class SaveValues
{
public int highscore = 0; //saved Highscore
public int collectedPoints = 0; //Points collected on this account
public bool shopUnlocked = false; //Is shop unlocked
public int lifetimeScore = 0; //count of all points ever collected
public string fileName = "SaveFile"; //name of the save file
private SaveData data; //save data variable
}
in the same script.
I hope you can show me a better way of archieving my goal or find the reason for this script to strangely increasing my values.
Please help me, I have made great progress in my game but I’m stuck with this problem for a few weeks now and I can’t find a proper solution.
Thanks in advance.