views:

358

answers:

3

It's ok if the answer to this is "it's impossible." I won't be upset. But I'm wondering, in making a game using C#, if there's any way to mimic the functionality of the "save state" feature of console emulators. From what I understand, emulators have it somewhat easy, they just dump the entire contents of the virtualized memory, instruction pointers and all. So they can resume exactly the same way, in the exact same spot in the game code as before. I know I won't be able to resume from the same line of code, but is there any way I can maintain the entire state of the game without manually saving every single variable? I'd like a way that doesn't need to be extended or modified every single time I add something to my game.

I'm guessing that if there is any possible way to do this, it would use a p/invoke...

+10  A: 

Well, in C# you can do the same, in principle. It's called serialization. Agreed, it's not the exact same thing as a memory dump but comes close enough.

To mark a class as serializable just add the Serializable attribute to it:

[Serializable]
class GameState

Additional information regarding classes that might change:

If new members are added to a serializable class, they can be tagged with the OptionalField attribute to allow previous versions of the object to be deserialized without error. This attribute affects only deserialization, and prevents the runtime from throwing an exception if a member is missing from the serialized stream. A member can also be marked with the NonSerialized attribute to indicate that it should not be serialized. This will allow the details of those members to be kept secret.

To modify the default deserialization (for example, to automatically initialize a member marked NonSerialized), the class must implement the IDeserializationCallback interface and define the IDeserializationCallback.OnDeserialization method.

Objects may be serialized in binary format for deserialization by other .NET applications. The framework also provides the SoapFormatter and XmlSerializer objects to support serialization in human-readable, cross-platform XML.

Wikipedia: Serialization, .NET Framework

Joey
so basically, mark everything serializable, and then call serialize on my highest level game class? Then it will automatically serialize all of the objects it contains, and so on?
Tesserex
Exactly. The finer details of how all that works together are a little more complex but that's basically it, yes.
Joey
+3  A: 

If you make every single one of your "state" classes Serializable then you can literally serialize the objects to a file. You can then load them all up again from this file when you need to resume.

See ISerializable

Robin Day
A: 

I agree with the other posters that making your game state classes Serializable is probably the way you want to go. For a high end alternative you could look into NHibernate which will persist objects to a database. You can find some good info on NHibernate at these links: http://www.codeproject.com/KB/database/Nhibernate_Made_Simple.aspx

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx

Bill W
NHibernate is overkill for that sort of thing - it is real simple to just serialize everything to a file.
slugster