问题描述
我们中的许多人都被灌输了使用 XML 存储数据的知识.它的优点和缺点是众所周知的,我当然不想在这里讨论它们.但是在我用 C++ 编写的项目中,我也在使用 Lua.我非常惊讶 Lua 可以很好地用于存储和处理数据.然而,Lua 的这一方面鲜为人知,至少在游戏编程领域是如此.
Many of us have been indoctrinated in using XML for storing data. It's benefits and drawbacks are generally known, and I surely don't want to discuss them here. However in the project I'm writing in C++, I'm also using Lua. I've been very surprised how well Lua can be used to store and handle data. Yet, this aspect of Lua is less recognized, at least in the game programming world.
我知道 XML 在诸如通过 Internet 发送数据的情况下以及在需要安全的地方(例如使用从网络下载的数据或加载用户可编辑的配置文件)以及最终在不同语言的程序正在读取相同数据的情况.
I'm aware that XML has it's advantages in cases like sending data over the internet, and in places where safety comes into play (using data downloaded from the net for example, or loading user-editable configuration files) and finally in cases where the same data is being read by programs in different languages.
然而,一旦我了解到使用 Lua 处理数据是多么的好和容易(尤其是有 luabind 来支持你!),我开始怀疑是否有任何理由使用 XML 来存储游戏数据,如果我们已经使用 Lua无论如何?
However once I learned how nice and easy it is to handle data using Lua (especially having luabind to back you up!), I started to wonder is there any reason to use XML to store game data, if we already use Lua anyway?
暴雪虽然使用 Lua 编写 UI 脚本,但仍将布局存储在 XML 中.原因是否仅与 UI 相关?
Blizzard, while using Lua for scripting the UI, still stores the layout in XML. Is the reason something that is only UI related?
使用 Lua 作为数据存储语言的缺点是什么?
What are the drawbacks of using Lua as a data storage language?
推荐答案
这可能不是您所期望的那种答案,但它可能会帮助您做出决定.
This might not be the kind of answer you expected, but it might help you make your decision.
Blizzard (WoW) 使用 XML 来定义 UI.它有点像 C# 中的 XAML,只是功能要弱得多,而且大多数插件只是使用 XML 来引导插件,然后在 lua 代码中构建 UI.
Blizzard (WoW) uses XML to define UI. It's kinda like XAML in C#, just a lot less powerful and most addons just use XML to bootstrap the addon and then build UI in lua code.
此外WoW 实际上将插件保存的变量"存储在 .lua 文件中.
在我看来,它并没有那么重要.选择您喜欢且易于扩展引擎的人使用的内容.
In my opinion it doesn't mater that much. Choose something you like and which is easy to use for those who are going to extend your engine.
XML 的好处是已经编写了很多工具和代码来测试、编写和解析 XML,这意味着它可以为您节省一些时间.例如,XML Schema 对于验证用户编写的文件非常有用(安全性只是一个副作用,好消息是,如果它通过您的架构,数据很可能是 100% 安全的,并准备好插入您的引擎)并且已经编写了很多验证器供您使用.
The good thing about XML is that there are A LOT of tools and code already written to test, write and parse XML which means it could save you some time. For example XML Schema's are very useful for validating user written files (security is just a side effect, the good thing is that if it passes your schema, the data is most likely 100% safe and ready to be plugged into your engine) and there quite a few validators already written for you to use.
然后一些用户又害怕 XML 文件(即使它们非常易读,可能太易读)并且更喜欢更简单"的东西.如果它只是为了存储(而不是配置),那么在大多数情况下没有人会编辑这些文件.XML 也会比 lua var dump 占用更多空间(应该没有关系,除非你有很多数据).
Then again some users are scared from XML files (even though they are very readable, maybe too readable) and would prefer something "simpler". If it's just for storage (not configuration) then no one is going to edit those file anyway in most cases. XML will also take more space then lua var dump (shouldn't matter, unless you have a lot data).
我认为你不会在这里出错.暴雪正在使用 lua 进行存储,我非常喜欢它的工作方式.
I don't think you can go wrong here. Blizzard is using lua for storage and I quite like like how it works.
这篇关于Lua 与 XML 的数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!