Lua/C Calling lua function from C, with table parameter

Started by
10 comments, last by sprite_hound 13 years ago
Hi. I need to call a lua function from C, with a parameter that will be a table.
For example, I have this lua function:


function game.event(e)
--process event here
end


I want to pass a table parameter with different fields, for example:
e = { type="TOUCH_BEGIN", { x = 10, y=10} }
e = { type="ACCELEROMETER", { x=0.5, y=0.2, z=0.4} }

For function calls with single parameters, I do something like this:

lua_getglobal(L, "game"); // Get the table instance
lua_getfield(L, -1, "event"); // Get the function
lua_pushinteger(L, 1000); // pass 1000 as function parameter
lua_pcall(L, 1, 0, 0); //call lua function


The question is, how can I create a temp table, like the tables from the example, and pass it as a parameter?

Thanks.
Advertisement

The question is, how can I create a temp table, like the tables from the example, and pass it as a parameter?
Thanks.

Lua 5.1 Reference Manual



[quote name='martin_bfg10k' timestamp='1302099385' post='4795045']
The question is, how can I create a temp table, like the tables from the example, and pass it as a parameter?
Thanks.

Lua 5.1 Reference Manual
[/quote]

Well, with that, I can create an empty table, but I don't know hot to add diferent values to that table, like numbers, strings, and subtables, and later pass it as a parameter in the function call.
Thanks.
Programming In Lua
If people who knows how to do it explained to you how to do it right away (saving you hours of having to figure it out by yourself) the value of their lives would drop considerably because knowing that is mostly all they are worth.


OP: Let me know if you figure it out, I'm interested in knowing this too. The easy way, whenever that's possible.
[size="2"]I like the Walrus best.
Thanks for the links, at the end I figured out how to pass tables as function parameters.

With the previous lua example:


function game.event(e)
--process events here
end



Let's say I want to pass this table as parameter: {type="touchBegin", touch={x=10.0, y=20.0}}

the C code would be like this (with all values hardcoded for simplicity)



lua_getglobal(L, "game"); // Get the table "game"
lua_getfield(L, -1, "event"); // Get the function

lua_newtable(L); // new table that will be the parameters table for the function

lua_pushstring(L, "touchBegin");
lua_setfield(L, -2, "type"); // table.type = "touchBegin"

lua_newtable(L); // subtable that will hold the touch values
lua_pushnumber(L, 10.0);
lua_setfield(L, -2, "x"); // subtable.x = 10.0
lua_pushnumber(L, 20.0);
lua_setfield(L, -2, "y"); // subtable.y = 20.0
lua_setfield(L, -2, "touch"); // table.touch = subtable

//now, we have the table ready for use as a function parameter
lua_call(L, 1, 0); // call event() with 1 parameter (the parameter table wich is in the top of the stack)

lua_pop(L,1); // finally, pop the table "game", for keep the stack balanced



Hope this can help.
Awesome. Thanks for sharing!

I wonder if there could be some good method to pool them so one doesn't have to allocate them with each call... That would probably speed things up a little if that was necessary.
[size="2"]I like the Walrus best.
Well, I tested this on iphone, sending touch events, and it is fast enough. I didn't notice any performance issue. If I disable sending events to lua, or enable it, the game runs always at 60 fps.

If people who knows how to do it explained to you how to do it right away (saving you hours of having to figure it out by yourself) the value of their lives would drop considerably because knowing that is mostly all they are worth.


'saving you hours' See this is the problem, force feed me information without me having to lift a finger as I am so lazy.
The OP did not ask the question in a way which would be considered smart and also has information missing. How I interpret this is that the OP wants information force feed and this will not stop until the cycle is stopped in addition they also consider their time more important than mine as they can not be bothered to look for the information instead the first action is to ask.
If there is not an answer to questions about Lua in PiL or the manual or if you have at least looked and can not find the information then I would personally be more forth coming with answers. As it happens both the links posted (which the OP should really have read first) give the 'right way' of doing what was requested and now at least the OP will now where to look for the information.

Who do you think told everyone else how to use Lua?


OP: Let me know if you figure it out, I'm interested in knowing this too.
[/quote]
Well RTFM or ask questions and provide information showing you have at least made an effort to find the data.




[font=Times][size=2]Never assume you are[/font][font=Times][size=2] [/font][font=Times][size=2]entitled[/font][font=Times][size=2] [/font][font=Times][size=2]to an answer. You are not; you aren't, after all, paying for the service. You will earn an answer, if you earn it, by asking a substantial, interesting, and thought-provoking question one that implicitly contributes to the experience of the community rather than merely passively demanding knowledge from others[/quote][/font]
[font=Times][size=2]
[/font]
Yeah. Always ready to lay a hand don't you dmail? Your collaboration in this thread has been remarkable as always.

Well Done!
[size="2"]I like the Walrus best.

This topic is closed to new replies.

Advertisement