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. */