控制结构

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

控制结构 是管理 Luau 代码执行流程的声明。有四种主要类型的控制结构:

  • 一个 如果然后 else 声明只会执行代码,如果指定的条件是 true 。代码执行不会重复。
  • 一个 而循环 只会执行代码,如果指定的条件是 true ,并且条件仍然是 true 时重复执行。
  • 一个 重复循环 执行验证码,如果条件是 true 就重复执行。
  • 一个 循环 执行代码的次数取决于指定的输入,可达到一定数量。

ifwhilerepeat的条件可以是任何 Luau 表达式或值。如果值不是 falsenil ,那么 Luau 在条件语句中将其评为 true 。与其他脚本语言不同,Luau 将零和空白字符串视为 true

如果声明

基本 if 语句测试其条件。如果条件为真,那么 Luau 将在 thenend 之间执行代码。

您可以使用 elseif 声明来测试额外条件,如果 if 条件为 false。您可以使用 else 语句来执行代码,如果所有 ifelseif 条件都失败。elseifelse 部分都可选,但你不能使用任何一个没有初始 if 声明。

在一个由 ifelseifelse 条件组成的链中,Luau 从上到下测试条件,在第一个 true 条件停止,然后执行随后的代码。


if 2 + 2 == 5 then
print("Two plus two is five") -- 因为条件为假,所以不打印
elseif 2 + 3 == 5 then
print("Two plus three is five") -- 两加三是五
else
print("All conditions failed") -- 因为前一个条件是真实的,所以不打印
end

while 循环

一个 whiledo 循环评价是否满足指定的条件是真是假。如果条件是 falsenil ,则循环结束,Luau 跳过循环中的代码。如果条件是 true , 那么 Luau 在循环中执行代码并重复过程。


local timeRemaining = 10
while timeRemaining > 0 do
print("Seconds remaining: " .. timeRemaining)
task.wait(1)
timeRemaining -= 1
end
print("Timer reached zero!")
--[[ 结果输出:
Seconds remaining: 10
Seconds remaining: 9
Seconds remaining: 8
Seconds remaining: 7
Seconds remaining: 6
Seconds remaining: 5
Seconds remaining: 4
Seconds remaining: 3
Seconds remaining: 2
Seconds remaining: 1
Timer reached zero!
]]

无限循环

您可以使用 whiledo 循环来通过设置 true 为条件来写无限游戏循环。


while true do
print("Looping...")
task.wait(0.5)
end
--[[ 结果输出:
Looping...
Looping...
Looping...
Looping...
...
]]

重复循环

循环 repeatuntil 直到一个条件成为真实为止。条件测试在代码块运行后评估 ,因此代码块至少运行一次 。与其他语言不同,本地变量内部的 — 循环中的 Luau 范围包括条件。


local currentGoblinCount = 18
-- 在游戏中生成最多 25 个哥布林
repeat
spawnGoblin()
currentGoblinCount += 1
print("Current goblin count: " .. currentGoblinCount)
until currentGoblinCount == 25
print("Goblins repopulated!")
--[[ 结果输出:
Current goblin count: 19
Current goblin count: 20
Current goblin count: 21
Current goblin count: 22
Current goblin count: 23
Current goblin count: 24
Current goblin count: 25
Goblins repopulated!
]]

对于循环

循环 A 执行代码的次数为一组数字计数器或收集中的 项目数量 基于的一次,或者基于数字计数器或收集中的 项目数量 的数量。

循环的数字

一个 fordo 循环确定使用计数还价执行循环的次数。循环声明带有起始值、终止值和可选增量。

Luau将计数器设置为与起始值相等,在 for 循环中执行代码块,然后将增量添加到计数还价。如果增量是正的,那么过程将继续重复,直到计数器与最终值相等或大于为止。如果增量为负值,则过程会重复直到计数器与最终值相等或小于为止。

可选增量默认为 1 。它不需要是整数。


for counter = 1, 3 do
print(counter)
end
--[[ 结果输出:
1
2
3
]]
for counter = 1, 6, 2 do
print(counter)
end
--[[ 结果输出:
1
3
5
]]
for counter = 2, 0, -0.5 do
print(counter)
end
--[[ 结果输出:
2
1.5
1
0.5
0
]]

通用循环

通用 for 循环遍历集合中的项目,而不是一系列数字。使用通用 for 循环,您可以为收集中的每个项目执行代码,并且可以轻松使用代验证码中的每个项目。

循环需要一个函数或迭代器来遍历不同类型的集合。全球 ipairs() 返回用于阵列的迭代器,全球 pairs() 返回用于词典的迭代器。string 图形库提供 string.gmatch() 循环过字符串。

通用循环

在 Luau 中,您可以直接在表上使用 in 关键字循环表,而不是使用迭代函数,例如 ipairs() :


for i, v in {1, 2, 3, 4, 5} do
print(i, v)
end

通用循环还允许您使用 __iter 虚拟方法来创建自定义迭代器函数。这个示例使用反向顺序遍历阵列,从最后一个元素到最前一个:


local myTable = {1, 2, 3, 4, 5}
myMetatable = {
__iter = function(t)
local i = #t + 1
return function()
i -= 1
if i > 0 then
return i, t[i]
end
end
end,
}
setmetatable(myTable, myMetatable)
for i, v in myTable do
print(i, v)
end
--[[ 结果输出:
5 5
4 4
3 3
2 2
1 1
]]

阵列

ipairs() 函数返回一个循环器,该循环器在表中循环数字索引并返回一个 indexvalue 对每个元素。这使得适合于阵列,其中所有索引都是数字的。


local array = {"a", "b", "c", "d", "e"}
for index, value in ipairs(array) do
print(index, value)
end
--[[ 结果输出:
1 a
2 b
3 c
4 d
5 e
]]

词典

pairs() 函数返回一个遍历所有索引(包括数字索引)的迭代器,并返回一个 keyvalue 对词典中每个条目。词典表中元素的遍历顺序是随意的。这使得它适合遍历字典,其中项目按非数字索引存储在不同顺序中。


local dictionary = {
[1] = "a",
["Hello"] = "b",
[5] = "c",
[true] = "d",
["World"] = "f",
[false] = "e"
}
for key, value in pairs(dictionary) do
print(key, value)
end
--[[ 结果输出:
Hello b
true d
false e
World f
5 c
1 a
]]

打破循环

要强制循环结束,请使用 break 关键字。以下代码示例显示了如何打破无限 whiledo 循环。


local secondsElapsed = 0
local timeout = 5
while true do
task.wait(1)
secondsElapsed += 1
print("Seconds elapsed:", secondsElapsed)
if secondsElapsed == timeout then
break
end
end
print("Five seconds elapsed. Time to move on!")
--[[ 结果输出:
1
2
3
4
5
Five seconds elapsed. Time to move on!
]]

继续循环

要强制循环继续循环并重新启动,请使用 continue 关键字。一个 for 循环将遍历计数还价;whilerepeatuntil 将在继续前检查循环条件。以下代码示例获取特定 的所有子女。


local function GetChildrenOfClass(parent: Instance, className: string): {Instance}
local children = {}
for _, child in parent:GetChildren() do
if child.ClassName ~= className then continue end -- 循环遍历
table.insert(children, child)
end
return children
end