Cocos custom class bound to lua

Source: Internet
Author: User

Cocos custom class bound to lua

1: follow the instructions of README. mdown under the rameworkscocos2d-x ools olua to do it step by step. It is necessary that, as described in the document, I have been so miserable by this pitfall. (Check whether there is any configured method: Run genbindings. py to see if the files in the liblua auto directory can be generated. If yes, your environment configuration will be successful .)

2: Write a custom C ++ class

Example:

 

#pragma once#include cocos2d.hUSING_NS_CC;class MyClass : public Ref{public:MyClass(void);~MyClass(void);bool init() { return true; };CREATE_FUNC(MyClass);int foo(int i);};

#include MyClass.hMyClass::MyClass(void){}MyClass::~MyClass(void){}int MyClass::foo(int i){return i + 100;}

3: A lot of. ini files and a genbindings. python file can be seen in the rameworkscocos2d-x ools olua directory.

 

Copy A. ini file. I will rename it: myclass. ini. Copy a python file and rename it mygenbindings. python.

Modify two configuration files:

Modifications to the python file:

 

 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'),                     'cocos2dx_experimental_video.ini' : ('cocos2dx_experimental_video', 'lua_cocos2dx_experimental_video_auto'),                     'cocos2dx_experimental.ini' : ('cocos2dx_experimental', 'lua_cocos2dx_experimental_auto'),                     'cocos2dx_controller.ini' : ('cocos2dx_controller', 'lua_cocos2dx_controller_auto'),                     }

To:

 

 

cmd_args = {'myclass.ini' : ('myclass', 'lua_myclass_auto'),                     }

Modify the myclass. ini file:

 

[Myclass] (very important, it should be the same as the file name. This is the first line.

Prefix = myclass

Target_namespace = (My namespace does not exist)

Headers = % (cocosdir) s/../runtime-src/Classes/MyClass. h (this is my class path, which is associated with AppDelegate. configure it according to your own path)

Classes = MyClass

The following skip =, rename_functions =, and so on are all empty.

In this way, the lua and cocos intermediate files can be generated. Like lua_cocos2dx_auto.cpp and hpp. Because the path of the intermediate file is not changed in the python file, it should be in the same directory as lua_cocosdx_auto.

Register the custom class and add the code to the AppDelegate. cpp file:

 

Auto engine = LuaEngine: getInstance (); register_all_myclass (engine-> getLuaStack ()-> getLuaState (); // Add this line of code here
In this way, we have registered our custom classes.

 

Compile the project and report an error:

 


Int _ cdecl register_all_myclass (struct lua_State *)(? Register_all_myclass @ YAHPAUlua_State @ Z). This symbol is used in the public: virtual bool _ thiscall AppDelegate: applicationDidFinishLaunching (void )(? ApplicationDidFinishLaunching @ AppDelegate @ UAE_NXZ) is referenced

 

Solution: add the two files just generated to the liblua project and regenerate the liblua project. It's done !! 

 

4: Call our class in lua code.

Add the following to main () of hello2.lua:

 

local test = MyClass:create()print(the first cocos bind lua result ..test:foo(2))



 

The answer is output. In this way, the cocos custom class is bound to lua.

 

 

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.