如何将参数传递给 table 中的函数?
How to pass arguments to a function within a table?
首先,我坚持使用 Lua 5.0,因为我正在为 WoW API(2006 年构建的客户端)开发。为什么要折磨自己?因为我很高兴看到什么是可能的。
所以这是我的问题:
我有一个 table,其中包含一堆函数,例如,这个:
function Questie:UNIT_AURA(unitId)
--log("UnitID: "..unitId)
end
我有另一个函数,它应该自动路由到那个函数(在一定程度上确实如此)。
function Questie:OnEvent()
Questie[event](arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
end
在这种情况下,event 是一个全局变量,它等于函数的名称,例如UNIT_AURA。 arg1 到 arg10 也是全局的,应该传递给事件导致的任何函数。
该函数被调用得很好,但是所有参数都是零(即使它们存在于Questie:OnEvent
。
所以我的问题是,如何让它们正确通过?显然,我做错了什么。
这是你的错误(如我所见)
function Questie:UNIT_AURA(unitId)
--log("UnitID: "..unitId)
end
— 该声明等同于
function Questie.UNIT_AURA(self, unitId)
--log("UnitID: "..unitId)
end
你可以这样称呼它:
Questie.UNIT_AURA(Questie, unitId)
或者像这样
Questie['UNIT_AURA'](Questie, unitId)
或者像这样
Questie:UNIT_AURA(unitId)
区别在于:
或.
好的,那么
function Questie:OnEvent()
Questie[event](arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
end
— 该声明等同于
function Questie.OnEvent(self)
local callback = Questie[event]
callback(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
end
您丢失了 table 作为第一个参数,而是传递了 arg1。
因此,在 UNIT_AURA self
内将是 unitId
,而 unitId
将是 nil
预期参数 = self
、unitId
给定 args = unitId
(仅)
正确的叫法是这样的:
function Questie:OnEvent()
Questie[event](self, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
end
首先,我坚持使用 Lua 5.0,因为我正在为 WoW API(2006 年构建的客户端)开发。为什么要折磨自己?因为我很高兴看到什么是可能的。
所以这是我的问题: 我有一个 table,其中包含一堆函数,例如,这个:
function Questie:UNIT_AURA(unitId)
--log("UnitID: "..unitId)
end
我有另一个函数,它应该自动路由到那个函数(在一定程度上确实如此)。
function Questie:OnEvent()
Questie[event](arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
end
在这种情况下,event 是一个全局变量,它等于函数的名称,例如UNIT_AURA。 arg1 到 arg10 也是全局的,应该传递给事件导致的任何函数。
该函数被调用得很好,但是所有参数都是零(即使它们存在于Questie:OnEvent
。
所以我的问题是,如何让它们正确通过?显然,我做错了什么。
这是你的错误(如我所见)
function Questie:UNIT_AURA(unitId)
--log("UnitID: "..unitId)
end
— 该声明等同于
function Questie.UNIT_AURA(self, unitId)
--log("UnitID: "..unitId)
end
你可以这样称呼它:
Questie.UNIT_AURA(Questie, unitId)
或者像这样
Questie['UNIT_AURA'](Questie, unitId)
或者像这样
Questie:UNIT_AURA(unitId)
区别在于:
或.
好的,那么
function Questie:OnEvent()
Questie[event](arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
end
— 该声明等同于
function Questie.OnEvent(self)
local callback = Questie[event]
callback(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
end
您丢失了 table 作为第一个参数,而是传递了 arg1。
因此,在 UNIT_AURA self
内将是 unitId
,而 unitId
将是 nil
预期参数 = self
、unitId
给定 args = unitId
(仅)
正确的叫法是这样的:
function Questie:OnEvent()
Questie[event](self, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
end