This article was reproduced in: http://www.cnblogs.com/zisou/p/cocos2dx-lua2.html
We mainly address the following issues:
1,c++ How to get a variable value inside LUA?
2,c++ how to get a table in Lua (LUA uses table to implement object-oriented, it can invoke the more advanced reference method inside LUA)
3,c++ How to access a method inside Lua?
How does 4,c++ access a parameter-taking method inside Lua?
How does 5,c++ access a parameter-taking method inside Lua with a return value?
If all the above problems are solved, we can meet the problems of data communication in the game development.
I integrated the Himi brother this tutorial inside the idea, refactoring a bit, defined a better use of the C++/lua data communication class library;
http://blog.csdn.net/xiaominghimi/article/details/8816887
Below I directly paste function, everybody to pick up:
PublicSendLuaData.h
publicsendluadata.cpp////Lua and c++/c interaction class #ifndef __publicsendluadata__#define __publicsendluadata__#include " Cocos2d.h "using namespace cocos2d;using namespace Std;extern" C "{#include" lua.h "#include" lualib.h "#include" Lauxlib.h "};class publicsendluadata{public:static publicsendluadata* getinstance (); /* Directly get the LUA variable name value filename luafilename variable name varName */const char* getluavarstring (const char* Luafilename,con St char* VarName); /* Gets the node name in a table in Lua, even the method file name Luafilename method name varname section name */const char* getluavaroneoftable (const char* Luafilename,const char* varname,const char* keyName); /* Call LUA Global table LUA file name luafilename table variable name varname */const char* getluavartable (const char* Luafilename , const char* varName); /* with parameter return file name Luafilename method name functionname parameter sequence arraypar parameter type Arraypartype */const char* Callluafu Ncparreturn (const char* luafilename,const char* Functionname,ccarray* arraypar,ccarray* Arraypartype); /* with parameter no return file name Luafilename method name functionname parameter sequence arraypar parameter type Arraypartype */const void Callluafun Cpar (const char* luafilename,const char* functionname,ccarray* arraypar,ccarray* arraypartype); private:static bool _isfirst; Static publicsendluadata* m_instance; Const char* Getfilefullpath (const char* fileName); ~publicsendluadata ();}; #endif
PublicSendLuaData.cpp
publicsendluadata.cpp////Created by zisou-ysj////Lua and c++/c Interactive class # include "PublicSendLuaData.h" #include "Ccluaeng Ine.h "publicsendluadata* publicsendluadata::m_instance = NULL; publicsendluadata* publicsendluadata::getinstance () {if (!m_instance) {m_instance = new Publicsendlua Data (); } return m_instance;} Get variable Name Value const char* publicsendluadata::getluavarstring (const char* luafilename,const char* varName) {lua_state* ls = Ccluaengine::d efaultengine ()->getluastack ()->getluastate (); int isOpen = Lual_dofile (ls, Getfilefullpath (luafilename)); if (isopen!=0) {cclog ("Open Lua Error:%i", IsOpen); return NULL; } lua_settop (LS, 0); Lua_getglobal (LS, varName); int statescode = lua_isstring (ls, 1); if (statescode!=1) {cclog ("Open Lua Error:%i", Statescode); return NULL; } const char* str = lua_tostring (ls, 1); Lua_pop (LS, 1); return str;} Const char* Publicsendluadata::geTluavaroneoftable (const char* luafilename,const char* varname,const char* keyName) {lua_state* ls = ccluaengine::d Efaultengine ()->getluastack ()->getluastate (); int isOpen = Lual_dofile (ls, Getfilefullpath (luafilename)); if (isopen!=0) {cclog ("Open Lua Error:%i", IsOpen); return NULL; } lua_getglobal (LS, varName); int statescode = lua_istable (ls,-1); if (statescode!=1) {cclog ("Open Lua Error:%i", Statescode); return NULL; } lua_pushstring (LS, keyName); Lua_gettable (LS,-2); Const char* valuestring = lua_tostring (ls,-1); Lua_pop (LS,-1); return valuestring;} Execute LUA table, return table structure const char* publicsendluadata::getluavartable (const char* luafilename,const char* varName) {lua_state* L s = ccluaengine::d efaultengine ()->getluastack ()->getluastate (); int isOpen = Lual_dofile (ls, Getfilefullpath (luafilename)); if (isopen!=0) {cclog ("Open Lua Error:%i", IsOpen); Return NULL; } 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+ "\ T"; Lua_pop (LS, 1); } lua_pop (LS, 1); return Result.c_str ();} The Execute LUA method with parameters has a return value of const char* Publicsendluadata::callluafuncparreturn (const char* luafilename,const char* functionname, ccarray* arraypar,ccarray* arraypartype) {lua_state* ls = ccluaengine::d efaultengine ()->getluastack () Getluastate (); int isOpen = Lual_dofile (ls, Getfilefullpath (luafilename)); if (isopen!=0) {cclog ("Open Lua Error:%i", IsOpen); return NULL; } lua_getglobal (LS, functionname); int countnum = Arraypar->count (); if (countnum>0) {for (int i = 0; I<arraypar->count (); i++) {ccstring* typestr = (ccstring*) Arraypartype->Objectatindex (i); ccstring* Strnr = (ccstring*) arraypar->objectatindex (i); if (Typestr->isequal (Ccstring::create ("string"))) {lua_pushstring (LS, strnr->getcstring ()) ; } else if (Typestr->isequal (ccstring::create ("int"))) {Lua_pushnumber (LS, STRNR-&G T;intvalue ()); } else if (Typestr->isequal (ccstring::create ("bool"))) {Lua_pushboolean (LS, strnr- >boolvalue ()); }}}//Lua_call first parameter: Number of arguments to function second parameter: Number of function return values */Lua_call (LS, countnum, 1); Const char* Iresult = lua_tostring (ls,-1); return iresult;} Execute Lua method with parameter no return value const void Publicsendluadata::callluafuncpar (const char* luafilename,const char* Functionname,ccarray * arraypar,ccarray* arraypartype) {lua_state* ls = ccluaengine::d efaultengine ()->getluastack ()->getLuaState (); int isOpen = Lual_dofile (ls, getfilefullPath (Luafilename)); if (isopen!=0) {cclog ("Open Lua Error:%i", IsOpen); } lua_getglobal (LS, functionname); int countnum = Arraypar->count (); if (countnum>0) {for (int i = 0; I<arraypar->count (); i++) {ccstring* typestr = (ccstring*) Arraypartype->objectatindex (i); ccstring* Strnr = (ccstring*) arraypar->objectatindex (i); if (Typestr->isequal (Ccstring::create ("string"))) {lua_pushstring (LS, strnr->getcstring ()) ; } else if (Typestr->isequal (ccstring::create ("int"))) {Lua_pushnumber (LS, STRNR-&G T;intvalue ()); } else if (Typestr->isequal (ccstring::create ("bool"))) {Lua_pushboolean (LS, strnr- >boolvalue ()); }}}//Lua_call first parameter: Number of arguments to function second parameter: Number of function return values */Lua_call (LS, countnum, 0); }const char* Publicsendluadata::getfilefullpath (cOnst char* fileName) {return ccfileutils::sharedfileutils ()->fullpathforfilename (filename). C_STR ();} Publicsendluadata::~publicsendluadata () {cc_safe_delete (m_instance); M_instance=null;}
There are comments on the above, I will not go into detail one by one, but the above method can meet the majority of the requirements of the C + + access to LUA;
I'll just say one example here:
ccarray* Arraypar = Ccarray::create () arraypar->addobject (ccstring::create ("parameter value")); ccarray* arraytype =ccarray::create (); Arraytype->addobject (Ccstring::create ("string")); Publicsendluadata::getinstance ()->callluafuncpar ("Lua filename", "parameter name", Arraypar, arraytype);
This is a specific call to LUA inside a parameter global method of the statement;
I wrote a type-matching method:
if (Typestr->isequal (Ccstring::create ("string"))) { lua_pushstring (LS, strnr->getcstring ()); } else if (Typestr->isequal (ccstring::create ("int"))) { lua_pushnumber (LS, strnr->intvalue ()); } else if (Typestr->isequal (ccstring::create ("bool"))) { Lua_pushboolean (LS, strnr->boolvalue ()); }
When you use it, pay attention to match this to the line;
What if we were to access C + + functions in Lua? Remember the first part of my article that Lua calls the C + + function class method?
I advocate using the tolua++ tool class to sit LUA access C + +;
Cocos2dx-c++ and LUA data communication