Sign in to follow this  
Daft

Lua - Passing Tables from Lua to C++

Recommended Posts

Hey all, ive come along way from when i didnt know anything about lua, but now im stuck on something small...( i think! ). Anyways, i know how to pass strings and doubles from lua to c++ functions ( in the parameters ) but is it at all possible to pass a whole table from lua to c++ then disect it in c++? any help would rock ~danny

Share this post


Link to post
Share on other sites
i checked that, it told me what i already knew ( how to pass multiple variables and such ) but what im trying to basically is send a struct thru lua to c++.

Share this post


Link to post
Share on other sites
You can't get a table from a lua_State directly, since the only way to manipulate tables is by calling lua_settable and lua_gettable on a lua_State. I think this is intentional, since Lua wouldn't be able to know when to garbage collect a table if you could pass it around in C/C++.

Share this post


Link to post
Share on other sites
As Barius said, you can't really just get a whole lua table. You would need to manually get the data from the table using the Lua API and manually store it in a class or struct. You could write a utility function to do this for you as well.

Share this post


Link to post
Share on other sites
Quote:
is it at all possible to pass a whole table from lua to c++ then disect it in c++?

No, not in the traditional sense. The only way to work with tables is through the stack. If this is what you really want, I recommend that you write a utility function that will take a Lua table, extract all the values and build an array/dictionary with said values (and even their keys). It's probably easier than it seems. You could also use a 3rd-party wrapper/SDK/toolkit, but that's the easy way out :P

Quote:
Anyways, i know how to pass strings and doubles from lua to c++ functions ( in the parameters ) but is it at all possible to pass a whole table from lua to c++ then disect it in c++?


From reading your query as a whole, I get the feeling that you are simply looking to do:

--lua5
MyTable = {x = 5, y = 10, w = 100, h = 50}
MyCPlusPlusFunction(MyTable)


instead of:

...
MyCPlusPlusFunction(MyTable.x, MyTable.y, MyTable.w, MyTable.h)


...... If that's the case, then here's an example:


int MyCPlusPlusFunction(lua_State *LuaVM)
{
// the following code is purposely simplified and unoptimized
int x,y,w,h;

lua_pushstring(LuaVM, "x"); // let Lua know what we're looking for
lua_gettable(LuaVM, 1); // let Lua know where we're looking for it
x = lua_tonumber(-1); // get requested value (which was placed on top of the stack by lua_gettable)
lua_pop(LuaVM, 1); // clean up

lua_pushstring(LuaVM, "y");
lua_gettable(LuaVM, 1);
y = lua_tonumber(-1);
lua_pop(LuaVM, 1);

// ... continue for the rest of the value

return 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