Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 martin_bfg10k   Members   -  Reputation: 198

Like
0Likes
Like

Posted 06 April 2011 - 08:16 AM

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.

Sponsor:

#2 djofdifjpodjfpodkpofdpofpd   Members   -  Reputation: 120

Like
0Likes
Like

Posted 06 April 2011 - 01:39 PM

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




#3 martin_bfg10k   Members   -  Reputation: 198

Like
0Likes
Like

Posted 06 April 2011 - 02:47 PM


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


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.

#4 __sprite   Members   -  Reputation: 461

Like
0Likes
Like

Posted 06 April 2011 - 06:31 PM

Programming In Lua

#5 owl   Banned   -  Reputation: 364

Like
0Likes
Like

Posted 06 April 2011 - 08:26 PM

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.
I like the Walrus best.

#6 martin_bfg10k   Members   -  Reputation: 198

Like
2Likes
Like

Posted 07 April 2011 - 03:28 AM

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.

#7 owl   Banned   -  Reputation: 364

Like
0Likes
Like

Posted 07 April 2011 - 04:01 AM

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.
I like the Walrus best.

#8 martin_bfg10k   Members   -  Reputation: 198

Like
0Likes
Like

Posted 07 April 2011 - 04:12 AM

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.

#9 djofdifjpodjfpodkpofdpofpd   Members   -  Reputation: 120

Like
-1Likes
Like

Posted 07 April 2011 - 06:24 AM

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.

Well RTFM or ask questions and provide information showing you have at least made an effort to find the data.




Never assume you areentitledto 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





#10 owl   Banned   -  Reputation: 364

Like
0Likes
Like

Posted 07 April 2011 - 12:24 PM

Yeah. Always ready to lay a hand don't you dmail? Your collaboration in this thread has been remarkable as always.

Well Done!
I like the Walrus best.

#11 martin_bfg10k   Members   -  Reputation: 198

Like
0Likes
Like

Posted 07 April 2011 - 12:57 PM

As a personal rule, if I know how to solve something that somebody asked, I just try to help him, telling how I solved that problem.
But if I have to do research just to help someone, then I just ignore the post.
I don't have any problem sharing knowledge of something that I know.

#12 __sprite   Members   -  Reputation: 461

Like
0Likes
Like

Posted 10 April 2011 - 12:28 PM

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.


Heh, sorry for the random link. To be honest it's been too long since I've done any raw lua stuff in c to have really commented more usefully. I just tend to use luabind for everything nowadays.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS