This section describes how to implement mutual calls between Lua and C ++.
DJLCData. h implementation class
//// DJLCData. h // uitestLua /// Created by du jia on 14-5-17. /// # ifndef _ uitestLua _ DJLCData __# define _ uitestLua _ DJLCData __# include cocos2d. h # include CCLuaEngine. hUSING_NS_CC; using namespace std; extern C {# include lua. h # include lualib. h # include lauxlib. h} class DJLCData {public: static DJLCData * getInstance ();/* getLuaVarString: call Lua global string luaFilename = lua file name varName = variable name in Lua */const char * getLuaVarString (const char * luaFileName, const char * varName ); /** @ brief getLuaOneOfTable: call one element of Lua global table luaFileName = lua file name varName = Name of the table variable in Lua keyName = Key of an element in the table in Lua */const char * getLuaVarOneOfTable (const char * luaFileName, const char * varName, const char * keyName);/** getLuaVarTable: Call lua global table luaFileName = lua file name varName = Name of the expected table variable (note: returns all data) */const char * getLuaVarTable (const char * luaFileName, const char * varName);/* callLuaFunction: call lua function luaFileName = lua file name functionName = Name of the function */const char * callLuaFunction (const char * luaFileName, const char * functionName) in Lua to be called ); /**************** 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
//// DJLCData. cpp // uitestLua /// Created by du jia on 14-5-17. //// # include DJLCData. hbool 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 I SOpen = 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 (lua FileName); 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);/* First parameter of lua_call: Number of function parameters; second parameter: Number of function return values */lua_call (ls, 3, 1); cons T char * iResult = lua_tostring (ls,-1); return iResult;} void DJLCData: callCppFunction (const char * luaFileName) {lua_State * ls = LuaEngine: getInstance () -> getLuaStack ()-> getLuaState ();/* The C ++ function called by Lua must be static */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 (two parameters sent when Lua calls the cpp function: % I, % s, luaNum, luaStr);/* value returned to Lua */lua_pushnumber (ls, 321); lua_pushstring (ls, shenqi ); /* Number of returned Lua values */return 2;} const char * DJLCData: getFileFullPath (const char * fileName) {// src // here // indicates that my lua file is placed in the src directory std: string str = StringUtils: format (src // % s, fileName); return FileUtils: getInstance ()-> fullPathForFilename (str ). c_str ();}
Hello2.lua
Function myadd (x, y) return x + yendluaStr = shenqiluaTable = {name = xiaonan, age = 20} function luaLogString (_ logStr, _ logNum, _ logBool) print (Lua script prints the string from C:, _ logStr, _ logNum, _ logBool) return Call Lua function OKendfunction call_cpp (_ logStr, _ logNum, _ logBool) num, str = cppFunction (999, lua string) print (two returned values are obtained from the cpp function:, num, str) end
Call method:
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: shenqicocos2d: rule: Table = name: xiaonanage: 20cocos2d: [LUA-print] The Lua script prints the string from C: shen23truecocos2d: Call Lua Function Back: Call Lua function OKcocos2d: when Lua calls the cpp function, two parameters are sent: 999, lua stringcocos2d: [LUA-print]. Two return values are obtained from the cpp function: 321 shenqi.