Use the cocos2d script to generate Lua binding

Source: Internet
Author: User


In the past few days, the boss needs to move dragonbones to cocos2dx 3.0 and bind the Lua interface. I just learned about Lua, and the engine I used has just changed from 2.2 to 3.0. I am not familiar with it. It took several days to finish it. I have time to summarize it.

In this article, let's talk about cocos2d's Lua binding modification. If you have time, write another article about how to register and callback to C ++ in Lua.

My directory structure

Assume that my directory name is dragonbones.

  • -Cocosdx directory
  • -Dragonbones
    • -C code
    • -C code Header
    • -Tools
      • Db_dragonbones.ini
      • Genbindings. py
    • -ScriptingThis directory is used to store automatically generated files.
Modification in genbindings. py
  • Project_root project path here I want to set the dragonbones directory as the project path, but my script run directory is under tools, so the settings are as follows:

    Project_root = OS. Path. abspath (OS. Path. Join (OS. Path. dirname (file ),'..'))

  • Cocos_root: Set the coco2dx project path

    Cocos_root = OS. Path. abspath (OS. Path. Join (project_root, '..', 'cocos2d-x '))

  • Set parsing script path this path is mainly used to configure the path to the cocos2d-x/tools/bindings-Generator/generator. py file

    Cxx_generator_root = OS. Path. abspath (OS. Path. Join (cocos_root, 'tools/bindings-generator '))

  • The configuration file path is actually the path of db_dragonbones.ini and the current. py file is in the same directory.

    Script_root = '% S/tool' % project_root

  • Set the directory of the generated file to the generated. HPP,. cpp, and Lua paths.

    Output_dir = '% S/scripting/LUA-bindings/auto' % project_root

  • Source file parameter to generate Lua binding

    Pai_args = {
    'Db _ dragonbones. ini ': ('db _ dragonbones',> 'lua _ dragonbones_auto '),

    • Db_dragonbones does not know what to use, but it must be the same as the name in the brackets in the first line of db_dragonbones.ini, which is generally the same as the INI file name.
    • Lua_dragonbones_auto defines the names of the generated HPP and CPP files.
INI File Modification
  • Method prefix

    Prefix = db_dragonbones

  • The namespace used in Lua is to put the bound class in a database table to prevent conflicts with other binding methods or classes.

    Target_namespace = DB

  • Original namespace in native code

    Cpp_namespace = dragonbones

    Note that, after this addition, we found that the method in the dragonbones namespace in native is not converted to DB in Lua because it needs to be in the cocos2d-x/tools/bindings-Generator/targets/Lua/conversions. conversion of namespace added to yaml

    "Dragonbones:": "DB ."

  • The header file contains all header files to be exposed. The reference directory is the cocos2dx directory to set the header file directory.

    Headers = % (cocosdir) S/../headers. h

  • Classes to be generated must be exposed to Lua.

    Classes = animationdata...

  • To skip the method that does not generate the Lua interface, I have a problem with the overload equal sign operator here. In this case, it would not have been called in Lua. It is better to change the method name to implement similar functions.

    Skip = animationdata: [operator =],

Almost all settings

I have to mention that the conversions. yaml file knows how to map the script creation process.

In the most important sentence, the interfaces and classes exposed to Lua should be as simple as possible, with fewer problems in the middle.

If you don't pay attention to it, there is no problem with writing interface compilation. When Lua calls it, it will tell you that it cannot be found.

Use the cocos2d script to generate Lua binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.