ice wrote:
> How can I access tables within tables? My data is structured as so:
> entity =
> {
> s =
> {
> pos =
> {
> trType = 0,
> trTime = 0,
> trDuration = 0,
> trBase = {128, 128, 128}
> trDelta = {0, 0, 0}
> }
> },
> r =
> {
> currentOrigin = {256, 256, 256}
> }
> }
> I need to be able to access all of that data. Would I just repeatedly
> call lua_pushstring with the new key and then call lua_gettable? What
> about accessing the array data? Any help would be appreciated.
Here are a couple of examples complete with stack comments. I assume
that 'entity' is an index in the LUA_GLOBALINDEX of the function which
contains this code.
/* access (entity.s.pos.trDuration) */
lua_getglobal(L, "entity"); /* entity */
lua_getfield(L, -1, "s"); /* entity s */
lua_getfield(L, -1, "pos"); /* entity s pos */
lua_getfield(L, -1, "trDuration"); /* entity s pos trDuration */
int n = lua_tonumber(L, -1);
/* access (entity.r.currentOrigin[1]) */
lua_getglobal(L, "entity"); /* entity */
lua_getfield(L, -1, "r"); /* entity r */
lua_getfield(L, -1, "currentOrigin"); /* entity r currentOrigin */
lua_rawgeti(L, -1, 1); /* entity r currentOrigin [1] */
int n = lua_tonumber(L, -1);
Note that lua_getfield(), lua_getglobal(), and lua_gettable() may
trigger a metamethod, but that lua_rawgeti() will not.
-Mark