本文介绍了用于 2D RPG 的 XML、YAML 和 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否将 XML、YAML 或 JSON 用于 C++ 2D RPG.

I can't figure out whether or not to use XML, YAML, or JSON for a C++ 2D RPG.

以下是我的想法:

  • 我需要一些简单的东西,不仅可以保存玩家数据,还可以保存环境数据,例如对象 (x, y) 坐标;加载时间;日期;图形配置等

  • I need something which is simple to save not just player data, but environment data, such as object (x, y) coordinates; load times; dates; graphics configurations, etc.

我需要一些灵活、易于使用、绝对重量轻、但功能强大的东西来处理上述问题.

I need something flexible, easy to use, and definitely light weight, but powerful to handle the above.

哪个是最好的选择?我有 JavaScript 中 JSON 的经验,但没有 C++ 经验.如果这是要走的路,在 C++ 中解析 JSON 有什么好的参考吗?

Which is the best choice? I have experience with JSON in JavaScript, but not C++. Are there any good references for parsing JSON in C++ if this is the route to go?

编辑

老实说,如果文本文件似乎是解决此类问题的最简单和最有效的解决方案(尤其是如果我可以将其写入二进制文件),那么我会全力以赴.

Honestly, if a text file seems like the simplest and most effective solution for something like this (especially if I can just write it to binary), then I'm all ears.

编辑 2

也欢迎提供其他建议.

推荐答案

我会用最简单的东西来满足你的要求.

I would use the simplest thing that satisfies your requirements.

如果您不需要分层存储,那么平面表格文件比其他任何东西都更容易处理.您所要做的就是从磁盘读取行并在选项卡上拆分.

If you don't need hierarchical storage, then flat tabular files are so much easier to deal with than anything else. All you have to do is read lines off disk and split on tab.

如果您正在查看更多的键/值对类型存储(而不是列表),那么 INI 文件 可能是合理的.这种格式具有很大的灵活性,但当您开始做比其设计用途更复杂的事情时,对其进行推理就不太容易了.

If you are looking at more of key/value pair type storage (as opposed to lists of things), then INI files can be reasonable. This format has a lot of flexibility, though reasoning about it can less approachable when you start doing more complicated things than it was designed for.

如果您需要分层,JSON 可能会更简单.有多种语言的 JSON 库,听起来您已经很熟悉了.

If you need hierarchical, it's possible that JSON would be simpler. There are JSON libraries in wide range of languages, and it sounds like you already familiar.

https://stackoverflow.com/questions/245973/whats-the-best-c-json-parser

sqlite 可能是另一种选择.SQL 中有龙,但有了一个很好的围绕 sqlite 的 C++ 包装器,它可以 易于管理.在我看来,主要的好处是 ACID.

sqlite may be another option. There be dragons in SQL, but with a nice C++ wrapper around sqlite, it can be manageable. The primary benefit would be ACID, in my opinion.

YAML 规范看起来有些冗长,所以我猜它有更多的厨房水槽.浏览一下 libyaml 文档,API 看起来有点像我过去使用的 SAX 接口.我对它没有后验知识,但如果没有充分的理由,我会不愿开始使用它.

The YAML spec looks somewhat lengthy, so I can guess that it has more kitchen sinks. Just skimming the libyaml docs, the API looks somewhat like SAX interfaces that I've used in the past. I have no a posteriori knowledge of it, but I would be reticent to start using it without a good reason.

XML 处理起来很糟糕,不要选择使用它.这有很多原因.我认为在我看来最相关的一个是它很容易使使用它的代码比其他情况更复杂.我见过的任何使用 XML 设计的系统,关于 XML 的推理都比它试图支持的设计兴趣更复杂.它有很多有效用途,但很少有其他存储系统不能满足要求.

XML sucks to deal with, don't opt in to it. There lots of reasons for this. I think the most relevant one in my mind is that it's prone to make the code that uses it more complicated than it would be otherwise. Any system I've seen designed with XML, reasoning about the XML is more complicated than the design interests that its trying to support. There are valid uses for it, though it's rare that another storage system wouldn't have been just as adequate.

无论您选择哪一个,编写尽可能少的代码来管理它.您真的想编写引擎将首先使用的类.然后担心序列化它们.如果你让你的序列化影响你的类设计,你可能会后悔.:)

Regardless of which one you choose, write as little code as you can managing it. You really want to write the classes your engine will use first. Then worry about serializing them. If you let your serialization influence your class design, you'll probably regret it. :)

这篇关于用于 2D RPG 的 XML、YAML 和 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:38