任何“保存状态”的方法都可以。在 C# 游戏中?
如果答案是“不可能”也没关系。我不会不高兴。但我想知道,在使用 C# 制作游戏时,是否有任何方法可以模仿控制台模拟器的“保存状态”功能。据我了解,模拟器有点简单,它们只是转储虚拟内存、指令指针等的全部内容。因此,它们可以以完全相同的方式恢复,在游戏代码中与以前完全相同的位置。我知道我无法从同一行代码恢复,但是有什么方法可以维护游戏的整个状态而无需手动保存每个变量?我想要一种不需要每次向游戏添加内容时都需要扩展或修改的方法。
我猜想如果有任何可能的方法可以做到这一点,它将使用 ap/invoke...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,原则上在 C# 中你也可以做同样的事情。它称为序列化。同意,它与内存转储不完全相同,但足够接近。
要将类标记为可序列化,只需添加
Serialized
属性:有关可能更改的类的其他信息:
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:Additional information regarding classes that might change:
如果将每一个“状态”类设置为可序列化,那么您就可以将对象序列化到文件中。然后,当您需要恢复时,您可以从此文件再次加载它们。
请参阅 ISerialized
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
我同意其他发帖人的观点,即使您的游戏状态类可序列化可能是您想要的方式。其他人涵盖了基本的序列化;对于高端替代方案,您可以考虑 NHibernate,它将对象保存到数据库中。您可以在以下链接中找到有关 NHibernate 的一些有用信息:
http://www.codeproject.com/KB/database/Nhibernate_Made_Simple.aspx
http://nhibernate.info/doc/burrow/faq
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