Based on the previous article
COCOS2DX exporting C + + classes for LUA use
The previous article was mainly because the LUA function could not be called, but this article can call the LUA function, then the purpose of the implementation is much simpler
The reason or the Tolua tool generated the HPP CPP file function is not correct although the function parameter in C + + is declared as lua_function but still treated as int
So we're going to manually handle the function call, which is to modify the generated function.
... Generate the corresponding code change to the following if (null == tolua_s) return 0;int argc = 0; netmgr* self = nullptr; #if cocos2d_debug >= 1tolua_error tolua_err;if (!tolua_isusertype (tolua_s, 1, "CC. Http ", 0, &tolua_err)) goto tolua_lerror; #endifself = static_cast<netmgr* > (Tolua_tousertype (tolua_s, 1, 0)); #if COCOS2D_DEBUG >= 1if (nullptr == self) {tolua_error (tolua_s, "invalid ' self ' in function ' tolua_cocos2d_ Node_registerscripthandler ' \ n ', null); return 0;} #endifargc = lua_gettop (tolua_s) - 1;if (argc == 1) {#if cocos2d_debug >= 1if (!toluafix_isfunction (tolua_s, 2, "Lua_function", 0, &tolua_ ERR)) goto tolua_lerror; #endifLUA_FUNCTION handler = toluafix_ref_function (tolua_s, 2, &NBSP;0); Self->setluAfunc (handler); Lua_settop (tolua_s, 1); return 0;} Lual_error (tolua_s, "%s has wrong number of arguments: %d, was expecting %d\n ", " CC. Node:registerscripthandler ", argc, 1);return 0; #if cocos2d_debug >= 1tolua_ Lerror:tolua_error (tolua_s, "#ferror in function ' Tolua_cocos2d_node_registerscripthandler '. ', &tolua_err);return 0; #endif
The header file should read
Class Toluafunc/*base class for-to-call LUN function*/{public:void setluafunc (lua_function handler);p rotected:void Callluafunc (const CHAR*ARG); Lua_function handler;}; #define URL_BASE "http://127.0.0.1:8080/cocos/" #define Data_max_length 100class netmgr:p ublic toluafunc{public: static netmgr*getinstance ();/** * @brief New A httprequest * @param action such as "Login?name=1&&pass=1" * @ */v OID Getrequest (const char* action); Private:netmgr () {}char _data[data_max_length];};
CPP file instead
#include "Network_srv.h" #include "CCLuaEngine.h" #include "Base/ccscriptsupport.h" void Toluafunc::setluafunc (Lua_function handler) {This->handler = handler;} Void toluafunc::callluafunc (Const char*arg) {Cc_assert (handler > 0, "Call lua func ' s handle must bigger than 0 '); Cocos2d::commonscriptdata data ( HANDLER,&NBSP;ARG); cocos2d::scriptevent scriptevent (Cocos2d::kcommonevent, &data); Cocos2d:: Luaengine::getinstance ()->sendevent (&scriptevent);} Void netmgr::getrequest (const char* action) {log ("c++ getrequest arg is %s ", action);string url = url_base;url += action; Httprequest*request = new httprequest;request->seturl (Url.c_str ());request-> Setrequesttype (Httprequest::type::get); Request->setresponsecallback ([=] (httpclient*client, Httpresponse *respone) {if (respone-≫getresponsecode () != 200) return;vector<char>* buffer = respone-> Getresponsedata (); Cc_assert (Data_max_length > buffer->size (), " NetMgr buffer size Overfloaw ");for (Int i = 0; i < buffer->size (); i++) {_data[i] = (*buffer) [i];} _data[buffer->size ()] = ' + '; Callluafunc (_data);}); Httpclient::getinstance ()->settimeoutforconnect (10); Httpclient::getinstance ()->send (request); Request->release ();} Netmgr* netmgr::getinstance () {static netmgr* _netmgr__ = 0;if (_netmgr__ == 0) {_netmgr__ = new netmgr;} return _netmgr__;}
LUA file
local function menucallbackclose () http:getrequest ("login?name=1& &pass=1 ") return &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;END&NBSP;&NBSP;&NBSP;&NBSP;LABEL=CC. Labelttf:create ("Login", "" "," label:setposition ") (SELF.SIZE.WIDTH/2,SELF.SIZE.HEIGHT/2); rootnode:addchild (label) local Menutoolsitem = rootnode:getchildbyname ("button_1") menutoolsitem: Addtoucheventlistener (menucallbackclose) local function callback (msg) label:setstring (msg); end http=netmgr:getinstance (); http:setluafunc (callback);
Where the CPP file can also be changed to this
void Toluafunc::callluafunc (const char*arg) {Cc_assert (Handler > 0, "Call Lua func ' s handle must bigger than 0");/*coco S2d::commonscriptdata data (handler, ARG); Cocos2d::scriptevent scriptevent (Cocos2d::kcommonevent, &data); Cocos2d::luaengine::getinstance ()->sendevent (&scriptevent); */cocos2d::luaengine::getinstance () Getluastack ()->pushstring (ARG); Cocos2d::luaengine::getinstance ()->getluastack () Executefunctionbyhandler (handler, 1); Cocos2d::luaengine::getinstance ()->getluastack ()->clean ();
C + + callback Lua function, implementing event