任何“保存状态”的方法都可以。在 C# 游戏中?

发布于 2024-08-21 16:18:03 字数 267 浏览 5 评论 0原文

如果答案是“不可能”也没关系。我不会不高兴。但我想知道,在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彡翼 2024-08-28 16:18:03

嗯,原则上在 C# 中你也可以做同样的事情。它称为序列化。同意,它与内存转储不完全相同,但足够接近。

要将类标记为可序列化,只需添加 Serialized属性

[Serializable]
class GameState

有关可能更改的类的其他信息:

如果将新成员添加到可序列化类中,则可以使用 OptionalField 属性 允许对对象的早期版本进行反序列化而不会出现错误。此属性仅影响反序列化,并防止运行时在序列化流中缺少成员时引发异常。还可以使用 NonSerialized 属性来标记成员 表明它不应该被序列化。这将使这些成员的详细信息保密。

修改默认反序列化(例如,自动初始化标记为 NonSerialized),该类必须实现 IDeserializationCallback 接口并定义 IDeserializationCallback.OnDeserialization 方法

对象可以以二进制格式序列化,以便由其他 .NET 应用程序反序列化。该框架还提供 SoapFormatterXmlSerializer 对象来支持人类可读的跨平台 XML 中的序列化。

维基百科:序列化,.NET Framework

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

腻橙味 2024-08-28 16:18:03

如果将每一个“状态”类设置为可序列化,那么您就可以将对象序列化到文件中。然后,当您需要恢复时,您可以从此文件再次加载它们。

请参阅 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

情泪▽动烟 2024-08-28 16:18:03

我同意其他发帖人的观点,即使您的游戏状态类可序列化可能是您想要的方式。其他人涵盖了基本的序列化;对于高端替代方案,您可以考虑 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文