Last year I made a LUA script for C + + packaging that had many friends interested in and tried to use, and I felt flattered. In fact, I do the packaging, learning the purpose is relatively strong, it still has a lot of defects. In order to make friends less detours, I recommend using Luaplus as C + + packaging.
Luaplus is the LUA C + + enhancement, which means that Luaplus itself is enhanced by the LUA source. It is a better choice to cooperate with C + +.
The current version of Luaplus is: Luaplus for Lua 5.01 Distribution build 1080 (February 28, 2004). You can go to the http://luaplus.org/site to download:
Source Code (HTTP://WWHIZ.COM/LUAPLUS/LUAPLUS50_BUILD1081.ZIP)
Target Code (HTTP://WWHIZ.COM/LUAPLUS/LUAPLUS50_BUILD1081_WIN32BINARIES.ZIP)
I'll show you how to use luaplus and how to make it more convenient for luaplus to work seamlessly with C + + classes.
1. Call Lua Scripts
This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/skyremember/archive/2008/09/13/2920834.aspx
// 创建Lua解释器:
LuaStateOwner state;
// 执行Lua脚本:
state->DoString("print('Hello World\n')");
// 载入Lua脚本文件并执行:
state->DoFile("C:\\test.lua");
// 载入编译后的Lua脚本文件并执行:
state->DoFile("C:\\test.luac");
2. Call each other with LUA scripts
// 为Lua脚本设置变量
state->GetGlobals().SetNumber("myvalue", 123456);
// 获得Lua变量的值
int myvalue = state->GetGlobal("myvalue").GetInteger();
// 调用Lua函数
LuaFunction<int> luaPrint = state->GetGlobal("print");
luaPrint("Hello World\n");
// 让Lua调用C语言函数
int add(int a, int b){ return a+b;}
state->GetGlobals().RegisterDirect("add", add);
state->DoString("print(add(3,4))");
// 让Lua调用C++类成员函数
class Test{public: int add(int a, int b){return a+b;}};
Test test;
state->GetGlobals().RegisterDirect("add", test, add);
state->DoString("print(add(3,4))");