Lua C++ 交互

http://blog.csdn.net/u012926027/article/details/50402899

appdelegate.cpp 中要调用 register_game_moudle 函数

lua 调用 C++ 函数

int register_game_moudle(lua_State* L)

{

lua_getglobal(L, "_G");

if (lua_istable(L,-1))//stack:...,_G,

{

CCLOG("register_game_moudle");

tolua_open(L);

tolua_module(L,"luagame",0);

tolua_beginmodule(L,"luagame");

tolua_function(L,"intoGame", lua_game_Text_intoGame);

tolua_function(L,"onReceive", lua_game_Text_onReceive);

tolua_function(L,"setRoomData", lua_game_Text_setRoomData);

tolua_function(L,"closeGame", lua_game_Text_closeGame);

tolua_function(L,"freshTable", lua_game_Text_freshTable);

tolua_function(L,"openWap", lua_game_Text_openWap);

tolua_function(L,"showTopPrompt", lua_game_Text_showTopPrompt);

tolua_endmodule(L);

}

lua_pop(L, 1);

return 1;

}

//  Lua函数 luagame.intoGame()  调用   C++函数 lua_game_Text_intoGame



C++ 调用Lua

int sendGameMsg(const char* buf, size_t len)

{

auto engine = LuaEngine::getInstance();

LuaStack* stack = engine->getLuaStack();

lua_State* L = stack->getLuaState();

lua_getglobal(L, "gbLuaCFunc");

lua_pushstring(L, "sendGameMsg");

lua_gettable(L, -2);

lua_pushlstring(L, buf, len);

lua_call(L, 1, 0);

lua_pop(L, 1);

return 1;

}

-- 即C++函数 sendGameMsg() 调用Lua函数:  gbLuaCFunc.sendGameMsg() 

int gameShare(int mode, const char* msg)

{

auto engine = LuaEngine::getInstance();

LuaStack* stack = engine->getLuaStack();

lua_State* L = stack->getLuaState();

lua_getglobal(L, "gbLuaCFunc");

lua_pushstring(L, "gameShare");

lua_gettable(L, -2);

lua_pushnumber(L,mode);

lua_pushstring(L, msg);

lua_call(L, 2, 0);

lua_pop(L, 1);

return 1;

}

你可能感兴趣的:(Lua C++ 交互)