4

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.

1 Answer 1

7

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 */
}
2
  • Oh thank you. I thought this is just a status if the function call was alright.
    – Objective
    Oct 23, 2013 at 14:35
  • 2
    @Objective My humble suggestion would be to start checking in references instead of guessworking next time :). Oct 23, 2013 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.