I want to be able to save and load all attributes of this PlayerStats
class to and from a binary file. Do Weapon
and PlayerClass
need to be serializable too?
The weapons array contains classes that inherit from Weapon
such as Pistol
and Rifle
. Would they also need to be serializable?
[Serializable]
public class PlayerStats : Saveable<PlayerStats> {
private Weapon[] weapons;
private PlayerClass activeClass;
// ...
}
I've already done this successfully with another class, but the only attribute the class had was a dictionary.
edit, this is what I'm trying to use to serialize that works with my other more simple class but not the PlayerStats
class:
public abstract class Saveable<T> {
#region BinarySaveable
public virtual void saveInstanceToBinaryFile(T instance, string fileName) {
BinaryFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream (fileName, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, instance);
stream.Close ();
}
public virtual T loadInstanceFromBinaryFile(string fileName) {
BinaryFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream (fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
T instance = (T)formatter.Deserialize (stream);
stream.Close ();
return instance;
}
#endregion
If this were Java I'd probably be able to just use the WriteObject(Object obj)
method.