Online also by a lot of similar blog to describe how to use the custom C + + file in Lua, but in the specific operation will encounter a variety of egg pain problems, here the specific steps and to note the place to record, convenient for others also convenient for themselves.
At first I also explained that I was using a Mac machine.
First of all, quick-cocos2d-x3.3final in the binding C + + document Frameworks/cocos2d-x/tools/tolua/readme.mdown, which contains a description of how to bind C + + files.
1, if the OS is not loaded Python, to http://brew.sh/, insert homebrew, you can also copy the following code directly to the terminal execution:
Ruby-e "$ (curl-fssl https://raw.githubusercontent.com/Homebrew/install/master/install)"
After execution, install the documentation instructions to execute the installation python
Brew Install Python
2, through the PIP, install Python dependencies.
First insert Pip
sudo easy_install pip
If this is the first time you need to provide your account password.
Insert the Pyyaml bag and take care not to use sudo
Pip Install Pyyaml
If the following action prompts no find Yaml, execute:
Pip Uninstall Pyyaml
Remove Pyyaml and re-execute the insert Pyyaml.
Then insert Cheetah:
sudo pip install Cheetah
3, although the Ndk_root path has been set up before, in order to later unnecessary error, here is to set up a bit, but also to reset the path of the Python_bin.
If you use the default method of inserting PYTHON, python_bin=/usr/local/bin/python2.7, but it may not be the same, you also need to check.
4. Create an. ini file, copy a copy of Cocos2d-x.ini, and rename it to Lua_custom_api.ini to modify the contents:
[lua_custom_api]prefix = lua_custom_ Apitarget_namespace = headers =% (cocosdir) s/my/myclass.hclasses = Myclass.*skip = Rename_functions = rename_classes = Remove_prefix = classes_have_no_parents = Base_classes_to_skip = abstract_classes =
If we created a MyClass.h and a MyClass.cpp, and placed it under the Frameworks/cocos2d-x directory under my
Target_namespaced is the namespace used by C + + files, something like CC.
The meaning of the specific parameters can be viewed in the Cocos2d-x.ini file for explanations.
5. Re-create a genbindings.py
Since we just added the C + + file, and did not change the cocos2d-x code, there is no need to bind the Cocos2d-x code again. Go to directory frameworks/cocos2d-x/tools/tolua/,copy a copy of genbindings.py, and name it custom_genbindings.py
Put the contents of it:
1Cmd_args = {'Cocos2dx.ini': ('Cocos2d-x','Lua_cocos2dx_auto'), 2 'Cocos2dx_assetsmanager.ini': ('Cocos2dx_assetsmanager','Lua_cocos2dx_assetsmanager_auto'), 3 'Cocos2dx_extension.ini': ('cocos2dx_extension','Lua_cocos2dx_extension_auto'), 4 'Cocos2dx_ui.ini': ('cocos2dx_ui','Lua_cocos2dx_ui_auto'), 5 'Cocos2dx_studio.ini': ('Cocos2dx_studio','Lua_cocos2dx_studio_auto'), 6 'Cocos2dx_spine.ini': ('Cocos2dx_spine','Lua_cocos2dx_spine_auto'), 7 'Cocos2dx_physics.ini': ('Cocos2dx_physics','Lua_cocos2dx_physics_auto'), 8 'Cocos2dx_experimental_video.ini': ('Cocos2dx_experimental_video','Lua_cocos2dx_experimental_video_auto'), 9 'Cocos2dx_experimental.ini': ('Cocos2dx_experimental','Lua_cocos2dx_experimental_auto'), Ten 'Cocos2dx_controller.ini': ('Cocos2dx_controller','Lua_cocos2dx_controller_auto'), One 'Cocos2dx_cocosbuilder.ini': ('Cocos2dx_cocosbuilder','Lua_cocos2dx_cocosbuilder_auto'), A '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'), the 'Cocos2dx_csloader.ini': ('Cocos2dx_csloader','Lua_cocos2dx_csloader_auto'), -}
Change to:
1 Cmd_args = {'lua_custom_api.ini' : ('lua_custom_api') lua_custom_api_auto'2 }
So when we execute./custom_genbindings.py, we only bind our custom C + + files.
6, Enter the directory frameworks/cocos2d-x/tools/tolua/, execute./custom_genbindings.py
CD frameworks/cocos2d-x/tools/tolua/./custom_genbindings.py
Be sure to enter the Tolua directory, or the execution will fail.
If successful, the Lua_custom_api_auto.hpp/cpp two files are generated in the Cocos/scripting/lua-bindings/auto directory and generated in the API directory Lua_custom_api_auto_ Api.lua documentation.
7. Introduction of Lua_custom_api_auto
Xcode Open Project Project, select Cocos2d_lua_bindings Project Project, the lua_custom_api_auto.hpp/cpp these two files into the auto directory
Open build phases, put. cpp under Complie sources,. HPP put under headers:
8. Introduction of custom C + + files
Select the Cocos2d_libs project, similar to the introduction of MyClass.h and MyClass.cpp files.
9. Using custom C + + files
Open the AppDelegate.cpp file for our project and add the following code to the appropriate location:
1 Lua_state *state = stack->getluastate (); 2 " _g " ); 3 Register_all_custom_api (state); 4 1);
Of course, don't forget to introduce LUA_CUSTOM_API_AUTO.HPP:
#include "lua_custom_api_auto.hpp"
Recompile the cocos2d_lua_bindings, Cocos2d_libs project, so you can use the custom C + + class.
The Mac version of LUA uses custom C + + files