• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
martin_bfg10k

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

11 posts in this topic

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:

[code]
function game.event(e)
--process event here
end
[/code]

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:
[code]
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
[/code]

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

Thanks.
0

Share this post


Link to post
Share on other sites
[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.
[/quote]
[url="http://www.lua.org/manual/5.1/manual.html#lua_newtable"]Lua 5.1 Reference Manual[/url]


0

Share this post


Link to post
Share on other sites
[quote name='dmail' timestamp='1302118792' post='4795169']
[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.
[/quote]
[url="http://www.lua.org/manual/5.1/manual.html#lua_newtable"]Lua 5.1 Reference Manual[/url]
[/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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Thanks for the links, at the end I figured out how to pass tables as function parameters.

With the previous lua example:
[code]

function game.event(e)
--process events here
end[/code]


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)

[code]

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

[/code]

Hope this can help.
2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='owl' timestamp='1302143181' post='4795316']
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.

[/quote]
'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 [url="http://www.catb.org/~esr/faqs/smart-questions.html"]smart[/url] and also has information [url="http://www.catb.org/~esr/faqs/smart-questions.html#before"]missing[/url]. 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?

[quote]
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.




[quote][font=Times][size=2]Never assume you are[/size][/font][font=Times][size=2] [/size][/font][font=Times][size=2][i]entitled[/i][/size][/font][font=Times][size=2] [/size][/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][/size][/font]
[font=Times][size=2]
[/size][/font]
-1

Share this post


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

Well Done!
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='owl' timestamp='1302143181' post='4795316']
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.
[/quote]

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0