[luabind] How to pass value from C++ to lua function by reference?
Brought to you by:
arvidn,
daniel_wallin
From: 1 1 <roa...@gm...> - 2014-08-03 19:10:04
|
When programming on C++, you can do the following: void byReference(int &y){ y = 5;} int main(){ int x = 2; // x = 2 byReference(x); // x = 5} How to do the same using luabind? Luabind docs says <http://www.rasterbar.com/products/luabind/docs.html#calling-lua-functions>: If you want to pass a parameter as a reference, you have to wrap it with the Boost.Ref. Like this: int ret = call_function(L, "fun", boost::ref(val)); But when I'm trying to do this: #include <iostream>#include <conio.h> extern "C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h"} #include <luabind\luabind.hpp> using namespace std;using namespace luabind; int main() { lua_State *myLuaState = luaL_newstate(); open(myLuaState); int x = 2; do { luaL_dofile(myLuaState, "script.lua"); cout<<"x before = "<< x <<endl; try { call_function<void>(myLuaState, "test", boost::ref(x)); } catch(const std::exception &TheError) { cerr << TheError.what() << endl; } cout<<"x after = "<< x <<endl; } while(_getch() != 27); lua_close(myLuaState);} script.lua: function test(x) x = 7end It gives me a "Trying to use unregistered class" error. So, how to pass value from C++ to lua function by reference, so I can change it inside the script and it will be changed in c++ program too? I'm using boost 1.55.0, lua 5.1, luabind 0.9.1 Thanks. P.S: I posted the same question on StackOverflow <http://stackoverflow.com/questions/25099437/luabind-how-to-pass-value-from-c-to-lua-function-by-reference>, but seems like nobody knows what to do. |