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