2
\$\begingroup\$

I'm new to Lua and I'm wondering, is it efficient to use Lua to store game data (such as monster's description, spells).

My problem is this: when I try to create a monster object every second, I have to run the Lua file and get the data repeatedly, which I think may be not efficient.

Before I try to use Lua, I use XML to store data: at the beginning of gameplay, I read the "monster.xml" file once, and keep it in the memory, and every time I need to create a monster, I just refer to it.

Is this XML approach more efficient than the Lua one ? Or are there other better solutions?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Lua can definitely be used as a configuration language, see this chapter in the manual. As for efficiency: Are you running into noticeable performance issues? Have you profiled your code? \$\endgroup\$
    – Eric
    Dec 30, 2012 at 13:48
  • \$\begingroup\$ actually, I am just hesitating which approach to go. \$\endgroup\$
    – YiFeng
    Dec 30, 2012 at 16:26
  • 1
    \$\begingroup\$ Define "efficient". \$\endgroup\$ Dec 30, 2012 at 19:42

4 Answers 4

17
\$\begingroup\$

Lua was actually designed to be an efficient data file format. It's original use was to load huge geographical data sets. Actual scripting/logic features came later. Even with all the new scripting features, it is still used by its original authors to store and load large data sets.

That is not to say that Lua is the most efficient choice. That will depend on a number of factors, but in general tightly packed binary data is the most efficient (disk access times are much longer than processing/parsing time).

Since you said you are having trouble deciding, here's the best answer I can give you: it doesn't matter. It really, really doesn't matter. Pick which ever you find easiest and most comfortable to use, implement it, and move on to making your actual game. "Best" solutions always lose out to "actually working" solutions in the real world. If you stop and worry and get paralyze over every "which is better" question you have during development, you will never ever actually have a working product. If you choose wrong, you can always go back and change the code later.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ +1 '"Best" solutions always lose out to "actually working" solutions in the real world.' \$\endgroup\$
    – Engineer
    Dec 31, 2012 at 2:19
  • \$\begingroup\$ Very inspiring! \$\endgroup\$
    – YiFeng
    Dec 31, 2012 at 15:59
  • \$\begingroup\$ "If you stop and worry and get paralyzed over every 'which is better' question you have during development, you will never actually have a working project" No truer words were ever spoken. I do that more often than I would like and it wears me out on every project I start like this. +1 \$\endgroup\$
    – Jax
    Jul 13, 2016 at 19:54
4
\$\begingroup\$

I think that the problem here is not lua or xml but the design. IMO you should use a fatory pattern coupled with a prototype. Your lua script should be used to create a new prototype of monster, then your factory will create new "instances" of this monster (cloning the prototype). This way you have to run your script only once.

However, if all of your monsters have different stats then you can run an other script per monster to select the prototype to use and setup these stats. This avoid to rerun the common part of the script multiple times.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ You describe caching as an answer, as if the question weren't already prematurely optimising. :p \$\endgroup\$
    – Anko
    Dec 31, 2012 at 9:09
2
\$\begingroup\$

I have a small library called Luatable that I use to do this very thing. It loads a Lua file and keeps it in memory, then allows you to access the data with (I think) a very intuitive interface.

Here's a small example.

Lua code that describes a monster:

monster = {
    health = 100,
    name "Troll",
    lootTypes = { "gold", "chain_armor" }
}

Accessing this data from C++:

Luatable::Value definition;
Monster monster;
definition.loadFromFile("Troll.lua");

troll.health = definition["monster"]["health"].asInt();
troll.name = definition["monster"]["name"].asString();
Luatable::Array& lootTypes = definition["monster"]["lootTypes"].asArray();
for (int i = 0; i < lootTypes.size(); ++i)
{
    monster.addLootType(lootTypes[i].asString());
}

There are more examples in the readme on the Bitbucket page.

\$\endgroup\$
0
\$\begingroup\$

It does not matter!

You said you load the file at the beginning of gameplay. I infer that it's being loaded during the loading screen. If you have a sufficiently slow HDD (e.g. in a low end laptop from 2008), parsing a file is still faster than reading it from disk, no matter which format you use.

Even if you later have to change it, this is where proper design comes in. Programming practices like TDD are encouraged because they make you split classes correctly. In this case you have a class that creates a monster from data, and if you later find out it needs to be faster, you simply replace it with a different class.

\$\endgroup\$

You must log in to answer this question.

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