# How to transfer a luabind::object between two lua states

I store an object by a function call from Lua to C++ in first script in my application and after that I'll try to pass that object to other scripts and vice versa.

A part of script class in C++:

void script::set_property(lua_State* L, luabind::argument const& value)
{
this->property_field = luabind::object(L, value);
}

luabind::object script::get_property(lua_State* L)
{
return this->property_field;
}


Then in my script which is Script1.lua I do this:

script1:set_property(a table or a userdata or a string)
print(type(script1:get_property()))


The problem is, values sent by scripts are distinct in different Lua states because every script in my application has its own lua_State. The above code works fine when I call script::get_property from Script1.lua.

Since all of my scripts have access to other scripts in their contexts when I try to do this in other scripts like Script2.lua or Script3.lua or others:

print(type(script1:get_property()))


It won't print data type here properly and it'll return a number instead, which is a wrong result and the reason is script1:get_property() is returning a value that is only understood by script1 and not in other scripts.

I understand values that have been registered in script1 are only registered into that script's stack and registry table particularly and no one else, but my question is, is there any way to transfer these values between different Lua states?

Thanks.

• What we're really interested in is why you need to transfer variables between states? Its far more likely that your overall architecture is flawed. (And by the sounds of it you're using lua too far into your stack) – Matt D Sep 25 '13 at 1:55
• @MattD Each script has a property which it's accessible by others. This makes scripts to be able to share data at run-time and the reason that I used luabind::object is for its generic behavior. The whole thing that I've presented here is just an example of what I'm trying to express because the actual project is more complex than this. And, I don't think there would be any flaw in here. – MahanGM Sep 25 '13 at 10:10

• Well as far as I know my problem is with the way that luabind::object is traveling. Besides, I don't want to serialize my data in Lua directly rather I want to do it in C++ which it'll bring the need for casting. Currently the data provided by this->property_field is wrong because the actual value is in the first script's stack and it's not going to transfer itself to other scripts' stacks when I try to get it from them. – MahanGM Sep 28 '13 at 16:55