Cocos2d-x v3.3 Lua Binding C + + class method summary

Source: Internet
Author: User

There are many cocos2d-x Lua-bound C + + class interface tutorials on the Web, and this article summarizes their experience. The key points of reference are http://cn.cocos2d-x.org/tutorial/show?id=1295,

The whole process step is very detailed, will compare the fool type, hoped to be useful to the novice reader entry.

Tutorial Basic Environment:

1. Using the engine is the v3.3 beta version

2. Development environment: OSX Yosemite

3. Cocos Code IDE

4. Xcode 6

1, on the Mac with Cocos code IDE to write Lua is more convenient, we use Coco code to create a new project, named Luabindingtest, choose Add Nativie Code (why choose this can go to understand), the project opened to run after the completion of new. If no problem, we take the next step.

2, with Xcode open the above project, select the iOS simulator run, the first run will have errors and abnormal problems, we modified: in Xcode-"general, the device orientation the portrait option to remove (the game generally horizontal screen or vertical screen), Keep only Langscape left/right, modify the applicationdidfinishlaunching method of AppDelegate.cpp, comment out initruntime (), Run, Should be able to see the picture.

3. Xcode, create a new class class (placed in the AppDelegate.cpp directory), name MClass, open CPP, we add some methods, MClass class to execute the binding process, and export the Lua interface.

MClass.h

#ifndef __luabindingtest__mclass__

#define __luabindingtest__mclass__

#include <stdio.h>

#include "cocos2d.h"

using namespace cocos2d;

class  Public ref{public:    bool  init ()    {        returnTrue ;    }    Create_func (MClass);     void Dolog ()    {        Cclog ("MClass binding succeeded! ");    }};

#endif/* Defined (__luabindingtest__mclass__) */

4. Back to the Project Finder, open the following directory,

。。。 Luabindingtest? Frameworks? Cocos2d-x? Tools? Tolua

We will see a lot of ini files and a genbindings.py,ini is the LUA binding configuration of the engine module, the genbindings.py script configures these existing INI files and then exports a C + + interface file (HPP and CPP files) exported to Lua. In order not to interfere with the existing LUA interface code of the engine, we use the gourd to draw a scoop and configure a LUA binding interface for our own project.  Copy Cocos2dx_spine.ini, rename to Game.ini, copy genbindings.py, rename to genbindings_game.py. All that remains is to modify the two files.

Game.ini

Modify the Place:

== =% (cocosdir) s/: /runtime-src/classes/==

genbindings_game.py

Modify Line 129th only

Cmd_args = {' Game.ini ' : (' game ', ' Lua_game_auto '), \

}

5. Terminal, CD to Tolua directory, Python genbindings_game.py.

If you encounter an error, please read the Readme.mdown file in the Tolua directory, and refer to the method above (especially note that the NDK r9b is required)

6. After the above process is successful we will get two files Lua_game_auto.cpp and LUA_GAME_AUTO.HPP,

Back to Xcode, import these two files and pay attention to importing into the Cocos2d_lua_bindings.xcodeproj Library project instead of Project Engineering!

Add Cocos2d_lua_bindings.xcodeproj's header Search Paths. (Little tricks, five..) )

7. Register our game module into the LUA environment, before registering, we need to add in Appdelegate, after v3.3, these module registration process all put in

Lua_module_register.h inside the Lua_module_register method

...
Main code # include
"lua_game_auto.hpp"intLua_module_register (lua_state*L) { //Dont ' Change the module register order unless your know what your is doingRegister_cocosdenshion_module (L); Register_network_module (L); Register_cocosbuilder_module (L); Register_cocostudio_module (L); Register_ui_moudle (L); Register_extension_module (L); Register_spine_module (L); Register_cocos3d_module (L);#ifCc_target_platform = = Cc_platform_android | | Cc_target_platform = = Cc_platform_iosRegister_audioengine_module (L);#endifLua_getglobal (L,"_g"); if(Lua_istable (l,-1))//Stack:...,_g,{register_all_game (L); } lua_pop (L,1); return 1;}

8. At this point, MClass's binding process is complete and we test it in Main.lua

Local test = Mclass:create ()

Test:dolog ()

Xcode runs to see if the log is printed, the pro-test is possible. If you want to run in Cocos code, need to compile once, the project right Cocos tools-"build Custom Runtimes (after the author operation, build prompt success, but run hint mclass for nil, unresolved, please advise)

9. Of course, it is not possible for us to write a single class to be exported to Lua, how to implement multiple C + + classes to be exported to Lua? The process is also very simple, where we can refer to the engine's practices such as Cocos2dx.ini, which is to configure multiple classes into the same INI file.

To avoid the need to introduce more than one C + + header file in the INI, we create a new lua_c_export.h file that will include the header file of the class that needs to be bound (the code that completes the three classes themselves) into the header file. Then the Game.ini file only configures this lua_c_export.h path, which avoids unnecessary modifications, knowing that the routing is very error-prone and confusing if multiple people collaborate. Refer to the following illustration

Game.ini

Terminal, CD to Tolua directory, Python genbindings_game.py. We call our bound C + + classes in Lua files, run

Print log, indicating that our multiple class bindings succeeded

The above is the entire LUA binding C + + class process, hope to be useful to the reader!

Cocos2d-x v3.3 Lua Binding C + + class method summary

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.