Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua vs. XML for data storage

Tags:

c++

xml

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.

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.

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?

Blizzard, while using Lua for scripting the UI, still stores the layout in XML. Is the reason something that is only UI related?

What are the drawbacks of using Lua as a data storage language?

like image 926
Kornel Kisielewicz Avatar asked Dec 29 '09 03:12

Kornel Kisielewicz


2 Answers

Thank you for your answers so far! I'll take the liberty of summing up the points for future reference.

Drawbacks of using Lua to store data, compared to XML

  1. Security both when transferring data and when storing it, especially when receiving data from an unknown source
  2. Portability, and ease of access to data by other tools
  3. Less useful existing tools like XML Schema validators
  4. Less support for parsing the data
  5. Problems with circular references
  6. Less restrictive -- harder to enforce proper conventions, usage and design

Benefits of using Lua to store data, compared to XML

  1. Using a single language for both scripting and data (no need for separate files, no need for special cases when loading)
  2. Less code and dependencies because of the usage of a single language
  3. More verbose and (arguably) more human readable
  4. A lot more flexible, and extensible (it's a programming language after all)
  5. Data is "executed" and can be accessed when needed as it sits in the virtual machine
  6. Takes less space, especially if compiled to bytecode

If I missed something in the first list, please point it out!

like image 98
Kornel Kisielewicz Avatar answered Sep 20 '22 19:09

Kornel Kisielewicz


This might not be the kind of answer you expected, but it might help you make your decision.

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.

Also WoW actually stores addon "Saved Variables" in .lua files.

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.

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.

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).

I don't think you can go wrong here. Blizzard is using lua for storage and I quite like like how it works.

like image 40
Maiku Mori Avatar answered Sep 17 '22 19:09

Maiku Mori