# Is it efficient to use Lua to store game data?

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?

• 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? – Eric Dec 30 '12 at 13:48
• actually, I am just hesitating which approach to go. – YiFeng Dec 30 '12 at 16:26
• Define "efficient". – Trevor Powell Dec 30 '12 at 19:42

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.

• +1 '"Best" solutions always lose out to "actually working" solutions in the real world.' – Engineer Dec 31 '12 at 2:19
• Very inspiring! – YiFeng Dec 31 '12 at 15:59
• "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 – JDSweetBeat Jul 13 '16 at 19:54

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.

• You describe caching as an answer, as if the question weren't already prematurely optimising. :p – Anko Dec 31 '12 at 9:09

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;

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