cocos2dx luaで「Lambda binding for lua is not supported.」
とコメントがありその後assert(false)となっている場所がある。
使えなくて不便だなと思っていていろいろと調べたところ
解決できたのでメモ、正しい方法か怪しいです。
まずそもそも、onTouchとかlambdaになっているところでも対応している場所があるので
できるんじゃないかなと思い調査をしました。
lua_cocos2dx_ui_manual.cppのソースを参考に魔改造を施しました。
今回使いたかったのは「cc.EventDispatcher:addCustomEventListener」
こんなかんじで修正を行ったところのみ抜粋
frameworks/cocos2d-x/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp
#include "CCLuaEngine.h"
int lua_cocos2dx_EventDispatcher_addCustomEventListener(lua_State* tolua_S)
{
int argc = 0;
cocos2d::EventDispatcher* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(tolua_S,1,"cc.EventDispatcher",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::EventDispatcher*)tolua_tousertype(tolua_S,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_EventDispatcher_addCustomEventListener'", nullptr);
return 0;
}
#endif
argc = lua_gettop(tolua_S)-1;
if (argc == 2)
{
std::string arg0;
std::function<void (cocos2d::EventCustom *)> arg1;
ok &= luaval_to_std_string(tolua_S, 2,&arg0, "cc.EventDispatcher:addCustomEventListener");
/*
do {
// Lambda binding for lua is not supported.
assert(false);
} while(0)
;
*/
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_EventDispatcher_addCustomEventListener'", nullptr);
return 0;
}
//
LUA_FUNCTION handler = ( toluafix_ref_function(tolua_S,3,0));
cocos2d::EventListenerCustom* ret = cobj->addCustomEventListener(arg0, [=](cocos2d::EventCustom* event){
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
stack->pushObject(event, "cc.Ref");
stack->executeFunctionByHandler(handler, 1);
stack->clean();
});
object_to_luaval<cocos2d::EventListenerCustom>(tolua_S, "cc.EventListenerCustom",(cocos2d::EventListenerCustom*)ret);
//
return 1;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.EventDispatcher:addCustomEventListener",argc, 2);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_EventDispatcher_addCustomEventListener'.",&tolua_err);
#endif
return 0;
}
これでluaではこんな感じで記載
cc.Director:getInstance():getEventDispatcher():addCustomEventListener("todo", function(event)
printInfo("todo")
end)
pull requestしようかな。。