How to invoke Lua in coco2dx-3.0

Source: Internet
Author: User

A method of exporting classes to LUA with 3.0 of tools, and actively generating the code yourself.


Ever wanted to export C + + classes to Lua. You have to manually maintain the pkg file, it is simply a nightmare. After 3.0 you will feel life is very easy.



I'm going to go over the details below.


1, install the necessary libraries and kits, and configure the relevant environment variables, please follow Cocos2d-x-3.0rc0\tools\tolua\readme.mdown said to do, do not repeat.

2, write C + + class (I test is cocos2d-x-3.0rc0\tests\lua-empty-test\project\classes\helloworldscene.cpp)

3, write a generated Python script, you will not write, okay, we will Tiger
1) Enter the folder Cocos2d-x-3.0rc0\tools\tolua, copy the genbindings.py, named genbindings_myclass.py

2) make the generated folder into our project and open the genbindings_myclass.py

Output_dir= '%s/cocos/scripting/lua-bindings/auto '% project_root

?

Change into

Output_dir= '%s/tests/lua-empty-test/project/classes/auto '% project_root

3) Change the command parameters to

cmd_args={' Cocos2dx.ini ': (' cocos2d-x ', ' Lua_cocos2dx_auto '),                     ' Cocos2dx_extension.ini ': (' cocos2dx_extension ') , ' Lua_cocos2dx_extension_auto '),                     ' Cocos2dx_ui.ini ': (' cocos2dx_ui ', ' Lua_cocos2dx_ui_auto '),                     ' Cocos2dx_ Studio.ini ': (' Cocos2dx_studio ', ' Lua_cocos2dx_studio_auto '),                     ' Cocos2dx_spine.ini ': (' cocos2dx_spine ', ' Lua_ Cocos2dx_spine_auto '),                     ' Cocos2dx_physics.ini ': (' cocos2dx_physics ', ' Lua_cocos2dx_physics_auto '),                     }

Change into

cmd_args={' Myclass.ini ': (' MyClass ', ' Lua_myclass_auto ')}

4) At this point you may ask Myclass.ini where we are going to write this file.

The same principle. I'm still a tiger. Take Cocos2dx_spine.ini to change.

[myclass]# the prefix to being added to the generated functions. You might or might not use this in your own# Templatesprefix = MyClass # Create a target namespace (in JavaScript, this W Ould create some code like the equiv. to ' NS = NS | | {} ') # All classes'll be embedded on that namespacetarget_namespace = Android_headers =-i% (androidndkdir) s/platforms/an droid-14/arch-arm/usr/include-i% (Androidndkdir) s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include-i % (androidndkdir) s/sources/cxx-stl/gnu-libstdc++/4.7/includeandroid_flags =-d_size_t_defined_ Clang_headers =-I% ( Clangllvmdir) S/lib/clang/3.3/include clang_flags =-nostdinc-x C + +-std=c++11 cocos_headers =-I% (cocosdir) s/cocos-i% ( Cocosdir) s/cocos/2d-i% (cocosdir) s/cocos/base-i% (cocosdir) s/cocos/ui-i% (cocosdir) s/cocos/physics-i% (cocosdir) s/ cocos/2d/platform-i% (Cocosdir) s/cocos/2d/platform/android-i% (cocosdir) s/cocos/math/kazmath-i% (cocosdir) s/ extensions-i% (Cocosdir) s/external-i% (cocosdir) s/cocos/editor-support-i% (cocosdir) s cocos_flags =-dandroid-dcocos2d_javascript cxxgenerator_headers = # extra arguments for Clangextra_arg uments =% (android_headers) s% (clang_headers) s% (cxxgenerator_headers) s% (cocos_headers) s% (android_flags) s% (clang_ Flags) s% (cocos_flags) s% (extra_flags) s # what headers to Parseheaders =% (Cocosdir) S/tests/lua-empty-test/project/clas Ses/helloworldscene.h # What classes to produce code for. You can use the regular expressions here. When testing the regular# expression, it'll be is enclosed in ' ^$ ', like this: ' ^menu*$ '. Classes = HelloWorld # what Shoul D we skip?  In the format classname::[function function]# ClassName are a regular expression, but would be used like this: "^classname$" Functions is also# regular expressions, they is not being surrounded by "^$". If you want to skip a whole class, just# add a single "*" as functions. See bellow for several examples. A Special class name is "*", which# would apply to all class names. This was a convenience wildcard to be able to skip similar named# functions from all classes. Skip = Rename_functions = Rename_classes = # for all class names, should we remove something when registering in the TA Rget Vm?remove_prefix = # classes for which there would be no "parent" lookupclasses_have_no_parents = # base Classes WHI CH would be skipped if their sub-classes found Them.base_classes_to_skip = Ref Processbase # classes that create no cons tructor# Set is special and we'll use a hand-written constructorabstract_classes = # Determining whether to use script Object (JS object) to control the lifecycle of Native (CPP) object or the other-the-around. Supported values are ' yes ' or ' no '. Script_control_cpp = no

