lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all,
 
I have a question regarding to accessing table value.
Assuming I have two table in Lua (as an initial script).
 
In Lua:
global_objects = {
 effectype = {
  water = {99, 3330, 88},
  air = {1, 2, 8}
 }
}
simpleTable = { effectype = 1 }

In my C++ code is something like this,

// this accpet integer index
void ReadTableVar(const char * table, int index, void *value)
{
 lua_getglobal(mLuaState, table);
 if(!lua_istable(mLuaState, -1)) {return;}
 if(lua_isnil(mLuaState, -1)) {return;}
 FindTableValue(mLuaState, index); // this loops though the table until "index"
 *((double *)value) = (double)lua_tonumber(mLuaState, -1);
 
// this accept string index
void ReadTableVar(const char * table, const char * index, void *value)
{
 lua_getglobal(mLuaState, table);
 if(!lua_istable(mLuaState, -1)) {return;}
 if(lua_isnil(mLuaState, -1)) {return;}
 FindTableValue(mLuaState, index); // this loops though the table until "index"
 *((double *)value) = (double)lua_tonumber(mLuaState, -1);
 
using this code, it works if call is,
ReadTableVar("simpleTable", "effectype", &value);
However, it does not work, if the call is,
ReadTableVar("global_objects.effectype.water", 1, &value);
When I trace through the code, I find that !lua_istable is true,
as a result, it returns.
I tried this in the lua interactive enviroment, it actually tells me that
global_objects.effectype.water is a table.
I have no idea why my C++ code does not evaluate global_objects.effectype.water
as a table. Should I actauly need to separate global_objects effectype and water
and explicitly do the table traverse?
 
(Currently I am using Lua 4.0.1)
Thanks in advance.
Nelson



Post your free ad now! Yahoo! Canada Personals