I have an entity parent class
public class EntityClass : MonoBehaviour{
public int entID;
//some vars and methods
}
then I have some classes that inherit from it:
public class CharClass : EntityClass{
public CharStats stats;
//some other char specific vars and methods
}
public class CropClass : EntityClass{
public CropStats stats;
//some other crop specific vars and methods
}
they each take a different class for stats, which are serializable:
[Serializable]
public class CharStats{
public float WalkSpeed;
//etc more char vars
}
[Serializable]
public class CropStats{
public byte croptype;
//etc more crop vars
}
Now I want to serialize these 2 classes, cropstats and charstats, and I want to serialize them as the same filetype (.stat). I serialize them like this:
BinaryFormatter bf = new BinaryFormatter();
Filestream fs = File.Create(filepathetc + "entID.stat")
bf.Serialize(fs, new CharStats() );
//or
bf.Serialize(fs, new CropStats() );
so 1.stat could either be a file that contains either a crop or a char, so that when I initialize an char or crop entity I can do
BinaryFormatter bf = new BinaryFormatter();
Filestream fs = File.Open(filepathetc + "entID.stat", FileMode.Open)
//?.stats = ( ??? ) bf.Deserialize(fs);
//? = CharClass or CropClass
//??? = CharStats or CropStats
Since .stat files can be either from chars or crops, is there a way before I deserialize the file to know if I should be creating a CharClass and deserializing the .stat as (CharStats) in this setup?
I can solve this by creating an extra file that has info if entID=1 is either a crop or a char, but I was just wondering if there was a way to know the type of class a file was serialized as before deserializing it, sorry if it is obviously not possible since to know what is inside a file it it has to be opened first.
Thanks!