Create a C + + custom class to let Lua script invoke

Source: Internet
Author: User
Tags lua

This will introduce the use of tolua++ to embed a custom C + + class so that the Lua script uses

General process:

Custom classes, using the tolua++ tool, are compiled into LuaCoco2d.cpp. Lua calls

Step one: Customize a C + + class, I define a class named: MySprite

MySprite.h

MySprite.cpp

Step two: Create a. pkg file according to the custom class, we define the custom Mysprite class into the. pkg file

Note: 1>, as long as it is based on the contents of the custom class. h, as for the. CPP implementation, LUA automatically calls the class's function after binding

2>, write a. pkg file with a few rules in mind, we'll find the Readme file in the tolua++ folder

    

1. Generating the lua<-->c bindings with tolua++

Build scripts for Windows (BUILD.bat) and Unix (build.sh) is provided
To generate the relevant files after modifying the. pkg files. These
Scripts basically run the following command:

Tolua++.exe-l Basic.lua-o LuaCocos2d.cpp cocos2d.pkg

This would generate the bindings file and patch it with come cocos2dx
Specific modifications.

On POSIX systems can also just run ' make ' to build the bindings
If/when you change. pkg files.

2. Writing. pkg files

1) enum keeps the same
2) Remove Cc_dll for the class defines, pay attention to multi inherites
3) Remove inline keyword for declaration and implementation
4) Remove public protect and private
5) Remove the decalration of class member variable
6) Keep Static keyword
7) Remove Memeber functions that declared as private or protected

Here are the detailed writing rules for. pkg files

Here we directly write a mysprite.pkg file, which reads as follows:

Class Mysprite:public ccsprite{

Static MySprite * CREATEMS (const char * fileName);

}

After you have written our. pkg file, put the file in the tolua++ folder.

Step Three: Configure the tolua++ tool, compile the. pkg file we wrote with tolua++, embed the custom class in LuaCocos2d.cpp

We found tolua++ under the tolua++ folder. Mac.zip file, and unzip it to get a tolua++ tool,

After extracting the tool, we are configuring the path of the tolua++, using Xcode to open the build.sh file, change the two places:

After the change, depending on the path of each computer, I changed to:

Here Tolua is the location of the tolua++ tool (with the path followed by/tolua++, which represents the tool), the following configuration is the location of the post-compilation LuaCocos2d.cpp file export

Finally, we register the defined. pkg file with the cocos2d.pkg in the tolua++ folder

When the above is done, we use the terminal directly to the tolua++ folder and execute the tolua++ tool using the Make command.

Ok! After executing the command, the LuaCocos2d.cpp file is generated on the desktop and the custom class is already binding

Replace the newly generated LuaCocos2d.cpp with the LuaCocos2d.cpp file in our generated project

Note: While the newly generated LuaCocos2d.cpp has already been binding our custom class but does not refer to our header file, we pour our custom class name into the LuaCocos2d.h

Step four: Test our custom classes in Lua

Save, Command + R, run successfully and invoke the custom class:

Create a C + + custom class to let Lua script invoke

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.