Creating a basic savefile
on start i set the currentData
either to the content from a savefile
or create default values from a new class and saves that to file.
So the next time the game is loaded we have
either default values or the ones saved on file.
// shortened sudo-code
void Awake(){
if(saveFileExists){
currentData = (saveData)binaryFormater.Deserialize(file);
} else {
currentData = new saveData();
}
}
public void saveAll(){
FileStream file = File.Create(savePath);
bf.Serialize (file, currentData);
file.Close();
}
here is the saveData from lets say version 1.0
[Serializable]
public class saveData {
public float test1 = 0.1f;
}
But I might want to add fields to the default saveData when I add new content
ending up with a saveData file that looks like this:
[Serializable]
public class saveData {
public float test1 = 0.1f;
public bool hasSomething = false;
}
However when I then load data from my save file it obviously doesn't contain the field
// public bool hasSomething = false;
**How do i proceed checking the saveFile if it has the propertie?**
I tried using something like
FieldInfo[] allFields = typeof(saveData).GetFields(BindingFlags.Instance | BindingFlags.NonPublic|BindingFlags.Public);
to get all the available fields in the class.
But when i check if the field exist in the savefile it returns falls even to the ones that exists.
**What is the routine here? or do simply check if the savefile has missing fields and then applying the default value to them?**
↧