LuaGlue icon indicating copy to clipboard operation
LuaGlue copied to clipboard

Passing a table from C++ to Lua

Open ehrhart opened this issue 11 years ago • 3 comments

Hello,

Is there currently any way to create a table in C++ and pass it to a Lua function?

I'm actually trying to convert a std::vector (or a std::list) so I can pass it to my Lua function

Thanks

ehrhart avatar Sep 17 '14 11:09 ehrhart

Hmm. There is no api currently to take a container and convert it to a lua table.

I've thought about this a little, and i can go two ways.. Either just auto-wrap std containers directly, or have stack functions automatically take the container and turn it into a table...

Wrapping the containers would make it so you can actually change the container directly in lua, not wrapping would mean you can't change the container, but it might be faster to access the data in lua because it doesn't have to bother with metatables and metamethods.

Tomasu avatar Sep 18 '14 03:09 Tomasu

Both ways are interesting, the latter one would work with my case. You could also have a LuaTable wrapper where you can put() basic variables or shared_ptrs

ehrhart avatar Sep 18 '14 11:09 ehrhart

Both ways are more or less equivalent if all you want to do is pass a stl container into lua as a table. The first would be implemented as a userdata object implementing the __(new)index metamethods. The second would just create a table and populate it with the items from the stl container.

As for a lua table wrapper, there is already a class (LuaGlueLuaTable) that could be extended to support that. And I might at some point. Right now it only exists to support passing tables back into C++ from lua.

Tomasu avatar Sep 18 '14 12:09 Tomasu