我应该如何保存和加载 C++ 游戏的用户配置文件?

发布于 2024-08-09 23:31:04 字数 294 浏览 3 评论 0 原文

我需要一种将游戏的用户配置(控件、图形等)保存和加载到文件的方法。我想以跨平台的方式做到这一点,如果我必须使用库,我希望它们有一个足够简单的语法。如何保存和加载配置文件,应该使用什么格式? XML 和 INI 似乎很流行,但我发现的所有解析器都有令人困惑的语法,而且我不知道从哪里开始让我自己的解析器运行。

顺便说一句,配置文件可能不会变得太复杂,因此轻量级且快速的文件是最好的。

顺便说一句 - 我正在考虑使用脚本来定义诸如项目之类的东西,但是像 xml 这样的标记语言更好还是脚本语言(像angelscript,这就是我正在使用的)更好?

I need a way to save and load user configurations for a game (controls, graphics, etc.) to a file. I want to do this in a cross-platform manner, and if I have to use libraries, I want them to have a simple enough syntax. How do I save and load config files, and what format should I use? XML and INI seem popular but all of the parsers I have found have confusing syntax, and I have no clue where to start to get my own parser going.

By the way, the config files probably won't get too complex, so something lightweight and fast would be best.

An aside - I was thinking about using scripting to define things like items, but would a markup language like xml be better or a scripting language (like angelscript, which is what I'm using) be better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

生生不灭 2024-08-16 23:31:04

Google Protocol Buffers

我长期以来一直存在一个困惑想想,序列化消息

序列化

通常是短暂的并且涉及传递对象。

正如文章中所述,COMCORBA 是其使用的著名示例。

消息传递

是关于不同实体之间的通信。


问题是,许多人在看到消息传递问题时只是想:序列化并解决它。

但这并不适应。

对象的序列化意味着能够对对象进行“编码”并恢复它...如果您突然改变方式并且原来的一个对象现在变成了 2 个,会发生什么情况,您如何处理向后兼容性 ?

这里的问题是,如果序列化在稳定模型上运行得很好,那么模型的任何更改都可能会很痛苦。

另一方面,消息传递将消息编码和解码与当前模型解相关,因此,对模型的更改只需在消息的编码和解码中进行处理,并且模型从向后兼容性的负担。


现在对于配置文件,您可能需要向后兼容性。那么选择消息传递解决方案符合您的最佳利益。

我建议使用 Google Protocol Buffers,因为它是可靠且经过验证的。

  1. 向前和向后兼容性很容易处理
  2. 可以生成人类可读或二进制格式
  3. 相同的消息可以用多种语言进行编码/解码

我不必解释为什么1.很重要。 2. 意味着您可以简单地手动编辑文件来调整它, 3. 意味着您可以轻松编写 GUI 或检查脚本。

Google Protocol Buffers

There is a long standing confusion I think, between serialization and messaging.

Serialization

Is generally short-lived and involves passing around objects.

As stated in the article, COM and CORBA are well-known examples of its uses.

Messaging

Is about communication between different entities.


The problem is that many people, when they see a messaging problem simply think: serialize and be done with it.

But this is not adapted.

Serialization of an object means being able to 'encode' the object, and restore it... what happens if suddenly you change your ways and what was one object is now 2, how do you deal with backward compatibility ?

The problem here is that if Serialization works pretty well with stable model, any change of the model may be a pain.

Messaging on the other hand decorrelates the message encoding and decoding from the current model, therefore, changes to the model just have to be treated in the encoding and decoding of the message, and the model is freed from the burden of backward compatibility.


Now for a configuration file, you probably want backward compatibility. Then it is in your best interest to elect a Messaging solution.

I suggest Google Protocol Buffers, because it's sound and proven.

  1. Forward and Backward compatibility is easy handled
  2. May generate either human readable or binary format
  3. A same message can be encoded / decoded in a wide variety of languages

I don't have to present why 1. is important. 2. means that you can simply edit the file manually to tweak it, 3. means that you can easily code a GUI or a checking script.

做个少女永远怀春 2024-08-16 23:31:04

如何创建一个代表您的配置的 C++ 类,然后将其序列化?例如,查看 boost 序列化。

http://www.boost.org/doc/ libs/1_40_0/libs/serialization/doc/index.html

What about to make a C++ class that represent your configuration and then serialize it? Look for example at boost serialization.

http://www.boost.org/doc/libs/1_40_0/libs/serialization/doc/index.html

他夏了夏天 2024-08-16 23:31:04

我正在使用 Lua,它是轻量级的并且运行良好作为配置语言。随着时间的推移,我开始越来越多地转向 Lua 脚本,直到除了核心引擎之外的所有内容都用 Lua 进行编码。在最新的迭代中,甚至游戏动态也使用回调在 Lua 中进行编码,对性能影响很小。我并不是说你应该这样做,但 Lua 会让你成长,并且很容易在 c/c++ 中实现。

I am using Lua which is light weight and works well as a configuration language. As years went by I started moving more and more out to the Lua scripts until everything but the core engine was coded in Lua. In the latest iteration even the game dynamics are coded in Lua using callbacks with very little hit on performance. I'm not saying you should do this but Lua will let you grow and is very easy to implement in c/c++.

够运 2024-08-16 23:31:04

我需要一种将游戏的用户配置(控件、图形等)保存和加载到文件的方法。

游戏通常使用专有格式来存储配置信息,因为它们经常被滥用(考虑破解配置文件以增加您拥有的备用生命数量或可以使用的弹药)。此类敏感信息通常以二进制形式写入,并进行良好的加密。如果您不需要存储可能影响游戏玩法的信息,那么继续使用基于文本/基于标记/人类可读的格式是完全可以的。二进制格式有自己的一系列关于平台等的问题(在这方面查找帖子)。

我正在考虑使用脚本来定义项目之类的东西,但是像 xml 这样的标记语言更好,还是脚本语言(比如我正在使用的angelscript)更好?

考虑到我在上一节中的评论,XML 绝对是一个不错的选择。它是人类可读的,许多解析器是免费可用的(而且很容易编写一个)等等。脚本编写完全是另一回事。您真的需要脚本以及控制结构和函数吗?这又是一个设计决定,所以由你来决定。

没有一个正确的答案。首先,文本/XML 格式对我来说看起来不错。但随着时间的推移和游戏复杂性的增加,您可能需要转向 DSL 和二进制形式。

I need a way to save and load user configurations for a game (controls, graphics, etc.) to a file.

Games typically use proprietary formats for storing configuration information because they can often be misused (think of hacking up a config file to increase the number of spare lives you have or the ammo that you can employ). Such sensitive information is often written in binary and for good measure encrypted. If there is no such information that you need to store that can compromise the game-play, it is perfectly fine to go ahead with a text-based/markup based/human readable format. Binary formats have their own set of issues w.r.t platforms etc (lookup SO for posts in this regard).

I was thinking about using scripting to define things like items, but would a markup language like xml be better or a scripting language (like angelscript, which is what I'm using) be better?

XML is definitely a good choice keeping in view my remarks in the previous section. It is human readable, a number of parsers are freely available (and also it is easy to write one) etc. Scripting is a different beast altogether. Do you really need to have a script and control structures and functions? This is a design decision again, so its upto you to decide.

There is no one right answer. To start off, a text/XML format looks fine to me. But with time and increased complexity of the game you may need to move towards a DSL and binary form.

独﹏钓一江月 2024-08-16 23:31:04

您可能对 消息类 感兴趣,它是我的 < a href="https://public.msli.com/lcs/muscle/" rel="nofollow noreferrer">网络库。它可以让您方便地存储任何类型的数据,并根据需要包含尽可能多或尽可能少的层次结构/结构。数据可序列化为与平台无关的二进制格式。有适用于 C、C++、Java、Python 和 C# 的 API,并且易于使用。

多年来我一直使用它来存储相当复杂的项目文件,它对我来说非常有效。

You might be interested in the Message class that is part of my networking library. It lets you store any kind of data conveniently, with as much or as little hierarchy/structure as you want to include. The data is serializable to a platform-neutral binary format. There are APIs for C, C++, Java, Python, and C#, and it's easy to use.

I've been using it to store fairly complex project files for years, and it's worked great for me.

鯉魚旗 2024-08-16 23:31:04

许多游戏都将这些设置存储在二进制文件中。它比 XML 更快,但现在我认为这并不重要。也许可以解释一下您在使用 XML 文件时遇到的问题,以便我们可以提供帮助。

A lot of games have stored these settings in a binary file. It would be faster than XML but nowadays I don't think it matters. Maybe explain what trouble you are having with XML files so we can help.

大姐,你呐 2024-08-16 23:31:04

IniParser - 易于使用,标准 ini 语法,C,但包装在类中很简单。

IniParser - Easy to use, standard ini syntax, C but trivial to wrap in a class.

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