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 */
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh thank you. I thought this is just a status if the function call was alright.
@Objective My humble suggestion would be to start checking in references instead of guessworking next time :).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.