LUA calls C + + function return value function

Source: Internet
Author: User
Tags lua

2015/05/28

LUA calls the C + + interface and returns to the LUA function with the contents of the pressed stack, which can have multiple return values. But the C + + interface itself also has a return value, and this return value is also very important, which determines the number of values that are returned to the LUA function at the end.

(1) C + + custom classes

int Test::getmsg (lua_state* L) {    +);      $ );     return 2 ;}

(2) C + + interface for tolua++ exported LUA calls (partially modified)

intLua_cocos2dx_custom_test_getmsg (lua_state*tolua_s) {    intARGC =0; Test* CObj =nullptr; BOOLOK =true; CObj= (placebypoint*) tolua_tousertype (tolua_s,1,0); {        intRET = cobj->getmsg (tolua_s);        Tolua_pushnumber (tolua_s, (lua_number) ret); return 1; }    return 0;}

(3) Use

local p = cc. Test:create ()local A, b, C = p:getmsg ()print(A, B, c)

The result is: 2, nil, nil

According to the general understanding, the result should be 100, 200, 2. So went to check the information, found themselves long time useless forget, C + + interface return value is also very important. "When you need to return values to Lua, the C function simply presses them on the stack with a positive order (the first return value is pressed first) and returns the number of these return values. Below these return values, the contents on the stack will be discarded by Lua. "[1]

Reference:

[1]

LUA calls C + + function return value function

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.