Using LUA for Configuration Files

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Using LUA for Configuration Files

Daniel Krenn
Hi,

I had some trouble earlier today trying to get a value from a table
from C. Is there a simple way to do this (maybe I did something wrong,
or I've missed something??)

I've tried code similar to lua_getglobal(L, "table.value"); which
doesn't seem to work. The work around I've used for the time being is
as follows:

double Config::GetValue(const char *name, double defaultValue)
{
  char string[80] = "tempN=";

  strcat(string, name);
  lua_dostring(L, string);

  lua_getglobal(L, "tempN");
  int top = lua_gettop(L);
  if(lua_isnumber(L, top))
  {
    return lua_tonumber(L, top);
  }

  return defaultValue;
}

Can anyone offer me a better solution?

Thanks,
Daniel
[hidden email]



Reply | Threaded
Open this post in threaded view
|

Re: Using LUA for Configuration Files

Alex Sandro Queiroz e Silva
On Mon, 5 Feb 2001, Daniel Krenn wrote:

> I had some trouble earlier today trying to get a value from a table
> from C. Is there a simple way to do this (maybe I did something wrong,
> or I've missed something??)

Hallo,
	Let t be the index of the table in the stack. One may write:

lua_pushstring(lua_state, "key");
lua_gettable(lua_state, t);

and then the value will be pushed in the stack.

  --Alex	[hidden email]		Lab. de Computação Gráfica/UFC
+----------------------------------------------------------------------------+
|"Minha força vem da solidão. Não tenho medo das chuvas tempestuosas nem das |
| grandes ventanias soltas, pois eu também sou o escuro da noite."	     |
|	- Clarice Lispector						     |
+----------------------------------------------------------------------------+


Reply | Threaded
Open this post in threaded view
|

Re[2]: Using LUA for Configuration Files

Daniel Krenn
ASQeS> Hallo,
ASQeS>         Let t be the index of the table in the stack. One may write:

ASQeS> lua_pushstring(lua_state, "key");
ASQeS> lua_gettable(lua_state, t);

ASQeS> and then the value will be pushed in the stack.

I considered doing that, which would work fine for a single layered
table, but if we want to have more nested configurations like:

character =
{
  name = "Test"
  startPositions =
  {
    pos1 = {x=50, y=10},
    pos2 = {x=30, y=20},
  }
}

then you would need to have the config program's GetValue() accessor handle
multiple keys / sub-keys wouldn't you?.

If we want to make a call like
GetValue("character.startPositions.pos1.x", 0), does this mean we have
to iterate through the key and break it into sub-keys before we get
down to the value??? Maybe I'm missing something - I haven't used LUA
a lot, so this is quite likely :)

Thanks,
Daniel
[hidden email]



Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Using LUA for Configuration Files

John Belmonte-2
Daniel Krenn wrote:
> If we want to make a call like
> GetValue("character.startPositions.pos1.x", 0), does this mean we have
> to iterate through the key and break it into sub-keys before we get
> down to the value??? Maybe I'm missing something - I haven't used LUA
> a lot, so this is quite likely :)

If you don't mind a little CPU churning, something like the following is nice and simple.  Just
prefix the value you want to retrieve with "return ":

    lua_dostring(L, "return character.startPositions.pos1.x")

-John