Lua - Passing Tables from Lua to C++

Started by
5 comments, last by swordfish 18 years ago
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
Advertisement
This site has a brief tutorial on Lua, with integration of Lua into C++.
Your question is answered there.
Using your brain doesn't hurt at all.
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++.
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++.
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.
It is foolish for a wise man to be silent, but wise for a fool.
There are a bunch of C++ codewrappers available for lua. Try using one of those to bind your C++ class/struct to lua, it usually helps.

http://lua-users.org/wiki/LuaAddons
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;}
http://blog.protonovus.com/

This topic is closed to new replies.

Advertisement