我想说我想在Lua C API的嵌套表中设置一个值(例如函数)。
-- lua.lua
glob = {
nest = {
-- set value in here
}
}
我如何设置堆栈以访问内部表?
是否只是多次调用gettable
然后调用settop
,如下面的代码所示?
lua_pushstring(state, "glob");
lua_gettable(state, -1);
lua_pushstring(state, "nest");
lua_gettable(state, -1);
lua_pushcclosure(state, &func, 0);
lua_settop(state, "funkyfunc");
lua_pop(state, 2);
答案 0 :(得分:1)
此代码将glob.nest.name
设置为C函数:
lua_getglobal(state, "glob");
lua_getfield(state, -1, "nest");
lua_pushcclosure(state, &func, 0);
lua_setfield(state, -1, "name");
要将其他字段添加到glob.nest
,请继续:
...
lua_pushcclosure(state, &anotherfunc, 0);
lua_setfield(state, -1, "anothername");