How to use some of the parameters in. INI: Name: simply name. Prefix: The last generated file will be prefixed with this name. such as Prefix.cpp, PREFIX.HPP, prefix_api.jsclasses: The name of the class you want to convert. Must be the entire class in the imported header file, where you can use the regular form to add multiple classes. Cocox2ds.ini. Extra_arguments: The number of system parameters required for some interfaces. such as the clang package, the introduction of the Android NDK package required to join the system, the wording can be used in the above three. ini. Headers: The path of the header file that you need to bind. Target_namespace: Name space.

The class of the last generated JS file starts with this namespace. For example, your class is SQLite, the namespace is COCOS2DX, then the last generation is cocos2dx.sqlite. Rename_functions: The ability to change the name of the method you want to bind to what you want. Ability to change multiple. Separated by commas, the notation sqlitecpp::[sqlite3_execcpp=sqlite3_exec], this is to rename the Sqlite3_ Execcpp method in Sqlitecpp to Sqlite3_exec method.

Rename_classes: Ibid. Rename the class.

Skip: Skip the methods and classes that you do not need to bind, so you do not generate them.

You should pay attention to these lines when you change.

[Myclass]prefix = Myclasstarget_namespace =headers =% (Cocosdir) s/tests/lua-empty-test/project/classes/ helloworldscene.hclasses = Helloworldskip =abstract_classes =

4, the following to own the initiative to generate code. Open the command line tool, CD to Cocos2d-x-3.0rc0\tools\tolua. Knock

Python genbindings_myclass.py

?

tid=196416# "class=" Toolbar_item command_help Help "style=" outline:0px!important; Text-decoration:none!important; Color:white!important; Margin:0px!important; padding:1px 0px 0px!important; Border:0px!important; Bottom:auto!important; Float:none!important; Height:auto!important; Left:auto!important; Line-height:1.1em!important; Overflow:visible!important; Position:static!important; Right:auto!important; Text-align:center!important; Top:auto!important; Vertical-align:baseline!important; Width:auto!important; Font-size:1em!important; Min-height:inherit!important; Display:block!important ">

enter execution. Assuming there's no problem in front of you, you'll have one more directory auto in cocos2d-x-3.0rc0\tests\lua-empty-test\project\classes, then generate Lua_myclass_auto.cpp and Lua_ inside. MYCLASS_AUTO.HPP add to the project

5. Add Lua to the module we generated when the scripting engine was initialized.


Edit AppDelegate.cpp, including lua_myclass_auto.hpp header file, in

luaengine* engine = Luaengine::getinstance ();

tid=196416# "class=" Toolbar_item command_help Help "style=" outline:0px!important; Text-decoration:none!important; Color:white!important; Margin:0px!important; padding:1px 0px 0px!important; Border:0px!important; Bottom:auto!important; Float:none!important; Height:auto!important; Left:auto!important; Line-height:1.1em!important; Overflow:visible!important; Position:static!important; Right:auto!important; Text-align:center!important; Top:auto!important; Vertical-align:baseline!important; Width:auto!important; Font-size:1em!important; Min-height:inherit!important; Display:block!important "?

Back add

Register_all_myclass (Engine->getluastack ()->getluastate ());

6. Compile and execute. This helloworld the class is exported to Lua.



Test------------------------------------------------
Open Hello.lua, edit the local function main ()

Change the front into

Localfunctionmain ()   --Avoid memory leak   collectgarbage ("Setpause")   collectgarbage ("Setstepmul",    Localhello = helloworld:create ()   localscenegame = cc. Scene:create ()   scenegame:addchild (hello)   cc. Director:getinstance (): Runwithscene (Scenegame)    if (1==1) then        return   end .....

If you still don't know what it means, go to the test http://www.tairan.com/archives/5493

 

How to invoke Lua in coco2dx-3.0

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.