控制结构 是管理 Luau 代码执行流程的声明。有四种主要类型的控制结构:
- 一个 如果然后 else 声明只会执行代码,如果指定的条件是 true 。代码执行不会重复。
- 一个 重复循环 执行验证码,如果条件是 true 就重复执行。
- 一个 循环 执行代码的次数取决于指定的输入,可达到一定数量。
if、while和repeat的条件可以是任何 Luau 表达式或值。如果值不是 false 或 nil ,那么 Luau 在条件语句中将其评为 true 。与其他脚本语言不同,Luau 将零和空白字符串视为 true。
如果声明
基本 if 语句测试其条件。如果条件为真,那么 Luau 将在 then 和 end 之间执行代码。
您可以使用 elseif 声明来测试额外条件,如果 if 条件为 false。您可以使用 else 语句来执行代码,如果所有 if 和 elseif 条件都失败。elseif 和 else 部分都可选,但你不能使用任何一个没有初始 if 声明。
在一个由 if、elseif、else 条件组成的链中,Luau 从上到下测试条件,在第一个 true 条件停止,然后执行随后的代码。
if 2 + 2 == 5 thenprint("Two plus two is five") -- 因为条件为假,所以不打印elseif 2 + 3 == 5 thenprint("Two plus three is five") -- 两加三是五elseprint("All conditions failed") -- 因为前一个条件是真实的,所以不打印end
while 循环
一个 while — do 循环评价是否满足指定的条件是真是假。如果条件是 false 或 nil ,则循环结束,Luau 跳过循环中的代码。如果条件是 true , 那么 Luau 在循环中执行代码并重复过程。
local timeRemaining = 10while timeRemaining > 0 doprint("Seconds remaining: " .. timeRemaining)task.wait(1)timeRemaining -= 1endprint("Timer reached zero!")--[[ 结果输出:Seconds remaining: 10Seconds remaining: 9Seconds remaining: 8Seconds remaining: 7Seconds remaining: 6Seconds remaining: 5Seconds remaining: 4Seconds remaining: 3Seconds remaining: 2Seconds remaining: 1Timer reached zero!]]
无限循环
您可以使用 while — do 循环来通过设置 true 为条件来写无限游戏循环。
while true doprint("Looping...")task.wait(0.5)end--[[ 结果输出:Looping...Looping...Looping...Looping......]]
重复循环
循环 repeat — until 直到一个条件成为真实为止。条件测试在代码块运行后评估 ,因此代码块至少运行一次 。与其他语言不同,本地变量内部的 — 循环中的 Luau 范围包括条件。
local currentGoblinCount = 18-- 在游戏中生成最多 25 个哥布林repeatspawnGoblin()currentGoblinCount += 1print("Current goblin count: " .. currentGoblinCount)until currentGoblinCount == 25print("Goblins repopulated!")--[[ 结果输出:Current goblin count: 19Current goblin count: 20Current goblin count: 21Current goblin count: 22Current goblin count: 23Current goblin count: 24Current goblin count: 25Goblins repopulated!]]
对于循环
循环 A 执行代码的次数为一组数字计数器或收集中的 项目数量 基于的一次,或者基于数字计数器或收集中的 项目数量 的数量。
循环的数字
一个 for — do 循环确定使用计数还价执行循环的次数。循环声明带有起始值、终止值和可选增量。
Luau将计数器设置为与起始值相等,在 for 循环中执行代码块,然后将增量添加到计数还价。如果增量是正的,那么过程将继续重复,直到计数器与最终值相等或大于为止。如果增量为负值,则过程会重复直到计数器与最终值相等或小于为止。
可选增量默认为 1 。它不需要是整数。
for counter = 1, 3 doprint(counter)end--[[ 结果输出:123]]for counter = 1, 6, 2 doprint(counter)end--[[ 结果输出:135]]for counter = 2, 0, -0.5 doprint(counter)end--[[ 结果输出:21.510.50]]
通用循环
通用 for 循环遍历集合中的项目,而不是一系列数字。使用通用 for 循环,您可以为收集中的每个项目执行代码,并且可以轻松使用代验证码中的每个项目。
循环需要一个函数或迭代器来遍历不同类型的集合。全球 ipairs() 返回用于阵列的迭代器,全球 pairs() 返回用于词典的迭代器。string 图形库提供 string.gmatch() 循环过字符串。
通用循环
在 Luau 中,您可以直接在表上使用 in 关键字循环表,而不是使用迭代函数,例如 ipairs() :
for i, v in {1, 2, 3, 4, 5} doprint(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() 函数返回一个循环器,该循环器在表中循环数字索引并返回一个 index 和 value 对每个元素。这使得适合于阵列,其中所有索引都是数字的。
local array = {"a", "b", "c", "d", "e"}for index, value in ipairs(array) doprint(index, value)end--[[ 结果输出:1 a2 b3 c4 d5 e]]
词典
pairs() 函数返回一个遍历所有索引(包括数字索引)的迭代器,并返回一个 key 和 value 对词典中每个条目。词典表中元素的遍历顺序是随意的。这使得它适合遍历字典,其中项目按非数字索引存储在不同顺序中。
local dictionary = {[1] = "a",["Hello"] = "b",[5] = "c",[true] = "d",["World"] = "f",[false] = "e"}for key, value in pairs(dictionary) doprint(key, value)end--[[ 结果输出:Hello btrue dfalse eWorld f5 c1 a]]
打破循环
要强制循环结束,请使用 break 关键字。以下代码示例显示了如何打破无限 while — do 循环。
local secondsElapsed = 0local timeout = 5while true dotask.wait(1)secondsElapsed += 1print("Seconds elapsed:", secondsElapsed)if secondsElapsed == timeout thenbreakendendprint("Five seconds elapsed. Time to move on!")--[[ 结果输出:12345Five seconds elapsed. Time to move on!]]
继续循环
要强制循环继续循环并重新启动,请使用 continue 关键字。一个 for 循环将遍历计数还价;while 和 repeat — until 将在继续前检查循环条件。以下代码示例获取特定 的所有子女。
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