Sign in to follow this  

Data to/from Lua

This topic is 2850 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

Hi all, I'm using Lua (LuaPlus to be precise) to farm out functionality to scripts from C++ code. One problem I'm seeing is wanting to to pass and receive composite C data types. For example I call a handler to spawn effects - I want to send over a few vectors to Lua, and let lua possibly send some vectors back. I'm curious to see how other people handle this without large parameter lists (e.g. the vector would be sent as x,y,z params). A solution I'm trying is to use a Lua scratch global to store/read the data (a bit like this), but this feels somewhat clunky... :(

Share this post


Link to post
Share on other sites
Also suggest using Luabind, its powerful binding functionality allows you to seamlessly transition data and code from Lua and C++. If your concerned about Boost dependency you can use MLuabind, which is a light weigh binding library in the same vein.

If your into performance but don't want to write all those binding functions by hand, there is OOLua which from the benchmarks I've seen the fastest binding library out so far.

Good Luck!

-ddn

Share this post


Link to post
Share on other sites
Cheers people...
LuaPlus has some nice binding facilities - I was wondering about others' ideas on passing data rather than the binding technicalities - the 'scratch table' thing seemed a bit horrible and my Lua is somewhat rusty nowadays ;)

Share this post


Link to post
Share on other sites
You can pass in a Lua table as a parameter into the C function but you'll have to parse it to extract the key/value pairs yourself if your not going to use a binding library. Or you can pass C object as user data which, but you'll have to make sure that type information is included somehow if you want to cast it back onto the C side to the appropriate object, (binding libraries do this for you so its like magic :D)

like something like this :

local vector={1,2,3}; --(x,y,z)

myCFunction(vector);

and the C function would look something like this



int myCFunction( lua_State *vm )
if (lua_istable(vm,1))
{
/* table is in the stack at index 't' */
lua_pushnil(vm); /* first key */
while (lua_next(vm, -2) != 0) {
float value = lua_tonumber(vm, -2); //value
int key = lua_tointeger(vm, -1);//key

// removes 'value'; keeps 'key' for next iteration
lua_pop(vm, 1);
}
}
return 0;
}




Good Luck!

-ddn

Share this post


Link to post
Share on other sites
As much as I know it is performace wise not the best idea to handle vectors as tables (= {x,y,z}), because creating and destroying tables has a major impact on performace (memory allocation of table is somewhat expensive: link). This shouldn't be an issue as long as you just need this to pass some paramters to your C++ interface, but doing some more frequent and complex calculations could be a problem.

I try to avoid passing vectors as tables and started refactoring my lua code to pass vectors as single x,y,z parameters. When I need larger parameter blocks I try to create or (reuse a cached) single parameter table holding all parameters necessary.

Share this post


Link to post
Share on other sites

This topic is 2850 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.

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