See "How quick-cocos2d-x 3.3 binds custom classes to LUA II" in the new project, we have completed the environment required for binding in our new project, and the next most critical step. Binds custom C + + classes to Lua.
In fact, in order to facilitate our response to different platforms, the engine has helped us create projects of different platforms. In order to edit the code conveniently, I opened the "Proj.win32" project under the "RUNTIME-SRC" folder using VS2013. go to item directory path "F:\Cocos2dx-Lua\Garfield\frameworks\runtime-src\Classes", I will create a custom class "Pet" Here, the code is as follows.
just a very simple pet class.
1. The wizard-like sprite that inherits from the engine definition;2. Define a static method create () by using the engine-defined macro Create_func (Pet);3. Two member functions Init (), Setpet ();4. Have a data member name.
very simple, we just want everything to start from the simple! Again, however, we have created the pet class in "F:\Cocos2dx-Lua\Garfield\frameworks\runtime-src\Classes" and we will use this path for a while. once you're ready to define the C + + class, let's review the previous section and, in our environment test, bind all C + + class files in the Cocos2d-x engine to Lua through the "genbindings.py" script. (while testing the environment, I did not specify this. But actually we run " genbindings.py ", in the final results, you can see the hint
" generating LUA bindings succeeds " , The smart reader must have guessed that what it does is bind C + + classes to Lua.
Next, for "genbindings.py", if you are not familiar with the Python language, please ignore the other code, just focus your gaze on 133 lines to 151 lines.
Specifies that the Tolua is in the same size without modification
Tolua_root = '%s/tools/tolua '% project_root
Specifies the binding of the binding script after the execution of a series of file output saved to the path
Although there is no need to modify it, we have to remember that the binding file we produce is output from
"F:\Cocos2dx-Lua\Garfield\frameworks\cocos2d-x\cocos\scripting\lua-bindings\auto"
Output_dir = '%s/cocos/scripting/lua-bindings/auto '% project_root
This array specifies some of the configuration parameters to execute the binding script, see the first line, ' Cocos2dx.ini ' is a configuration file, we can find it in the "genbindings.py" sibling directory. ' Cocos2d-x ' is actually a character constant specified in the first line of the ' cocos2dx.ini ' configuration file, primarily used to generate a binding bridge file, for example, conditional compilation ifndef and the generated registration custom class interface. I will mention this later in a moment, if you do not understand, please ignore it temporarily. ' Lua_cocos2dx_auto ' is the language name specified for the generated bound bridging file.
Cmd_args = {' Cocos2dx.ini ': (' cocos2d-x ', ' lua_cocos2dx_auto '), \
... ...
}
Understanding "genbindings.py", in its sibling directory you can find "cocos2dx.ini", This is a compounding document that is closely related to it . Open it and we'll do a little understanding.
Please focus on lines 1th, 4, 8, 25.
Do you remember the array we mentioned above?
Cmd_args = {' Cocos2dx.ini ': (' cocos2d-x ', ' lua_cocos2dx_auto '), \
... ...
}
Line 1th is the character content specified by the second argument.
Line 4th is a prefix that is used by conditional compilation ifndef and the generated registration custom class interface name, as we mentioned earlier.
Line 8 Specifies the namespace of the binding class
Section - line Specifies the path of the custom class header file that needs to be bound
With this in view, we will make some modifications to these two files to complete our class-to-LUA bindings.
First, we're copying a copy . ' Cocos2dx.ini ', name it as ' Custom_pet.ini ' . Make a copy of ' genbindings.py ' and name it ' custom_pet_genbindings.py '.
Change 'custom_pet.ini' 1th, 4, 8, 25 lines. As follows:
because we didn't specify a namespace, line 8th ' target_namespace ' to empty it.
and ' headers ' is just the relative path given. Remember "Pet" class where we were created?
It was in F:\Cocos2dx-Lua\Garfield\frameworks\runtime-src\Classes. "
ok! Next we change the " custom_pet_genbindings .py" Span style= "line-height:30px". Focus on line 133th to 138th and we'll make some changes here. As follows:
in cmd_args ' custom_pet.ini " custom_pet ' Lua_pet_auto '
Finally, we execute ' custom_pet_genbindings.py '. "Generating LUA bindings succeeds"!
See it, you should cheer up:). You have completed a C + + custom class-to-lua binding.
In the next section, let's explore how to invoke the Pet class in your project using LUA.
"Quick-cocos2d-x 3.3 How to bind custom classes to LUA III" bind custom classes to Lua