添加调用lua全局函数并可以传一个字符串参数

This commit is contained in:
leelyn
2020-05-12 02:44:04 +08:00
parent 41297ecdc3
commit 6d7252047b
2 changed files with 41 additions and 0 deletions

View File

@@ -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);