Skip to content

Instantly share code, notes, and snippets.

@Mainvooid
Last active February 27, 2019 08:01
Show Gist options
  • Select an option

  • Save Mainvooid/e7cd6b673fb33b4bf2c68d0faac68643 to your computer and use it in GitHub Desktop.

Select an option

Save Mainvooid/e7cd6b673fb33b4bf2c68d0faac68643 to your computer and use it in GitHub Desktop.
Lua c++ 共享生命周期示例 #Lua #LuaBridge #C++
struct A
{
A() {}
int a=10;
static void regToLuaState(lua_State *L) {
luaL_openlibs(L);
getGlobalNamespace(L)
.beginNamespace("LuaCallFuncDll")
.beginClass <A>("A")
.addConstructor <void(*)(void)>()
.addData("a", &A::a)
.endClass()
.endNamespace();
};
};
struct B
{
B() { ref = new A; }
static void regToLuaState(lua_State *L) {
luaL_openlibs(L);
getGlobalNamespace(L)
.beginNamespace("LuaCallFuncDll")
.beginClass <B>("B")
.addConstructor <void(*)(void)>()
.addData("ref", &B::ref)
.endClass()
.endNamespace();
};
RefCountedPtr<A> ref;
};
A::regToLuaState(L);
B::regToLuaState(L);
B b;
push(L, &b);
lua_setglobal(L, "b");
std::cout << *b.ref << std::endl;
b.ref->a = 20;
//lua: print(b.ref.a) //20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment