Here we talk about how Lua and C + + communicate with each other
Original connection: http://blog.csdn.net/qqmcy/article/details/26052771
DJLCDATA.H Implementation Class
djlcdata.h//uitestlua////Created by du Jia Li on 14-5-17.////#ifndef __uitestlua__djlcdata__#define __UITESTLUA__DJLCDA Ta__#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: Call LUA global string luafilename = Lua file name varName = The variable name to fetch in LUA */const CH ar* getluavarstring (const char* luafilename,const char* varName); /* * @brief getluaoneoftable: Call one of the LUA global table elements luafilename = Lua file name varName = to fetch LUA The table variable name KeyName = The key for one of the elements in the table to be taken in LUA */const char* getluavaroneoftable (const char* Luafi Lename,const char* varname,const char* keyName); /* * getluavartable: Call LUA global Table Luafilename = Lua file name varName = name of table variable to be taken (note: all data returned) */Const char* getluavartable(const char* Luafilename,const char* varName); /* Callluafunction: Call lua function Luafilename = Lua file name functionname = The function name to invoke in 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
djlcdata.cpp//uitestlua////Created by du Jia Li on 14-5-17.////#include "DJLCData.h" bool djlcdata::_isfirst;djlcdata* D jlcdata::_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::getinst Ance ()->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_stat e* 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::getinsta NCE ()->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 (LS, it lua_next) {string key = Lua_tostring (ls,-2); String value = lua_tostring (ls,-1); result = result + key + ":" + value + "\ T"; 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::g Etinstance ()->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 first parameter: The second parameter of the function's number of arguments: The number of function return values */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 (); /*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 references from LUA when invoking CPP functions:%i,%s", LUANUM,LUASTR); /* value returned to LUA */lua_pushnumber (LS, 321); Lua_pushstring (LS, "ShenQi"); /* Returns the number of LUA values */return 2; }const char* djlcdata::getfilefullpath (const char *filename) {//src//here//refers to my Lua file placed under SRC this folder std::string str = Stringutils::format ("src//%s", fileName); Return Fileutils::getinstance ()->fullpathforfilename (str). C_STR ();}
Hello2.lua
function Myadd (x, y) return x + yendluastr = "ShenQi" luatable = {name = "Xiaonan", age = "$"}function lualogstring (_lo Gstr,_lognum,_logbool) print ("Lua script prints a string from C:", _logstr,_lognum,_logbool) return "Call Lua function OK" Endfunction call_cpp (_logstr,_lognum,_logbool) num,str = cppfunction (999, "LUA string") print (" Get two return values 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:xiaonancocos2d:Table = name:xiaonanage:20cocos2d: [Lua-print] LUA The script prints a string from C: Shen23truecocos2d:call lua function Back:call LUA functions Okcocos2d:lua The two parameters that came when invoking CPP: 999,lua STRINGCOCOS2D: [Lua-print] obtains two return values from the CPP function: 321shenqi
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
COCOS2D-X3.0 next Lua and C + + call each other