简体   繁体   中英

return several parameter from lua C function

I want to get several parameters in Lua from a C function. I tried to push several arguments on the lua stack:

static int myFunc(lua_State *state)
{
    lua_pushnumber(state, 1);
    lua_pushnumber(state, 2);
    lua_pushnumber(state, 3);

    return 1;
}

and call it in Lua like this:

local a,b,c = myFunc()

Unfortunately b and c values are nil. I dont want to write a function for every value I need but to take advantage of Luas capabilities to retrieve several arguments from a function.

The return value of the C function is the number of values returned.

Change it to return 3; and you're good to go.

Here, have a reference from Programming in Lua :

static int l_sin (lua_State *L) {
  double d = lua_tonumber(L, 1);  /* get argument */
  lua_pushnumber(L, sin(d));  /* push result */
  return 1;  /* number of results */
}

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