Is there any way to get several values from a function without creating variables for it?
local major, minor, revision, codename = love.getVersion() -- Get current LÖVE version as a string.
Run Code Online (Sandbox Code Playgroud)
So instead of making four different variables (or array) we'll use something that will just return a value you need.
get( love.getVersion(), 0 ) -- Will return the first value (major).
Run Code Online (Sandbox Code Playgroud)
I read somewhere that I can use square brackets and triedlove.getVersion()[1] but it says "Attempt to index a number value."
为了举例,我们假设love.getVersion()定义如下:
function love.getVersion ()
return 1, 2, 3, "four"
end
Run Code Online (Sandbox Code Playgroud)
select(index, ...):如果index是数字,则select返回参数索引 of 之后的所有参数index。考虑:
print("A:", select(3, love.getVersion()))
local revision = select(3, love.getVersion())
print("B:", revision)
Run Code Online (Sandbox Code Playgroud)
输出:
A: 3 four
B: 3
Run Code Online (Sandbox Code Playgroud)
如有疑问 -参考手册 -select .
你提到过尝试love.getVersion()[0]。这几乎是它,但你首先需要包装返回到实际的表中的值:
A: 3 four
B: 3
Run Code Online (Sandbox Code Playgroud)
输出:
C: four
Run Code Online (Sandbox Code Playgroud)
如果您想在一行中完成(本着“不创建变量”的精神),您还需要将表格括在括号中:
local all_of_them = {love.getVersion()}
print("C:", all_of_them[4])
Run Code Online (Sandbox Code Playgroud)
输出:
D: 1
Run Code Online (Sandbox Code Playgroud)
_变量:来自其他语言,您可以只分配您不感兴趣的值_(如果它是一条短平线,没有人会注意到我们创建了一个变量),如下所示:
C: four
Run Code Online (Sandbox Code Playgroud)
输出:
E: 2
Run Code Online (Sandbox Code Playgroud)
请注意,我跳过_了示例中的任何以下内容(不需要local _, minor, _, _)。