我希望知道如何在Lua C API中从函数中获取多个返回值。
Lua代码:
function test(a, b)
return a, b -- I would like to get these values in C++
end
C++ 代码:(调用函数的部分)
/* push functions and arguments */
lua_getglobal(L, "test"); /* function to be called */
lua_pushnumber(L, 3); /* push 1st argument */
lua_pushnumber(L, 4); /* push 2nd argument */
/* call the function in Lua (2 arguments, 2 return) */
if (lua_pcall(L, 2, 2, 0) != 0)
{
printf(L, "error: %s\n", lua_tostring(L, -1));
return;
}
int ret1 = lua_tonumber(L, -1);
int ret2 = lua_tonumber(L, -1);
printf(L, "returned: %d %d\n", ret1, ret2);
我得到的结果是:
我期望的结果是:返回:4 4
需要对代码进行调整以实现预期结果。返回:3 4
int ret1 = lua_tonumber(L, -1);
should beint ret1 = lua_tonumber(L, -2);
- user1810087