如何从C++中的Lua函数获取返回的表格?

我正在尝试弄清楚如何从C++中的Lua函数获取返回的表格。

我的代码:

if (lua_pcall(L,010)) {
        std::cout << " ERROR:" << lua_tostring(L,-1) << std::endl;
}
vector<float> vec;

if (lua_istable(L,-1) {

    //如何将表复制到vec?
}

如果表的大小未知,我该如何将返回的表格复制到向量中? 谢谢!

点赞
用户5224286
用户5224286

我认为我找到了使用 lua_next 的方法。

lua_getglobal(L, name);

if (lua_pcall(L, 0, 1, 0)) {
        std::cout << "ERROR : " << lua_tostring(L, -1) << std::endl;
}
vector<float> vec;

if (lua_istable(L, -1)) {

   lua_pushvalue(L, -1);
   lua_pushnil(L);

   while (lua_next(L, -2))
   {

        if (lua_isnumber(L, -1))
        {
            vec.push_back(lua_tonumber(L, -1));
        }
        lua_pop(L, 1);
    }
    lua_pop(L, 1);
}
2018-06-28 02:54:36