2
我不是Lua的專家,但我已經紅了一些文章來理解它是如何工作的。不過,我調用屬於C++表的lua函數有問題。Lua - C++集成:從C++中調用函數表
關於下面描述的例子我試圖從代碼調用foo:bar。呼叫成功。然而,參數「a」是零(返回值是正確的 - 當我將返回值更改爲例如10時,則顯示正確的結果)
在推送函數參數給腳本期間,我錯過了什麼嗎?
lua_State* state = LuaIntegration->GetLuaState();
lua_getglobal(state, "foo");
if(lua_istable(state, lua_gettop(state))) {
lua_getfield(state, -1, "bar");
if(lua_isfunction(state, lua_gettop(state))) {
lua_pushinteger(state, 0);
if (lua_pcall(state, 1, 1, 0) != 0) {
ErrorMessage = lua_tostring(state, -1);
}
ReturnValue = lua_tointeger(state, -1);
}
}
它調用函數在Lua:
foo = base_foo:new()
function foo:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function foo:bar(a)
if a==10 then
return a
end
return 0
end
這是有道理的。謝謝 –