Cocos2d-x Lua binding implements custom objects that access Lua in C + +

Source: Internet
Author: User
Tags goto lua

LUA binding is simpler than jsbinding, and it is easy to implement classes and objects that Access C + + in Lua, whether using scripts to automatically bind or manually write bound code. But if you want to access the custom classes and objects in Lua in C + +, you need to modify the C + + code yourself.

Application Scenarios:

1, assume that there is a class Mylayer in Lua, inherit the Cclayer, and add the A,b,c,d these 4 properties.

2. In Lua, when you create an instance object of Mylayer and add it to the current scene, and then use the Scene.getchildbytag method to take out the Mylayer instance object, you will see that the custom properties a,b,c,d are gone. The reason is that Toluafix_pushusertype_ccobject (tolua_s, NID, Pluaid, (void*) Tolua_ret, "Ccnode") is called, and a ccnode is returned, Here's the code in LuaCocos2d.cpp.

static int tolua_cocos2d_ccnode_getchildbytag00 (lua_state* tolua_s) {#ifndef tolua_release tolua_error tolua_err; C0/>!tolua_isusertype (tolua_s,1, "Ccnode", 0,&tolua_err) | |     ! Tolua_isnumber (tolua_s,2,0,&tolua_err) | |     ! Tolua_isnoobj (tolua_s,3,&tolua_err))  goto Tolua_lerror; else#endif {  ccnode* self = (ccnode*)  Tolua_ Tousertype (tolua_s,1,0);  int tag = ((int)  Tolua_tonumber (tolua_s,2,0)), #ifndef tolua_release  if (!self) tolua_error (tolua_s, "invalid" Self ' in function ' Getchildbytag ' ", NULL); #endif  {   ccnode* Tolua_ret = (ccnode*)  Self->getchildbytag (tag);    int NID = (tolua_ret)? (int) Tolua_ret->m_uid:-1;    int* Pluaid = (tolua_ret)? &tolua_ret->m_nLuaID:NULL;    Toluafix_pushusertype_ccobject (tolua_s, NID, Pluaid, (void*) Tolua_ret, "Ccnode");  } } return 1; #ifndef tolua_release tolua_lerror:tolua_error (tolua_s, "#ferror in function ' Getchildbytag '.", &tolua_ ERR); return 0; #endif}

3, the solution is very simple, in the light of the worthless,

A), first call Tolua_usertype (tolua_s, "Mylayer"), register the custom class, this code can be added to any call lua_state* this pointer, but must be called only once at initialization.

b), write the following code in the LuaCocos2d.cpp, looking at the position to put:

Expanded Getchildbytag to return LUA objects, by lizc/* Method:getchildbytag of Class Ccnode */#ifndef tolua_disable_tolua_cocos2d_    ccnode_getchildbytag01static int tolua_cocos2d_ccnode_getchildbytag01 (lua_state* tolua_s) {#ifndef TOLUA_RELEASE    Tolua_error Tolua_err;        if (!tolua_isusertype (tolua_s,1, "Ccnode", 0,&tolua_err) | |        !tolua_isnumber (tolua_s,2,0,&tolua_err) | |        (!tolua_isvaluenil (Tolua_s,3,&tolua_err) &&!tolua_isstring (tolua_s,3,0,&tolua_err)) | |    !tolua_isnoobj (Tolua_s,4,&tolua_err)) goto Tolua_lerror;        else#endif {ccnode* self = (ccnode*) tolua_tousertype (tolua_s,1,0);        int tag = ((int) tolua_tonumber (tolua_s,2,0)); Const char* Usertypename = ((const char*) tolua_tostring (tolua_s,3,0)); #ifndef tolua_release if (!self) Tolua_erro R (tolua_s, "Invalid ' self ' in function ' Getchildbytag '", NULL); #endif {ccnode* Tolua_ret = (ccnode*) se         Lf->getchildbytag (tag);   int NID = (tolua_ret)?            (int) Tolua_ret->m_uid:-1; int* Pluaid = (tolua_ret)?            &tolua_ret->m_nLuaID:NULL;        Toluafix_pushusertype_ccobject (tolua_s, NID, Pluaid, (void*) Tolua_ret, usertypename); }} return 1; #ifndef Tolua_releasetolua_lerror:return tolua_cocos2d_ccnode_getchildbytag00 (tolua_s); #endif} #endi F//#ifndef tolua_disable

c), and then add

Tolua_function (tolua_s, "Getchildbytag", tolua_cocos2d_ccnode_getchildbytag01), to Tolua_function (Tolua_S, " Getchildbytag ", tolua_cocos2d_ccnode_getchildbytag00); below, easy to manage.

4. How to use

--mylayer must inherit from a class from C + +, which is inherited cclayerlocal Mylayer = mylayer:create () mylayer.a = 1mylayer.b = 2local scene = Ccscene: Create () scene:addchild (mylayer, 0, 0) mylayer = Scene:getchildbytag (0, "Mylayer") Mylayer = Tolua.cast (Mylayer, "MyLayer ") Cclog (" Mylayer.a "). MYLAYER.A) Cclog ("mylayer.b": mylayer.b)



Cocos2d-x Lua binding implements custom objects that access Lua in C + +

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.