繁体   English   中英

如何从C访问嵌套的Lua表

[英]How to access nested Lua tables from C

假设我想在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);

这段代码将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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM