可以通过字符串名称调用函数


function AIORotation:SetRotation(val)
if val == "abcd" then
self.db.profile.activeRotation = AIORotation.aaaa:new()
elseif val == "dddd" then
self.db.profile.activeRotation = AIORotation.dddd:new()
end
end

我将一个字符串传递给这个函数,并希望根据字符串名称调用一个函数。如果没有一堆if语句,这样的事情可能发生吗?

所以理想情况下,它应该是类似于的东西

function AIORotation:SetRotation(val)
self.db.profile.activeRotation = AIORotation.<INSERT_VAL_HERE>:new()
end

但我不确定这样的事情在夏威夷是否可能发生。

您需要使用[]语法。[]语法允许您使用任何变量、表达式(在运行时求值(或常量。

function AIORotation:SetRotation(val)
self.db.profile.activeRotation = AIORotation[val]:new()
end

.var语法实际上与["var"](语法糖(完全相同。

读取移动表语法:https://www.lua.org/pil/2.5.html

最新更新