一般都是使用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(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,只是一个逻辑类而已,就不贴了