From 6d7252047bfa1ebe107b2dddd82553df376be89d Mon Sep 17 00:00:00 2001 From: leelyn Date: Tue, 12 May 2020 02:44:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B0=83=E7=94=A8lua?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=87=BD=E6=95=B0=E5=B9=B6=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E4=BC=A0=E4=B8=80=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lua-bindings/manual/CCLuaStack.cpp | 34 +++++++++++++++++++ .../lua-bindings/manual/CCLuaStack.h | 7 ++++ 2 files changed, 41 insertions(+) diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp index 1bb54a3..81c47a5 100644 --- a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp @@ -297,6 +297,40 @@ int LuaStack::executeGlobalFunction(const char* functionName) return executeFunction(0); } +int LuaStack::executeGlobalFunctionWithString(const char* functionName, const char* data) +{ + lua_getglobal(_state, functionName); + + if (!lua_isfunction(_state, -1)) + { + CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", functionName); + lua_pop(_state, 1); + return 0; + } + + pushString(data); + int error = lua_pcall(_state, 1, 1, 0); + + if (error) + { + CCLOG("[LUA ERROR] %s", lua_tostring(_state, - 1)); + lua_pop(_state, 1); // clean error message + return 0; + } + + // get return value + if (!lua_isnumber(_state, -1)) + { + lua_pop(_state, 1); + return 0; + } + + int ret = lua_tointeger(_state, -1); + lua_pop(_state, 1); + return ret; +} + + void LuaStack::clean() { lua_settop(_state, 0); diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.h b/cocos/scripting/lua-bindings/manual/CCLuaStack.h index 8a2d032..d694fc9 100644 --- a/cocos/scripting/lua-bindings/manual/CCLuaStack.h +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.h @@ -144,6 +144,13 @@ public: */ virtual int executeGlobalFunction(const char* functionName); + /** + * 执行lua的全局函数,可以带一个字符串参数 + * @参数1 functionName,全局函数名 + * @参数2 data,utf8字符串 + * @返回 int值告诉被调用者结果 + */ + virtual int executeGlobalFunctionWithString(const char* functionName, const char* data); /** * Set the stack top index 0. */