如何从C ++中的Lua函数获取返回的表? [英] How to get returned table from Lua function in C++?

查看:421
本文介绍了如何从C ++中的Lua函数获取返回的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何从C ++中的Lua函数获取返回的表.

I'm trying to figure out how to get the returned table from the Lua function in C++.

我的代码:

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

if (lua_istable(L, -1) { 

    //how to copy table to vec?
}

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

How can I copy the returned table to vector if the table size is unknown? Thanks!

推荐答案

我想我已经找到了使用lua_next的方法.

I think I found out how to do it using 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);
}

这篇关于如何从C ++中的Lua函数获取返回的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