从C访问Lua子表字段

我想在Lua中存储模型描述并将其读成非后续的.所有数据都以增量顺序存储

device_pins = 
{
    {is_digital=true, name = "A", number = 1, on_time=15000000000, off_time=22000000000},
    {is_digital=true, name = "B", number = 2, on_time=15000000000, off_time=22000000000},
    {is_digital=true, name = "C", number = 3, on_time=15000000000, off_time=22000000000}    
}

它与C结构中存储数据的方式大致相同.所以我想循环遍历device_pins,比如device_pins [1..3]并访问子表值,就像我在Lua中做的那样:
device_pins [1] .name等到目前为止,我可以遍历表但无法访问子表字段,我试过lua_getfield但似乎不适合这里

lua_getglobal (luactx, "device_pins");
if (0 == lua_istable(luactx, 1))
{
    out_log("No table found");
}
lua_pushnil(luactx);
while (lua_next(luactx, 1) != 0) 
{    
out_log(lua_typename(luactx, lua_type(luactx, -1)));   
lua_pop(luactx, 1);
}

最佳答案 试试这个:

lua_getglobal (luactx, "device_pins");
if (0 == lua_istable(luactx, -1))
{
    out_log("No table found");
}
for (i=1; ; i++)
{    
    lua_rawgeti(luactx,-1,i);
    if (lua_isnil(luactx,-1)) break;
    out_log(luaL_typename(luactx, -1));   
    lua_getfield(luactx,-1,"name");
    out_log(lua_tostring(luactx,-1));   
    lua_pop(luactx, 2);
}

如果使用相对(=负)堆栈位置,则更容易跟踪堆栈内容.

点赞