lua-intf
lua-intf copied to clipboard
Passing a C++ shared_ptr to LUA
Hello, Thanks for sharing this great library. It has solve almost all my problems but I still have an issue.
I've done all my function and class bindings carefully following the provided instructions. Now I want to call a Lua function from C++ and I want to pass a shared_ptr as an argument to this function, or pass it as a global variable.
I have tried using this binding:
std::shared_ptr<Object> o;
LuaIntf::LuaBinding(L)
.beginModule("module")
.addVariableRef("object", o, false)
.endModule();
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_getfield(L, LUA_GLOBALSINDEX, functionName);
lua_pcall(L, 0, 0, 0);
And this:
lua_pushlightuserdata(L, o);
lua_setglobal(L, "object");
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_getfield(L, LUA_GLOBALSINDEX, functionName);
lua_pcall(L, 1, 0, 0);
with no success.
I appreciate if you can help me realize how it is the correct way to do it.
Thank you very much in advance, Kind regards,
Mauricio.
Assume you already have success with std::shared_ptr<Object> for other function, the best way to pass std::shared_ptr<Object> to lua function from c++ is via:
- LuaRef::fromValue
- or Lua::push
- or Lua::setGlobal
The lua c API won't work, you need to use one of the LuaIntf API.