Lua绑定流程

绑定是为了实现将C++代码注册到lua环境,使得lua可以调用C++函数。
https://blog.csdn.net/u012668054/article/details/48465233


Lua绑定步骤:

  1. 生成协议文件.
  2. 协议id加入到协议列表(cpp):PbMsgFactory.cppmsg_table_pb.lua
  3. 将C++代码绑定到Lua。
  4. 生成pb文件。
  5. 生成GenCode。
  6. 在main.lua中加入创建代码:
    function createWecActivityLuckyDay(groupId)return WecActivityLuckyDay:create(groupId) end
  7. 在WecActivityCenterView.cpp中调用lua代码:
    Node* _actNode = LuaUtils::call_if_exists(L, "createWecActivityLuckyDay", m_currGroupId);
  8. 在WecLuaConversions.cpp和lua_cocos2dx_coco_studio_manual.cpp中添加额外需要的函数并注册tolua
    weclua_getStateDataByGroupIDForLua、lua_cocos2dx_ActionTimeline_setAnimationEndCallFunc

  1. 生成协议:proto+protobuf
    执行client/common/excel/proto2cpp.py
    生成文件:client\WeCGame\IF\Classes\protoclient\WeCGame\IF\Classes\protobuf

  2. 协议id加入到协议列表(cpp)
    执行client/common/cs_proto/run4client.bat
    生成的相关文件:client\common\cs_proto\generated

  3. 运行genbindings_if.py脚本。

  4. 生成lua对应的protobuf文件
    Art/cocosstudio/GenPbForLua.bat
    修改GenPbForLua.py文件,将需要生成pb的协议加入到GenPbForLua.py文件中

    Lua绑定流程_第1张图片

  5. 生成UI对应的lua代码GenCode
    Art/cocosstudio/GenerateLuaBindingCode.bat
    修改bat文件,-files参数改为需要生成的csd文件名,不带csd后缀。然后双击bat文件生成。


错误:

  1. LibclangError:%1不是有效的Win32。
    Lua绑定流程_第2张图片

    解决:Python、pyyaml、pyCheetah都用32位版本
  2. TranslationUnitLoadError: Error parsing translation unit.
    Lua绑定流程_第3张图片

    造成这个问题的原因是因为类名写错,或者路径写错,或者是写了一个不存在的类。
  3. The namespace conversion wasn't set in 'ns_map' section of the conversion.yaml
    The namespace(google::protobuf::Map,Res::AreaFlag>) conversion wasn't set in 'ns_map' section of the conversion.yaml
    将函数google::protobuf::Map<::std::string, ::Res::AreaFlag> LocalController::getLocateMap()加入skip中,跳过该函数的绑定。
    具体为:打开ini文件比如:cocos2dx_if_controller.ini,在skip中加入LocalController::[getLocateMap],
  4. skip参数出错
    methods = skip.split("::") ValueError: too many values to unpack
    skip = CCLoadSprite::[getCurrentTime]末尾少写了,
  5. libclang: crash detected during parsing
libclang: crash detected during parsing: {
  'source_filename' : 'E:\iCOK\iCok0803\client\WeCGame\cocos2d/../IF/Classes/Ext/GUI/InputFieldMultiLine.h'
  'command_line_args' : ['-IE:\iCOK\iCok0803\client\WeCGame\tools\ndk/platforms/android-14/arch-arm/usr/include', '-IE:\iCOK\iCok0803\client\WeCGame\tools\ndk/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include', '-IE:\iCOK\iCok0803\client\WeCGame\tools\ndk/sources/cxx-stl/gnu-libstdc++/4.7/include', '-IE:\iCOK\iCok0803\client\WeCGame\tools\llvm-3.3\prebuilt\windows-x86_64/lib/clang/3.3/include', '', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos/2d', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos/ui', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos/base', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos/physics', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos/2d/platform', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos/2d/platform/android', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos/math/kazmath', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/extensions', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/extensions/assets-manager', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/external', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos/editor-support', '-IE:\iCOK\iCok0803\client\WeCGame\cocos2d/cocos/deprecated/CCDictionary.h', '-D_SIZE_T_DEFINED_', '-DGOOGLE_PROTOBUF_NO_THREADLOCAL', '-nostdinc', '-x', 'c++', '-std=c++11', '-U', '__SSE__', '-DANDROID', '-D__WCHAR_MAX__=0x7fffffff', '-U__MINGW32__', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/../libs/rapidxml', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/controller', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/Ext', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/Ext/GUI', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/Ext/wec', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/model', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/Net', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/Net/command', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/Net/command/alliance', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/Net/push', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/scene', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/scene/battle', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/scene/cropscene', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/scene/dragon', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/scene/loading', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/scene/world', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/view', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/view/dragonView', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/view/fogView', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/view/guide', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/view/guiExt', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/view/skill', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/view/transition', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/view/Widget', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/view/popup', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/../../libs/tersafe/include/', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/../../libs/protobuf_300/include/', '-IE:\iCOK\iCok0803\client\WeCGame\IF\Classes/../../IF/Classes/protobuf/'],
  'unsaved_files' : [],
  'options' : 0,
}

出错原因:头文件定义了一个未实现的函数InputFieldMultiLine::getStringNum(),将该函数删除即可。
貌似并不是这个原因。

  1. 忘记注册
    Lua绑定流程_第4张图片

    注册:
    LuaController::Init() {register_if_module();}
    lua_cocos2dx_if_manual::register_if_module(){register_all_cocos2dx_if_controller(tolua_S);}
  2. 创建


    Lua绑定流程_第5张图片
  3. 点击赐福后:actNode=null
    Lua绑定流程_第6张图片
  4. Not Receive Lucky Day NTF
    协议id有变化
  5. 解码失败decode error:
    忘记生成最新的pb。
  6. 明明当前类里有定义,还是报错未声明
    Classes\lua\WecLuaConversions.cpp(848) : error C2065: 'weclua_getStateDataByGroupIDForLua' : undeclared identifier
    原因:声明不能放在末尾。要先声明,再注册。

你可能感兴趣的:(Lua绑定流程)