lua-users home
lua-l archive

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


> The way I would like this to work is something like:
> 
> lua_getglobal(pLuaState, "Enemy.Hitpoints");

Try this, which works for any depth:

static void mygetglobal(lua_State* L, const char* s) {
  const char* limit = s + strlen(s);
  lua_pushvalue(L, LUA_GLOBALSINDEX);
  for (;;) {
    const char* e = strchr(s, '.');
    lua_pushlstring(L, s, (e ? e : limit) - s);
    lua_gettable(L, -2);
    lua_remove(L, -2);
    if (e == NULL || lua_isnil(L, -1)) break;
    s = e + 1;
  }
}

(based on http://lua-users.org/lists/lua-l/2003-03/msg00081.html )