我可以使用 table.concat 作为一组参数吗?

Am I able to use table.concat as a set of arguments?

我正在使用 LOVE2D 来适应 lua 一点,我正在尝试调用一个函数使圆圈出现在屏幕上,有 5 个参数,我有一个 table 称为 'button',其中包含所需的参数。我想使用 table.concat 来填充所有空白参数,但它不允许我这样做。有办法吗?

function toRGB(r,g,b)
    return r/255,g/255,b/255
end

function love.load()
    button = {}
    button.mode = "fill"
    button.x = 0
    button.y = 0
    button.size = 30
end

function love.draw()
    love.graphics.setColor(toRGB(60,60,60))
    love.graphics.circle(table.concat(button))
end

table.concat returns 一个字符串。这不是你想要的。

要获得 table 个元素的列表,请使用 table.unpack。但是此函数仅适用于具有从 1 开始的连续数字索引的 tables。

另外 love.graphics.circle 按位置访问其参数,而不是按名称。因此,您必须确保放入该函数的表达式列表的顺序正确。

所以像这样:

button = {"fill", 0, 0, 30}
love.graphics.circle(table.unpack(button))

会起作用。

如果您像示例中那样使用其他 table 键,则必须编写一个函数,使 returns 值按正确的顺序排列。

最简单的情况

button = {}
button.mode = "fill"
button.x = 0
button.y = 0
button.size = 30
button.unpack = function() return button.mode, button.x, button.y, button.size end
love.graphics.circle(button.unpack())

或者你可以这样做:

function drawCircle(params)
  love.graphics.circle(params.mode, params.x, params.y, params.size)
end
drawCircle(button)

还有许多其他方法可以实现这一点。