Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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, 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?

Thanks !

share|improve this question
1  
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
1  
Define "efficient". –  Trevor Powell Dec 30 '12 at 19:42

3 Answers 3

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

share|improve this answer
    
+1 '"Best" solutions always lose out to "actually working" solutions in the real world.' –  Nick Wiggill Dec 31 '12 at 2:19
    
Very inspiring! –  YiFeng Dec 31 '12 at 15:59

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.

share|improve this answer
2  
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;
definition.loadFromFile("Troll.lua");

troll.health = definition["monster"]["health"].asInt();
troll.name = definition["monster"]["health"].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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.