Cocos2dx-lua Development of C + + bound to Lua

Source: Internet
Author: User
Tags lua python script

A. Brief introduction

This article describes the fact that in a real game development project, binding a custom C + + class to Lua allows Lua to invoke the C + + class. Creates a Python script that executes the Python script to automatically bind our C + + classes to Lua. Generate the code we want. The article describes the configuration environment, scripting, test classes, calls in the actual project, and some of the problems encountered

Two The horses did not move the fodder first

I am currently using the Quicklua 3.3 win7 system. Download the tools you need

Can take a look at \tools\tolua \readme.mdown inside has detailed introduction, and all have

I am now the win system, translation

(1). Make sure your ndk is android-ndk-r9b, with environment variables

(2). Python 2.7.3 (32bit), configure environment variables, check online

Install Pyyaml my previous installation failed because the Python version was incorrect and then changed to the

(3). Pycheetah extract to your Python file under Lib\site-packages

Refer to http://cn.cocos2d-x.org/tutorial/show?id=2518 for details

Three Create a header file class CustomHeaders.h

Creating a class in the \frameworks\runtime-src\classes of a project is where the project is put classes.

Batch binding when used, the specific role will be described later, you can create an example

/*
Manually add a class header file that needs to be bound to Lua
*/
#ifndef __customheaders_h__
#define __customheaders_h__
//... Import the header file of the class we need to bind
#include "MyTestClass.h"//example
#include "MyTestClass2.h"//example

#endif

Four Several test classes

We need classes that are bound to Lua. Here I have written two test classes Mytestclass and MyTestClass2. Such as

MyTestClass2.h

#ifndef __mytestclass2_h__
#define __mytestclass2_h__
#include "Cocos2d.h"
Class MyTestClass2
{
Public
static int gettestdata ();
Private
};
#endif

MyTestClass2.cpp

#include "MyTestClass2.h"
int Mytestclass2::gettestdata ()
{
int a = 22222;
return A;
}

Two of them are all the same. is to return an integer.

Five Write a Python script, and an INI

A copy of the genbindings.py under the \tools\tolua folder. We re-named the py we used.

Copy one of the INI files under the \tools\tolua folder. We can re-name the config file we used, like Myclass.ini.

I should become a myclass_genbindings.py here. Name casually. There are two places to change and find.

(1) Modify PY

1.output_dir = '%s/cocos/scripting/lua-bindings/auto '% project_root

Change the output path to our own classes folder. You can write your own path based on your own path, for example:

Output_dir= '%s/project/cardgame/frameworks/runtime-src/classes '% project_root.

The path of the assembly. Project_root the current project directory, then add your own classes path to the back

Of course, you can create an auto folder under the Classes folder to hold the automatically generated files, which I did not create

2.

Cmd_args = {' Cocos2dx.ini ': (' cocos2d-x ', ' Lua_cocos2dx_auto '), ...
}

Change to:

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

The first parameter is the previous INI file. The second one needs to be used later. The third one is the name of the script-generated file Lua_myclass_auto

(2) Modify INI

1. Change the first line to [the second one above] for example [MyClass]

2.prefix = MyClass

3.target_namespace = What is changed into Target_namespace =

Note: The following will be referred to = NULL or = other reason

4.headers = What is changed to the CustomHeaders.h path we wrote above, we can stitch our path according to the path we already have.

such as:% (Cocosdir) s/project/cardgame/frameworks/runtime-src/classes/customheaders.h

5, classes = We need to bind the name of the class, such as

Classes = Mytestclass MyTestClass2 can support multiple. You need to import the header file of the custom class in the CustomHeaders.h file, and then add the class name after classes= to

It's OK. We CD into the myclass_genbindings.py script folder and execute the script Python myclass_genbindings.py to automatically bind the pull.

Tip: In the folder, press and hold shift+ right mouse button, you can see here to open the Command window, more convenient,

Six. The above write INI simple introduction

1, must first understand the regular expression, Baidu Direct search Regular expression

2, key parameters
prefix related to the first name of the LUA function, such as PREFIX=COCOS2DX, then the generated code is to start with LUA_COCOS2DX
Target_namespace Surface These LUA functions register to which namespace, in Lua, is the representative registered to that table, such as Target_namespace=custom, then the call in Lua is custom. At the beginning (but this parameter seems to be not controlled, the real control is in C + + whether there is a custom namespace)
There are two points to note
(1), if there is a custom namespace, COCOS2DX home directory under the Tools folder Bindings-generator\targets\lua under the Conversions.yaml (JS is the bindings-generator\targets\ SpiderMonkey conversions.yaml) Add your own namespace:
Ns_map:
"cocos2d::extension::": "CC."
"Cocos2d::ui::": "Ccui."
"cocos2d::": "CC."
"Spine::": "sp."
"Cocostudio::": "CCS."
"Cocosbuilder::": "CC."
"Cocosdenshion::": "CC."
"Custom::": "Custom."
This file seems to be some kind of replacement operation configuration (that is, to replace the set of strings in a C + + file), you know it by yourself.

(2) If your code does not have a namespace, it is recommended that you get one, or you'll encounter a type in a C + + class

Scoop 鱙 Class A function parameter or return value to use the state type, in the generation process report error, error message said there is no namespace ... So this is the 1th place to add a line: "A::": "A." The information. In the case of namespaces, the generator passes directly. Here is a strange question.

Headers fill in the header file of the code you wrote

classes fill in the classes to be generated, support multiple classes, support regular expressions, such as classes = a,^a,^a$

Skip to specify the masking function, A function that does not need to be exposed to LUA, supports regular expressions, such as Skip = Sprite::[getquad getblendfunc ^setposition$ setblendfunc]

Rename_functions Renaming a function name exposed to LUA (typically exposed to LUA in C + + writing function names), such as Rename_functions = Spriteframecache::[addspriteframeswithfile=addspriteframes Getspriteframebyname=getspriteframe],
is the original function name before the equals sign, and the new function name in Lua

Rename_classes renames the class, as Rename_classes = Simpleaudioengine::audioengine. The previous name is the first, and the new Lua is behind it. This doesn't seem to work.

Classes_have_no_parents Specifies some classes that do not have a parent class

Base_classes_to_skip Specify some base classes to skip

abstract_classes specifies that some abstract classes or classes without constructors are manually written to the LUA function after the

is written, so long as the modified custom or modified genbindings.py is executed (http:// www.cocoachina.com/bbs/read.php?tid=196416  mentioned) can generate the relevant code

3, automatically generated code will automatically filter out C + + Protect, private properties, Overloads a function of the parent class, a function with an ellipsis argument (such as Menu::create (menuitem* item, ...))

Seven. In our project, add the class you just created and the automatically generated class, and then

In AppDelegate.cpp:

(1) #include "LUA_MYCLASS_AUTO.HPP"//import our auto-generated files

(2) Find out where the scripting engine was initialized, as with other statements

register_all_** (L) is the first name we had before. This is Register_all_myclass (L)

Compile Build

OK.

In Lua. Examples of Use

Local TestValue = Mytestclass:gettestdata ()
Local testValue2 = Mytestclass2:gettestdata ()
Print (' =============== '). TestValue. " -------------".. TestValue2)

The problem may be incorrect configuration, incorrect class writing, and so on. Output that can be found

Cocos2dx-lua Development of C + + bound to Lua

Related Article

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.