为了举例,我们假设love.getVersion()
定义如下:
function love.getVersion ()
return 1, 2, 3, "four"
end
Using select(index, ...)
:
If index
那么是数字select
返回参数索引之后的所有参数index
。考虑:
print("A:", select(3, love.getVersion()))
local revision = select(3, love.getVersion())
print("B:", revision)
outputs:
A: 3 four
B: 3
如有疑问 -参考手册 -select.
使用表包装器:
你提到过尝试love.getVersion()[0]
。那是almost它,但您首先需要将返回的值包装到实际的表中:
local all_of_them = {love.getVersion()}
print("C:", all_of_them[4])
outputs:
C: four
如果您想在一行中完成此操作(本着“不创建变量”的精神),您也需要将表格括在括号中:
print("D:", ({love.getVersion()})[1])
outputs:
D: 1
使用_
多变的:
来自其他语言,您可以只分配您不感兴趣的值_
(如果它是一条短平线,没有人会注意到我们创建了一个变量),如下所示:
local _, minor = love.getVersion()
print("E:", minor)
outputs:
E: 2
请注意,我跳过了以下任何内容_
在示例中(不需要local _, minor, _, _
).