Lua table in C++ dll

我的 Lua 表格看起来像这样:

qt={
    bid_number=20;
    ask_number=20;
    bid=20 个元素的表格;
    ask=20 个元素的表格;
}

所以 #qt=0,我想把这个表格发送到 C++ dll 并且操作它的字段。 我怎样做呢?目前我只能在 C++ dll 中操作这样的表格:tbl={a,b,c}。我是这样做的:

static int forLua_SumArray (lua_State* L) {    // Get the length of the table (same as # operator in Lua)
    int n = luaL_len(L, 1);
    double sum = 0.0;

    // For each index from 1 to n, get the table value as a number and add to sum
    for (int i = 1; i <= n; ++i) {
      lua_rawgeti(L, 1, i);
      sum += lua_tonumber(L, -1);
      lua_pop(L, 1);
    }

    lua_pushnumber(L, sum);
    return 1;
}

请帮我开始操作更复杂的表格。

点赞
用户9753452
用户9753452
2018-08-31 15:33:41
用户10252378
用户10252378

将给定的可接受序号的 Lua 值转换为 C 字符串。Lua 值必须是字符串或数字,否则函数将返回 null。

const char *lua_tolstring (lua_State *L, int index, size_t *len);

然后 lua_tostring 也将堆栈中的实际值更改为字符串。 luaL_checkstring 调用 lua_tolstring

2018-08-31 15:42:01