cocos2d-x内部使用tolua进行lua绑定,可是引擎并无提供一个通用的接口让咱们能够把一个lua函数注册给C++层面的回调事件。
翻看引擎的lua绑定代码,咱们能够仿照引擎中的方法来作。
值得吐槽的是,这套流程在开发中几乎是不可避免的,而cocos2d-x居然不把它做为一个公用接口暴露给开发者,而须要我本身动手,真是无奈。api
下面以一个简单的消息分发类为例子,演示如何完成这一工做。函数
class MessageDispather { public: static MessageDispather* sharedDispather(); public: void invokeLuaCallbackFunction(int msgId, const char* text); void registerScriptHandler(int nHandler); private: int mLuaHandlerId; };
#include "CCLuaEngine.h" MessageDispather* sharedDispather() { static MessageDispather* instance = NULL; if(instance == NULL) instance = new MessageDispather(); return instance; } void MessageDispather::invokeLuaCallbackFunction(int msgId, const char* text) { if(mScriptHandler > 0) { CCLuaStack* stack = CCLuaEngine::defaultEngine()->getLuaStack(); stack->pushInt(msgId); stack->pushString(text); stack->executeFunctionByHandler(mScriptHandler, 2); stack->clean(); } } void MessageDispather::registerScriptHandler(int nHandler) { mLuaHandlerId = nHandler; }
#include "CCLuaEngine.h"
这个头文件来自cocos2d-x\scripting\lua\cocos2dx_support
整个文件夹里的内容是cocos2d-x引擎作lua绑定时封装的一些工具类。
你须要在你的项目中添加这个目录的include搜索路径。工具
void registerScriptHandler(int nHandler)
这个函数须要暴露给lua。
在lua中调用这个函数,参数nHandler的位置传进去一个lua函数,就可以在C++这边获得一个nHandler的整数句柄值。
以后任什么时候间若是想要在C++中调用刚刚注册的lua回调函数,须要以这个整数值来指代那个函数。post
void invokeLuaCallbackFunction(int msgId, const char* text)
在C++用调用此函数,咱们期待它会调用到一个在lua中定义的回调函数。
具体这个函数里的实现是什么意思,若是你对lua c api有必定了解的话应该很容易能看懂,我就再也不作解释。ui
class MessageDispather { static MessageDispather* sharedDispather(); void registerScriptHandler(LUA_FUNCTION nHandler); };
local function onMessage(msgId, text) print(msgId, text) end MessageDispatcher:sharedDispatcher():registerScriptHandler(onMessage)
有没有发现咱们的pkg文件中有一个类型是LUA_FUNCTION
??
对,由于这个参数在lua中应该传入一个函数,而到了C++这边咱们拿到的倒是一个int。
这并非tolua的缺省行为,而是cocos2d-x针对这种状况作的一个特殊处理。
翻看cocos2d-x的tolua绑定流程,咱们能够发现build.bat中的内容是这样的:tolua++ -L basic.lua -o "../../scripting/lua/cocos2dx_support/LuaCocos2d.cpp" Cocos2d.pkg
这里basic.lua是一些额外的逻辑,其中处理LUA_FUNCTION类型的逻辑也在里面。lua
那么咱们能够照猫画虎,请建立这样一个lua文件:spa
_is_functions = _is_functions or {} _to_functions = _to_functions or {} -- register LUA_FUNCTION, LUA_TABLE, LUA_HANDLE type _to_functions["LUA_FUNCTION"] = "toluafix_ref_function" _is_functions["LUA_FUNCTION"] = "toluafix_isfunction" _to_functions["LUA_TABLE"] = "toluafix_totable" _is_functions["LUA_TABLE"] = "toluafix_istable" local toWrite = {} local currentString = '' local out local WRITE, OUTPUT = write, output function output(s) out = _OUTPUT output = OUTPUT -- restore output(s) end function write(a) if out == _OUTPUT then currentString = currentString .. a if string.sub(currentString,-1) == '\n' then toWrite[#toWrite+1] = currentString currentString = '' end else WRITE(a) end end function post_output_hook(package) local result = table.concat(toWrite) local function replace(pattern, replacement) local k = 0 local nxt, currentString = 1, '' repeat local s, e = string.find(result, pattern, nxt, true) if e then currentString = currentString .. string.sub(result, nxt, s-1) .. replacement nxt = e + 1 k = k + 1 end until not e result = currentString..string.sub(result, nxt) if k == 0 then print('Pattern not replaced', pattern) end end replace([[*((LUA_FUNCTION*)]], [[(]]) replace([[tolua_usertype(tolua_S,"LUA_FUNCTION");]], [[]]) WRITE(result) end
而后在你执行tolua++的时候把这个文件做为-L参数传进去就能够了。rest