Last active
February 27, 2019 08:01
-
-
Save Mainvooid/e7cd6b673fb33b4bf2c68d0faac68643 to your computer and use it in GitHub Desktop.
Lua c++ 共享生命周期示例 #Lua #LuaBridge #C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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