Jump to content
  • Advertisement
Sign in to follow this  
Daft

Lua - Passing Tables from Lua to C++

This topic is 4479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
This site has a brief tutorial on Lua, with integration of Lua into C++.
Your question is answered there.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!