I understand Cocos2d-x 3.6 lua--Cocos How to bind LUA custom classes

Source: Internet
Author: User
Tags python script

Cocos2d-x 2.x vs Cocos2d-x 3.x difference (tolua++)


Cocos2d-x in the 2.x version is using tolua++ and. pkg files to register themselves in the LUA environment, but starting from Cocos2d-x 3.x , with The Bindings-generator script replaces the tolua++.


    The working mechanism of the Bindings-generator script is:
        1, do not write. pkg and. h files, directly define aINIFileregistering with the LUA environmentWhat the module name is, just do it.
        2. The method of generating the tolua++ tool is determined, and the Python script dynamically parses the C + + class and automatically generates the bridged. h and. CPP code without invoking the tolua++ command.

3, although the tolua++ command is no longer called, but the underlying still use tolua++ library functions, such as Tolua_function,bindings-generator script generated code with the use of tolua++ Tools generate almost the same


    The Bindings-generator script mastered the initiative to generate the tolua++ Bridge code, Not only can you save a lot of. Pkg and. h files, but you can better insert custom code to achieve some special purposes in the COCOS2D-X environment, such as memory recycling, so Cocos2d-x has given up the tolua++ from 3.x. and. Pkg instead of using their own bindings-generator scripts is a very commendable clever practice.

    Next, how to use the Bindings-generator script:
        1, write their own C + + class, according to Cocos2d-x rules, inherit the Cocos2d::ref class, in order to use the Cocos2d-x memory recovery mechanism.
        2. Write an. ini file so that bindings-generator can tell how the C + + class is exposed based on this configuration file.
        3, modify the Bindings-generator script, let it read this. ini file
        4, execute Bindings-generator script, generate bridge C + + class method
        5. Use VS2012 to add the custom C + + class and the generated bridging file to the project, otherwise it will not compile
        6, modify the AppDelegate.cpp, execute the bridging method, the custom C + + class is registered into the LUA environment.

    The first is the custom C + + class. I'm used to saving files inframeworks/runtime-src/classes/Directory:

Frameworks/runtime-src/classes/myclass.h

#include "cocos2d.h" using namespace Cocos2d;class myclass:public ref{public:  MyClass ()   {};  ~myclass ()  {};  BOOL Init () {return true;};  Create_func (MyClass);  int foo (int i);};
    Frameworks/runtime-src/classes/myclass.cpp

#include "MyClass.h" int myclass::foo (int i) {  return i + 100;}
    Then write the. ini file. In the frameworks/cocos2d-x/tools/tolua/directory you can see the genbindings.py script and a lot of. ini files, these are the actual execution environment of Bindings-generator. Find a little. ini file, copy it, and rename it to Myclass.ini. Most of the content can be done without change, here only the important parts that must be changed:
    Frameworks/cocos2d-x/tools/tolua/myclass.ini

[Myclass]prefix           = mineclass # Add the prefix name xx, register the file header and register function name prefix name (XX) combination named Target_namespace = my # space named, when called, to My.xxx, XXX is the method of the custom class headers          =% (Cocosdir) s/: /runtime-src/classes/myclass.h    # Gets the file header of the custom class Classes          = MyClass                                          # requires registration of the class yy (method), while the Register function name prefix name (XX) _yy combination named
    frameworks/cocos2d-x/tools/tolua/genbindings.py
Cmd_args = {' Cocos2dx.ini ': (' cocos2d-x ', ' Lua_cocos2dx_auto '),             ' Myclass.ini ': (' MyClass ', ' Lua_myclass_auto '), c7/> ...
    (In fact, this step can be omitted, as long as the genbindings.py script automatically search all INI files in the current directory, do not know the future Cocos2d-x team will not be optimized)
At this point, the preparation for bridging the file is done, and the genbindings.py script is executed:

Python genbindings.py
    After the genbindings.py script is executed successfully, the newly generated file is seen in the frameworks/cocos2d-x/cocos/scripting/lua-bindings/auto/directory:


        


Note: If Python error, see if there is a lack of yaml, cheetah package, if, install package on the line.

After a brief explanation, the lua_myclass_auto.cpp is translated into

Tolua_api int Register_all_mineclass (lua_state* tolua_s) {///entry point, which creates a managed internal variable tolua_open (tolua_s);//Create a new module Tolua_module ( tolua_s, "My", 0);//Register a module or class Tolua_beginmodule (tolua_s, "my");//Class Registration Lua_register_mineclass_myclass (tolua_s); Tolua_ Endmodule (tolua_s); return 1;}
    Registration class:

int Lua_register_mineclass_myclass (lua_state* tolua_s) {    tolua_usertype (tolua_s, "MyClass");//Registered user type    Tolua _cclass (tolua_s, "MyClass", "MyClass", "CC". Ref ", nullptr); Registration class    Tolua_beginmodule (tolua_s, "MyClass");//Register Module        tolua_function (tolua_s, "new", Lua_mineclass_myclass_ constructor); Bind function (Bind the "new" of the MyClass object inside Lua to your lua_mineclass_myclass_constructor () function.)        Tolua_function (tolua_s, "Init", lua_mineclass_myclass_init);        Tolua_function (tolua_s, "foo", Lua_mineclass_myclass_foo);        Tolua_function (tolua_s, "create", lua_mineclass_myclass_create);    Tolua_endmodule (tolua_s);    std::string typeName = typeid (MyClass). Name ();//Save Registration class    G_luatype[typename] = "MyClass";    g_typecast["MyClass"] = "MyClass";    return 1;}
    To bind a function, note the following:

CObj = (myclass*) tolua_tousertype (tolua_s,1,0);//is to bounce the object under the data stack as a pointer (ctest*).
    The object is popped from the stack (which is actually the object of the custom class), and the object reference method (including parameters and handle) is pushed back to the stack to achieve the call between C and Lua.


2. Compile and run

Open classes/lua_module_register.h file, add header file

#include "tolua++/lua_myclass_auto.hpp"
    Add a registration function to the static int lua_module_register (lua_state* L)

Register_all_mineclass (L);
    If vs2012 compile error, it is estimated that the source files and generated files are not added to the project; For special handling, such as replacing the genbindings.py script to generate a file path, note that in the VS2012 environment, the properties->c/c++-> additional Include directories, add paths.

Lua Code:

function Myadd (x, y)--custom local test = My. Myclass:create () print ("Lua bind:"). Test:foo ()    return x + yend
Compile run:



Citation blog: http://segmentfault.com/a/1190000000631630

Cited article: http://cn.cocos2d-x.org/tutorial/show?id=2496

I understand Cocos2d-x 3.6 lua--Cocos How to bind LUA custom classes

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: info-contact@alibabacloud.com 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.