Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing a C++ shared_ptr to LUA #134

Open
MauFranco opened this issue Apr 16, 2017 · 1 comment
Open

Passing a C++ shared_ptr to LUA #134

MauFranco opened this issue Apr 16, 2017 · 1 comment

Comments

@MauFranco
Copy link

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.

@SteveKChiu
Copy link
Owner

SteveKChiu commented May 8, 2017

Assume you already have success with std::shared_ptr for other function, the best way to pass std::shared_ptr to lua function from c++ is via:

  1. LuaRef::fromValue
  2. or Lua::push
  3. or Lua::setGlobal

The lua c API won't work, you need to use one of the LuaIntf API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants