Loop through all Lua global variables in C++ [duplicate]

会有一股神秘感。 提交于 2019-11-30 21:06:12

Okay I solved it.

lua_pushglobaltable(L);       // Get global table
lua_pushnil(L);               // put a nil key on stack
while (lua_next(L,-2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2)
  name = lua_tostring(L,-2);  // Get key(-2) name
  lua_pop(L,1);               // remove value(-1), now key on top at(-1)
}
lua_pop(L,1);                 // remove global table(-1)

When lua_next() can't find more entries the key name is popped leaving the table on top(-1).

Next problem would be to distinguish my own globals from the rest of the table entries...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!