Hello, I am currently working to create a 2D platformer-like game and I am stumped on saving game data (Serialization). I understand what serialization is (To an extent) but I cannot get it to work.
Here is my serialization code:
public void SavePlayerProfile()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("PlayerSaveData.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, this);
stream.Close();
}
And here is my deserialization code:
public PlayerProfile LoadPlayerProfile()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("PlayerSaveData.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
PlayerProfile p = (PlayerProfile)formatter.Deserialize(stream);
stream.Close();
return p;
}
Both of these work like a charm but the PlayerProfile object cannot be deserialized unless it is serialized within the same instance of the game. Once I close the project and try loading (Deserializing), it will throw a NullReferenceException. I truly hope the problem isn't something too obvious that I may be overlooking xD