简体   繁体   中英

How to get returned table from Lua function in C++?

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

My code:

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!

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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