有一个命令调用" tap(x,y)"我需要一种方法来组合行变量和列变量
代码:
--[[ Row Pos ]]--
r1 = 587.95
r2 = 383.05
--[[ Barracks Column ]]--
cb1 = 476.53
cb2 = 722.26
--[[ Barracks Variable ]]--
wizard = "r2" .. "," .. "cb1"
healer = "r2" .. "," .. "cb2"
tap(healer);
usleep(30000)
tap(wizard);
usleep(30000);
错误:
Bad Argument #2 to 'touchdown' (number expected, got string)
这意味着它想要数字,但我输入字符串是否有不同的方法可以做到这一点?
答案 0 :(得分:1)
您不需要将两个变量合并为一个字符串。只需使用列和行变量:
-- Row Pos
r1 = 587.95
r2 = 383.05
-- Barracks Column
cb1 = 476.53
cb2 = 722.26
-- wizard
tap(cb1, r2) -- did you mean r1? I just used your example
usleep(30000)
-- healer
tap(cb2, r2)
usleep(30000)
注意首先给出列(x)。
对您的代码的一些评论:没有必要结束单行注释,如我的示例中所示。此外,不需要分号,我省略了它。
如果你想使用一个收集两个参数的变量,你可以使用一个表并解压缩它:
local wizard = { cb1, r2 } -- { x, y }
tap(table.unpack(wizard))
您现在可以使用函数tap
的包装器,添加语法糖:
local old_tap = tap
function tap(location)
old_tap(table.unpack(location))
end
更进一步,检查参数并以正确的方式调用函数:
local old_tap = tap
function tap(x, y)
if type(x) == "table" then
old_tap(table.unpack(x))
else
old_tap(x, y)
end
end
-- now tap can be used both ways:
tap(cb1, r2) -- wizard
local healer = { cb2, r2 }
tap(healer)