這裡講下Lua與C++之間如何實現相互調用
DJLCData.h 實現類
// // DJLCData.h // uitestLua // // Created by 杜甲 on 14-5-17. // // #ifndef __uitestLua__DJLCData__ #define __uitestLua__DJLCData__ #include cocos2d.h #include CCLuaEngine.h USING_NS_CC; using namespace std; extern C{ #include lua.h #include lualib.h #include lauxlib.h } class DJLCData { public: static DJLCData* getInstance(); /* getLuaVarString:調用Lua全局string luaFilename = lua文件名 varName = 所要取Lua中的變量名 */ const char* getLuaVarString(const char* luaFileName,const char* varName); /* * @brief getLuaOneOfTable:調用Lua全局table中的一個元素 luaFileName = lua文件名 varName = 所要取Lua中的table變量名 keyName = 所要取Lua中的table中某一個元素的Key */ const char* getLuaVarOneOfTable(const char* luaFileName,const char* varName,const char* keyName); /* * getLuaVarTable:調用lua全局table luaFileName = lua文件名 varName = 所要取的table變量名 (注:返回的是所有的數據) */ const char* getLuaVarTable(const char* luaFileName,const char* varName); /* callLuaFunction:調用lua函數 luaFileName = lua文件名 functionName = 所要調用Lua中的函數名 */ const char* callLuaFunction(const char* luaFileName,const char* functionName); /***************** lua -> C++ **************/ void callCppFunction(const char* luaFileName); private: static int cppFunction(lua_State* ls); static bool _isFirst; static DJLCData* _shared; const char* getFileFullPath(const char* fileName); }; #endif /* defined(__uitestLua__DJLCData__) */
// // DJLCData.cpp // uitestLua // // Created by 杜甲 on 14-5-17. // // #include DJLCData.h bool DJLCData::_isFirst; DJLCData* DJLCData::_shared; DJLCData* DJLCData::getInstance() { if (_shared == nullptr) { _shared = new DJLCData(); } return _shared; } const char* DJLCData::getLuaVarString(const char *luaFileName, const char *varName) { lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if (isOpen != 0) { log(Open Lua Error:%i,isOpen); return nullptr; } lua_settop(ls, 0); lua_getglobal(ls, varName); int statesCode = lua_isstring(ls, 1); if (statesCode != 1) { log(Open Lua Error : %i,statesCode); return nullptr; } const char* str = lua_tostring(ls, 1); lua_pop(ls, 1); return str; } const char* DJLCData::getLuaVarOneOfTable(const char *luaFileName, const char *varName, const char *keyName) { lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if (isOpen != 0) { log(Open Lua Error : %i,isOpen); return nullptr; } lua_getglobal(ls, varName); int statesCode lua_istable(ls, -1); if (statesCode != 1) { log(Open Lua Error: %i,statesCode); return nullptr; } lua_pushstring(ls, keyName); lua_gettable(ls, -2); const char* valueString = lua_tostring(ls, -1); lua_pop(ls, -1); return valueString; } const char* DJLCData::getLuaVarTable(const char *luaFileName, const char *varName) { lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if (isOpen != 0) { log(Open Lua Error:%i,isOpen); return nullptr; } lua_getglobal(ls, varName); int it = lua_gettop(ls); lua_pushnil(ls); string result = ; while (lua_next(ls, it)) { string key = lua_tostring(ls, -2); string value = lua_tostring(ls, -1); result = result + key + : + value + ; lua_pop(ls, 1); } lua_pop(ls, 1); return result.c_str(); } const char* DJLCData::callLuaFunction(const char *luaFileName, const char *functionName) { lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if (isOpen) { log(Open Lua Error:%i,isOpen); return nullptr; } lua_getglobal(ls, functionName); lua_pushstring(ls, shen); lua_pushnumber(ls, 23); lua_pushboolean(ls, true); /* lua_call 第一個參數:函數的參數個數 第二個參數:函數返回值個數 */ lua_call(ls, 3, 1); const char* iResult = lua_tostring(ls, -1); return iResult; } void DJLCData::callCppFunction(const char *luaFileName) { lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); /* Lua調用的C++的函數必須是靜態的 */ lua_register(ls, cppFunction, cppFunction); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if (isOpen != 0) { log(Open Lua Error:%i,isOpen); return; } } int DJLCData::cppFunction(lua_State *ls) { int luaNum = (int)lua_tonumber(ls, 1); char* luaStr = (char*)lua_tostring(ls, 2); log(Lua調用cpp函數時傳來的兩個參數:%i,%s,luaNum,luaStr); /* 返給Lua的值 */ lua_pushnumber(ls, 321); lua_pushstring(ls, shenqi); /* 返給Lua值個數 */ return 2; } const char* DJLCData::getFileFullPath(const char *fileName) { //src// 這裡的//是指我的lua文件放在src這個目錄下 std::string str = StringUtils::format(src//%s,fileName); return FileUtils::getInstance()->fullPathForFilename(str).c_str(); }
hello2.lua
function myadd(x, y) return x + y end luaStr = shenqi luaTable = {name = xiaonan,age = 20} function luaLogString(_logStr,_logNum,_logBool) print(Lua 腳本打印從C傳來的字符串:,_logStr,_logNum,_logBool) return Call Lua function OK end function call_cpp(_logStr,_logNum,_logBool) num,str = cppFunction(999,lua string) print(從cpp函數中獲得兩個返回值:,num,str) end
調用方法:
log(%s,DJLCData::getInstance()->getLuaVarString(hello2.lua, luaStr)); log(%s,DJLCData::getInstance()->getLuaVarOneOfTable(hello2.lua, luaTable, name)); log(Table = %s,DJLCData::getInstance()->getLuaVarTable(hello2.lua, luaTable)); log(Call Lua Function Back :%s,DJLCData::getInstance()->callLuaFunction(hello2.lua, luaLogString)); DJLCData::getInstance()->callCppFunction(hello2.lua); DJLCData::getInstance()->callLuaFunction(hello2.lua, call_cpp);
cocos2d: shenqi cocos2d: xiaonan cocos2d: Table = name:xiaonan age:20 cocos2d: [LUA-print] Lua 腳本打印從C傳來的字符串: shen 23 true cocos2d: Call Lua Function Back :Call Lua function OK cocos2d: Lua調用cpp函數時傳來的兩個參數:999,lua string cocos2d: [LUA-print] 從cpp函數中獲得兩個返回值: 321 shenqi