tolua 從lua傳值到c++
阿新 • • 發佈:2018-12-20
一般都是使用tolua將c++部分的函式和引數,在lua部分呼叫,現在有個需求,要從lua部分,獲取引數,傳遞到c++部分,在由c++傳遞到java部分
--首先還是要有一個常規的hpp --lua_cocos2dx_loginscenemanager.hpp
#include "base/ccConfig.h" #ifndef __cocos2dx_loginscenemanager_h__ #define __cocos2dx_loginscenemanager_h__ #ifdef __cplusplus extern "C" { #endif #include "tolua++.h" #ifdef __cplusplus } #endif int register_all_cocos2dx_loginscenemanager(lua_State *tolua_S); #endif // __cocos2dx_loginscenemanager_h__
-- 然後還是一個常規的cpp --lua_cocos2dx_loginscenemanager.cpp
#include "QuickSDK/lua_cocos2dx_loginscenemanager.hpp" #include "LoginScene.h" #include "scripting/lua-bindings/manual/CCLuaValue.h" #include "scripting/lua-bindings/manual/LuaBasicConversions.h" #include "scripting/lua-bindings/manual/tolua_fix.h" --這個的用法是從c++獲取引數,傳遞到lua int lua_cocos2dx_loginscenemanager_LoginScene_getChannelType(lua_State* tolua_S) { int argc = 0; bool ok = true; //引數判斷 argc = lua_gettop(tolua_S) - 1; if (argc == 0) { int ret = LoginScene::GetInstance()->getChannelType(); tolua_pushnumber(tolua_S, (lua_Number)ret); return 1; } luaL_error(tolua_S, "%s, %d, was expecting %d \n", "LoginScene::GetInstance()->getChannelType()", argc, 0); return 0; } --這個的用法就是從lua獲取引數傳遞到c++ int lua_cocos2dx_loginscenemanager_LoginScene_setGameInfo(lua_State *tolua_S) { int argc = 0; LoginScene *cobj = nullptr; bool ok = true; #if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; #endif #if COCOS2D_DEBUG >= 1 if (!tolua_isusertype(tolua_S, 1, "LoginScene", 0, &tolua_err)) goto tolua_lerror; #endif cobj = (LoginScene *)tolua_tousertype(tolua_S, 1, 0); #if COCOS2D_DEBUG >= 1 if (!cobj) { tolua_error(tolua_S, "invalid 'cobj' in function " "'lua_cocos2dx_loginscenemanager_LoginScene_setGameInfo'", nullptr); return 0; } #endif argc = lua_gettop(tolua_S) - 1; if (argc == 9) { const char *arg0; const char *arg1; const char *arg2; const char *arg3; const char *arg4; const char *arg5; const char *arg6; const char *arg7; int arg8; std::string arg0_tmp; ok &= luaval_to_std_string(tolua_S, 2, &arg0_tmp,"LoginScene:setGameInfo"); arg0 = arg0_tmp.c_str(); std::string arg1_tmp; ok &= luaval_to_std_string(tolua_S, 3, &arg1_tmp, "LoginScene:setGameInfo"); arg1 = arg1_tmp.c_str(); std::string arg2_tmp; ok &= luaval_to_std_string(tolua_S, 4, &arg2_tmp, "LoginScene:setGameInfo"); arg2 = arg2_tmp.c_str(); std::string arg3_tmp; ok &= luaval_to_std_string(tolua_S, 5, &arg3_tmp, "LoginScene:setGameInfo"); arg3 = arg3_tmp.c_str(); std::string arg4_tmp; ok &= luaval_to_std_string(tolua_S, 6, &arg4_tmp, "LoginScene:setGameInfo"); arg4 = arg4_tmp.c_str(); std::string arg5_tmp; ok &= luaval_to_std_string(tolua_S, 7, &arg5_tmp, "LoginScene:setGameInfo"); arg5 = arg5_tmp.c_str(); std::string arg6_tmp; ok &= luaval_to_std_string(tolua_S, 8, &arg6_tmp, "LoginScene:setGameInfo"); arg6 = arg6_tmp.c_str(); std::string arg7_tmp; ok &= luaval_to_std_string(tolua_S, 9, &arg7_tmp, "LoginScene:setGameInfo"); arg7 = arg7_tmp.c_str(); ok &= luaval_to_int32(tolua_S, 10, (int *)&arg8, "LoginScene:setGameInfo"); if (!ok) { tolua_error(tolua_S, "invalid arguments in function " "'lua_cocos2dx_loginscenemanager_LoginScene_setGameInfo'", nullptr); return 0; } CCLOG("%s cobj->setGameInfo==", arg3); bool ret = cobj->setGameInfo(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); tolua_pushboolean(tolua_S, (bool)ret); return 1; } luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "LoginScene:setGameInfo", argc, 9); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error( tolua_S, "#ferror in function 'lua_cocos2dx_loginscenemanager_LoginScene_setGameInfo'.", &tolua_err); #endif return 0; } int lua_cocos2dx_loginscenemanager_LoginScene_GetInstance(lua_State* tolua_S) { int argc = 0; bool ok = true; //引數判斷 argc = lua_gettop(tolua_S) - 1; if (argc == 0) { LoginScene *ret = LoginScene::GetInstance(); object_to_luaval<LoginScene>(tolua_S, "LoginScene", (LoginScene*)ret); return 1; } luaL_error(tolua_S, "%s, %d, was expecting %d \n", "LoginScene::GetInstance()", argc, 0); return 0; } static int lua_cocos2dx_loginscenemanager_LoginSceneManager_finalize(lua_State *tolua_S) { printf("luabindings: finalizing LUA object (LoginScene)"); return 0; } int lua_register_LoginSceneManager(lua_State *tolua_S) { tolua_usertype(tolua_S, "LoginScene"); tolua_cclass(tolua_S, "LoginScene", "LoginScene", "", nullptr); tolua_beginmodule(tolua_S, "LoginScene"); tolua_function(tolua_S, "getChannelType", lua_cocos2dx_loginscenemanager_LoginScene_getChannelType); tolua_function(tolua_S, "setGameInfo", lua_cocos2dx_loginscenemanager_LoginScene_setGameInfo); tolua_function(tolua_S, "GetInstance", lua_cocos2dx_loginscenemanager_LoginScene_GetInstance); tolua_endmodule(tolua_S); std::string typeName = typeid(LoginScene).name(); g_luaType[typeName] = "LoginScene"; g_typeCast["LoginScene"] = "LoginScene"; return 1; } TOLUA_API int register_all_cocos2dx_loginscenemanager(lua_State *tolua_S) { tolua_open(tolua_S); tolua_module(tolua_S, nullptr, 0); tolua_beginmodule(tolua_S, nullptr); lua_register_LoginSceneManager(tolua_S); tolua_endmodule(tolua_S); return 1; }
至於這裡出現的LoginScene,只是一個邏輯類而已,就不貼了