0
\$\begingroup\$

Possible Duplicate:
How to choose how to store data?

The game I'm designing will be mainly written in a high level scripting language (leaning towards either Lua or Squirrel) with a C++ core. In addition to scripts I'm also going to need different data files. Many data files will be for static information such as graphical assets and monster types. I'd also want to create and update data files at runtime for user information like option settings and game saves.

Can I get away with using plain script files (i.e. .lua or .nut files) for my data files, or is it better to use dedicated markup formats like XML or YAML?

If I use script files, loaded separately from my true scripts, then I wouldn't need an extra library to read those files. Scripting languages like Lua also have table syntax that lend themselves towards data definition. On the other hand I'd have to write my own schema check code. These languages also don't seem to support serialization "out of the box" like the markup format libraries do.

\$\endgroup\$
5
  • \$\begingroup\$ Which one you use is entirely up to you. Any of these will serve your needs. So what matters is how important the various features are for your needs. You know your needs better than we do. How much do you value runtime validation of data? If you wrote these files, are you concerned that they may become invalid? How much does serialization matter to your needs? Etc. You can answer these questions; we cannot. \$\endgroup\$ Dec 5, 2012 at 1:00
  • 1
    \$\begingroup\$ We can't really tell you what to choose, but the duplicate linked can tell you how to choose. Personally, I'd go with XML if you're wanting end users to be able to read/edit the data, otherwise just go with serialized data. But it's up to you. \$\endgroup\$
    – House
    Dec 5, 2012 at 1:00
  • \$\begingroup\$ The How to choose how to store data? question was interesting, though I'm more curious about the specific tradeoffs between defining and loading data from an XML file vs. a Lua file. \$\endgroup\$
    – Aaron
    Dec 5, 2012 at 1:06
  • 1
    \$\begingroup\$ @Aaron: You know what the tradeoffs are; you mentioned them in your question. So what exactly are you looking for, validation that there aren't any other factors? \$\endgroup\$ Dec 5, 2012 at 1:08
  • \$\begingroup\$ Not really a duplicate, the linked question is about data files vs. databases, this one is about data files vs. scripts. \$\endgroup\$ Dec 5, 2012 at 9:08

2 Answers 2

3
\$\begingroup\$

I recommend using a markup format rather than putting all of your data in script files. The main reason being that editing and generating JSON or XML will be easier than generating comparable Lua or Squirrel code due to the availability of JSON and XML libraries. Another reason is it allows you more flexibility by keeping the data in its own format, and the scripts in their own format:

  • If you decide to change scripting languages your data will still be usable.
  • If you decide to not use scripting at all your data will still be usable.

In fact, you could take things to the other extreme and even store Lua or Squirrel code chunks in your data files -- but this is probably something you would want to avoid to keep your data and scripts clean.

A note about JSON and Squirrel: Squirrel 3.0 supports JSON-like declaration of objects, so you could actually use raw JSON strings and the Squirrel VM can parse them into tables. I see this as an advantage of using both technologies together.

\$\endgroup\$
1
  • \$\begingroup\$ Humans know squirrels like ninjas know flash powder. \$\endgroup\$
    – Engineer
    Dec 6, 2012 at 21:23
1
\$\begingroup\$

Can I get away with using plain script files (i.e. .lua or .nut files) for my data files...

Sure, that's a valid option.

I've worked on a game where a good chunk of the data files were written in Lua, nobody got hurt too bad, and it had some benefits:

  • Lua table syntax is quite human readable, even if it has some quirks.
  • One "parser" for everything (actually a bit more than a parser, the fantastic LuaBind). This can actually be a good thing: smaller code base, less things to maintain.
  • You get "programmable data": it's easy to add a little routine in a data file e.g. to generate lists of things or parameterize some parts. I'm quite sure this sounds horrible to some ears, but it can actually be really useful for rapid prototyping. Just use it wisely, and refactor away the parts that get too dirty.

...or is it better to use dedicated markup formats like XML or YAML?

That is yours to tell, there is no such thing as the best option.

Of course there are some downsides to the data-as-scripts approach, Zack The Human mentioned some of them in his answer. I'd add that tool support is something very important to consider before going this way. There are good tools for editing regular data formats (XML, JSON), but you'll probably have to roll your owns if you plan to use a scripting language.

As usual the final choice is yours, just go the way that seems to suit your project the best.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .