I would like to know how to get multiple return values from function in Lua C API.
Lua Code:
function test(a, b)
return a, b -- I would like to get these values in C++
end
C++ Code: (part where it calls the function)
/* 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);
The result I get:
returned: 4 4
The result I expect:
returned: 3 4
int ret1 = lua_tonumber(L, -1);should beint ret1 = lua_tonumber(L, -2);