With Cocos2dx-lua's classmates, we all know the callback function , in Lua , we can pass a method as a parameter to another method and then call it in the method . But we often have to write C + + code and then turn it into Lua, so how does C + + receive the Lua method and make the call . Here's a look at the implementation of the most recently written download listener .
1.updatelistener.h
#ifndef __updatelistener__#define __updatelistener__#include "cocos2d.h" #include "CCLuaEngine.h" #include " ExtensionMacros.h "#include" AssetsManagerEx.h "#if (cc_target_platform! = CC_PLATFORM_WINRT) && (cc_target_ PLATFORM! = CC_PLATFORM_WP8) #include <string>ns_cc_ext_begin/* * Updatelistener * @autor SD. Mount * @date 2015.09.29 */class updatelistener:public assetsmanagerexdelegateprotocol{protected://declaration lua function, followed by C after registration + + calls Lua_function _errorscript;//declares the LUA function, after registering C + + calls Lua_function _progressscript;//declares the LUA function, followed by C + + Call Lua_function _ after registration Successscript;public:updatelistener (); ~updatelistener (); Error callback virtual void OnError (Assetsmanagerex::errorcode ErrorCode);//process callback virtual void onprogress (int percent);// Successful callback virtual void onsuccess ();//Registration function/*** Registration Error event * @return */void registerscripthandlererror (lua_function funcid);/*** Registration Progress Event * @return */void registerscripthandlerprogress (lua_function funcid);/*** Registration Success Event * @return */void Registerscripthandlersuccess (Lua_function funcid);Unregister function/*** Error event * @return */void unregisterscripthandlererror (void)/*** Unregister Progress event * @return */void Unregisterscripthandlerprogress (void);/*** Registration Success Event * @return */void unregisterscripthandlersuccess (void);/*** Call LUA function * Param:errorcode error code, error callback call * @return */void Callscripthandler (Assetsmanagerex::errorcode ErrorCode);/*** Call LUA function * param:percent percent, procedure callback * @return */void callscripthandler (int percent);/*** call lua function * @return */void Callscriptha Ndler ();}; Ns_cc_ext_end; #endif//Cc_target_platform! = CC_PLATFORM_WINRT#ENDIF/* defined (__updatelistener__) */
2.updatelistener.cpp
#include "UpdateListener.h" #include "cocos2d.h" using namespace std; Ns_cc_ext_begin; Updatelistener::updatelistener (void) {}updatelistener::~updatelistener () {}//error callback void Updatelistener::onerror ( Assetsmanagerex::errorcode ErrorCode) {This->callscripthandler (ErrorCode);} Process callback void updatelistener::onprogress (int percent) {This->callscripthandler (percent);} Successful callback void Updatelistener::onsuccess () {This->callscripthandler ();} Registration function/*** Registration Error event * @return */void updatelistener::registerscripthandlererror (lua_function funcid) {this->_ Errorscript = Funcid;} /*** Registration Progress Event * @return */void updatelistener::registerscripthandlerprogress (lua_function funcid) {this->_ Progressscript = Funcid;} /*** Registered Success Event * @return */void updatelistener::registerscripthandlersuccess (lua_function funcid) {this->_ Successscript = Funcid;} /*** Unregister Error Event * @return */void updatelistener::unregisterscripthandlererror (void) {if (this->_errorscript) {this- >_errorscript = NULL;}} /*** Registration Progress Event * @return */void Updatelistener::unRegisterscripthandlerprogress (void) {if (this->_progressscript) {this->_progressscript = NULL;}} /*** Registration Success Event * @return */void updatelistener::unregisterscripthandlersuccess (void) {if (this->_successscript) { This->_successscript = NULL;}} /*** Call LUA function * Param:errorcode error code, error callback call * param:percent percent, procedure callback * @return */void Updatelistener::callscripthandler ( Assetsmanagerex::errorcode ErrorCode) {if (This->_errorscript && ErrorCode) {Ccluastack *stack = Ccluaengine ::d efaultengine ()->getluastack (); lua_state* state = Stack->getluastate ();//pressure parameter go inside Stack->clean (); Lua_ Pushnumber (state, (int) errorCode); Stack->executefunctionbyhandler (This->_errorscript, 1);//emptying stack information stack- >clean ();}} /*** Call LUA function * param:percent percent, procedure callback * @return */void updatelistener::callscripthandler (int percent) {if (this->_ Progressscript && percent) {ccluastack *stack = ccluaengine::d efaultengine ()->getluastack (); lua_state* State = Stack->getluastate ();//pressure parameter go inside Stack->clean (); lUa_pushnumber (state, (int) percent); Stack->executefunctionbyhandler (This->_progressscript, 1);// Empty stack information Stack->clean ();}} /*** Call Lua function * @return */void Updatelistener::callscripthandler () {if (this->_successscript) {Ccluastack *stack = CCLu Aengine::d efaultengine ()->getluastack (); lua_state* state = Stack->getluastate ();//pressure parameter go in. Stack->clean (); Stack->executefunctionbyhandler (this->_successscript, 0);//emptying stack information Stack->clean ();}} Ns_cc_ext_end;
3. Note To_lua matters
lua_function funcid = (lua_function) toluafix_ref_function (tolua_s,2,0); cannot be written as lua_function funcid = * ((lua_function) *) toluafix_ref_function (tolua_s,2,0); because I used the script to generate it, I got an error.
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4847063.html
[cocos2dx-lua]0-002. How to receive the LUA method in C + +, and then Tolua