Cocos2d-Lua 3.12 生成绑定 导出C++类给LUA调用( tolua genbindings.py 的使用)

前端之家收集整理的这篇文章主要介绍了Cocos2d-Lua 3.12 生成绑定 导出C++类给LUA调用( tolua genbindings.py 的使用)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

工具:Python、pyyaml、pyCheetah都用32位版本(可避免很多错误)

(ps:tolua目录下README.mdown里有下载链接

frameworks\cocos2d-x\tools\tolua\README.mdown


例子:

MyClass.h

  1. #include"cocos2d.h"
  2. usingnamespacecocos2d;
  3. classMyClass:publicRef
  4. {public:
  5. MyClass(){};
  6. ~MyClass(){};
  7. boolinit(){returntrue;};
  8. CREATE_FUNC(MyClass);
  9. intfoo(inti);
  10. };


MyClass.cpp

#include"MyClass.h"

  • intMyClass::foo(inti)
  • {
  • returni+100;
  • }

  • 生成所需的配置文件 MyClass.ini 重点提示 可从其他.ini文件复制修改 [MyClass]
    # the prefix to be added to the generated functions. You might or might not use this in your own
    # templates修改
    prefix =MyClass

    # all classes will be embedded in that namespace修改
    target_namespace =my
    #修改
    #headers = %(cocosdir)s/../runtime-src/Classes/MyClass.h
    headers =MyClass.h

    #修改
    classes =MyClass


    生成frameworks\cocos2d-x\tools\tolua\MyGenbindings.py

    tolua_root = '%s/tools/tolua' % project_root
    output_dir = '%s/tools/tolua/auto' % project_root 输出目录,可自定义,根据需要

    cmd_args = {#'cocos2dx.ini' : ('cocos2d-x','lua_cocos2dx_auto'),\
    #'cocos2dx_extension.ini' : ('cocos2dx_extension','lua_cocos2dx_extension_auto'),\
    #'cocos2dx_ui.ini' : ('cocos2dx_ui','lua_cocos2dx_ui_auto'),\
    #'cocos2dx_studio.ini' : ('cocos2dx_studio','lua_cocos2dx_studio_auto'),\
    #'cocos2dx_spine.ini' : ('cocos2dx_spine','lua_cocos2dx_spine_auto'),\
    #'cocos2dx_physics.ini' : ('cocos2dx_physics','lua_cocos2dx_physics_auto'),\
    #'cocos2dx_experimental_video.ini' : ('cocos2dx_experimental_video','lua_cocos2dx_experimental_video_auto'),\
    #'cocos2dx_experimental.ini' : ('cocos2dx_experimental','lua_cocos2dx_experimental_auto'),\
    #'cocos2dx_controller.ini' : ('cocos2dx_controller','lua_cocos2dx_controller_auto'),\
    #'cocos2dx_cocosbuilder.ini': ('cocos2dx_cocosbuilder','lua_cocos2dx_cocosbuilder_auto'),\
    #'cocos2dx_cocosdenshion.ini': ('cocos2dx_cocosdenshion','lua_cocos2dx_cocosdenshion_auto'),\
    #'cocos2dx_3d.ini': ('cocos2dx_3d','lua_cocos2dx_3d_auto'),\
    #'cocos2dx_audioengine.ini': ('cocos2dx_audioengine','lua_cocos2dx_audioengine_auto'),\
    #'cocos2dx_csloader.ini' : ('cocos2dx_csloader','lua_cocos2dx_csloader_auto'),\
    #'cocos2dx_experimental_webview.ini' : ('cocos2dx_experimental_webview','lua_cocos2dx_experimental_webview_auto'),\
    #'cocos2dx_physics3d.ini' : ('cocos2dx_physics3d','lua_cocos2dx_physics3d_auto'),\
    #'cocos2dx_navmesh.ini' : ('cocos2dx_navmesh','lua_cocos2dx_navmesh_auto'),\

    'MyClass.ini' : ('MyClass','lua_MyClass_auto'),根据需要添加
    }

    运行pythonmygenbindings.py 即生成输出目录frameworks\cocos2d-x\tools\tolua\auto

    遇到问题参考:

    一、How to Use bindings-generator
    ==================
    On Windows:
    ------------
    * Make sure that you have installed `android-ndk-r10c` or later.
    * Download python2.7.3 (32bit) from (http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi).
    * Add the installed path of python (e.g. C:\Python27) to windows environment variable named 'PATH'.
    * Download pyyaml from http://pyyaml.org/download/pyyaml/PyYAML-3.11.win32-py2.7.exe and install it.
    * Download pyCheetah from https://raw.github.com/dumganhar/my_old_cocos2d-x_backup/download/downloads/Cheetah.zip,unzip it to "C:\Python27\Lib\site-packages"
    * Set environment variables `NDK_ROOT` and `PYTHON_BIN`
    * Go to "cocos2d-x/tools/tolua" folder,and run "genbindings.py". The generated codes will be under "cocos\scripting\auto-generated\lua-bindings".


    二、python version 2.7 required,which was not found in the registry

    方法:新建一个register.py文件,把一下代码贴进去,保存

    #

    # script to register Python 2.0 or later for use with win32all

    # and other extensions that require Python registry settings

    # written by Joakim Loew for Secret Labs AB / PythonWare

    # source:

    # http://www.pythonware.com/products/works/articles/regpy20.htm

    # modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html

    importsys

    from_winreg*

    # tweak as necessary

    version = sys.version[:3]

    installpath = sys.prefix

    regpath ="SOFTWARE\\Python\\Pythoncore\\%s\\"% (version)

    installkey ="InstallPath"

    pythonkey ="PythonPath"

    pythonpath ="%s;%s\\Lib\\;%s\\DLLs\\"% (

    installpath,installpath,installpath

    )

    defRegisterPy():

    try:

    reg = OpenKey(HKEY_CURRENT_USER,regpath)

    exceptEnvironmentError as e:

    reg = CreateKey(HKEY_CURRENT_USER,regpath)

    SetValue(reg,installkey,REG_SZ,installpath)

    CloseKey(reg)

    :

    print"*** Unable to register!"

    return

    "--- Python",version,"is now registered!"

    if(QueryValue(reg,installkey) == installpathand

    QueryValue(reg,pythonkey) == pythonpath):

    "=== Python""is already registered!"

    return

    "*** You probably have another Python installation!"

    __name__=="__main__"RegisterPy()

    win7是 64的原因,在安装python(32位)时,如果选择只为当前用户,以上问题是不会出现的,如果选择所有用户,那就用上面的方法解决吧。

    三、TranslationUnitLoadError: Error parsing translation unit. 的提示错误,基本都是.ini文件没有配置正确,仔细检查一下 .ini文件里的 “headers =”指向的路径是否正确

    四、ImportError: No module named Cheetah.Template

    错误提示就知道缺少cheetah库,所以先从http://pythonhosted.org//Cheetah/这个地址下载库,打开终端,cd 到下载文件夹的目录,

    输入命令:sudopythonsetup.py install即可

    如果是windows提示这个错误,那么也是下载Cheetan这个库,然后安装


    五、

    使用PYTHONPATH环境变量,在这个环境变量中输入相关的路径,不同的路径之间用逗号(英文的!)分开,如果PYTHONPATH 变量还不存在,可以创建它!


    这里的路径会自动加入到sys.path中,而且可以在不同的python版本中共享,应该是一样较为方便的方法


    unzip it to "C:\Python27\Lib\site-packages" 若运行出现错误,可以

    设置环境变量:PYTHONPATH

    D:\AndroidWin764\Python27\libs\site-packages

    如何使用生成文件

    一、生成文件添加至项目中,编辑AppDelegate.cpp文件

    修改applicationDidFinishLaunching方法register_all_*加上生成的的类名。必须放在engine->executeScriptFile前面。

    // register lua module
    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);
    lua_State* L = engine->getLuaStack()->getLuaState();
    lua_module_register(L);

    //frameworks\cocos2d-x\tools\tolua\auto\lua_MyClass_auto.cpp

    //register_all_MyClass(lua_State* tolua_S)

    register_all_MyClass(engine->getLuaStack()->getLuaState());


    register_all_packages();

    if (engine->executeScriptFile("src/main.lua"))
    {
    return false;
    }

    然后,可以编辑LUA脚本了。

    原文链接:https://www.f2er.com/cocos2dx/338256.html

    猜你在找的Cocos2d-x相关文章