开发者

Any way to "save state" in a C# game?

开发者 https://www.devze.com 2022-12-20 05:28 出处:网络
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 o

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 somewh开发者_JS百科at 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...


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


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


I agree with the other posters that making your game state classes Serializable is probably the way you want to go. Others have covered basic serialization; 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://nhibernate.info/doc/burrow/faq

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号