- Home /
Deserialize a static class C#
I recently wrote a script that was supposed to store variables for my whole game and also to save and load these via binary formatted serialization. A few days ago, my script had an InvalidCastException but the problem got solved. Now I edited it, but it gives me an error:
`data': cannot declare variables of static types
This is what I changed the script to, now:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[System.Serializable]
public static class SaveValues
{
public static int highscore = 0; //saved Highscore
public static int collectedPoints = 0; //Points collected on this account
}
[System.Serializable]
public static class DataHandleClass
{
public static void Load()
{
if(File.Exists(Application.persistentDataPath + "/savedGames.bfp"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/savedGames.bfp", FileMode.Open);
SaveValues data = (SaveValues)bf.Deserialize(file);
file.Close();
SaveValues.highscore = data.highscore;
SaveValues.collectedPoints = data.collectedPoints;
}
}
public static void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/savedGames.bfp");
SaveValues data = new SaveValues(); //this is the problem
data.highscore = SaveValues.highscore;
data.collectedPoints = SaveValues.collectedPoints;
file.Close();
}
}
I get that I cannot declare this static function right there, but I need to have these variables and the class as static so I can easily change them from other scripts. Can anyone help me to correct this? How can I use serialization the right way? Maybe @Bunny83 can help me out again?
Thanks in advance
Answer by Bunny83 · Nov 07, 2015 at 06:35 AM
Static variables can't be serialized / deserialized, at all. When you deserialize something you get an instance back. Static variables don't belong to an instance.
You should always avoid static variables if possible.
Static variables can raise a lot of problems. A static class can't be instantiated. That's one point of static classes.
If you really want to keep your data static, as i said in my answer on your other question, either use a "singleton" approach (like shown in the other answer) or you have to copy your variables into an actual instance of a class. You could turn your "SaveValues" class into a normal class and keep your static variables. When you want to serialize the values you have to copy the static variables to the instance variables of a temporary instance which you can serialize. When you want to deserialize you do the reverse:
[System.Serializable]
public class SaveValues
{
public static int highscore = 0;
public static int collectedPoints = 0;
public int save_highscore = 0;
public int save_collectedPoints = 0;
}
public static class DataHandleClass
{
public static void Load()
{
if(!File.Exists(Application.persistentDataPath + "/savedGames.bfp"))
return;
BinaryFormatter bf = new BinaryFormatter();
using(FileStream file = File.Open(Application.persistentDataPath + "/savedGames.bfp", FileMode.Open))
{
SaveValues data = (SaveValues)bf.Deserialize(file);
file.Close();
SaveValues.highscore = data.save_highscore;
SaveValues.collectedPoints = data.save_collectedPoints;
}
}
public static void Save()
{
BinaryFormatter bf = new BinaryFormatter();
using(FileStream file = File.Create (Application.persistentDataPath + "/savedGames.bfp"))
{
SaveValues data = new SaveValues();
data.save_highscore = SaveValues.highscore;
data.save_collectedPoints = SaveValues.collectedPoints;
bf.Serialize(file, data);
file.Close();
}
}
}
If you don't want to mix instance and static variables in the same class you can use a seperate class which you actually use to serialize the data.