表 数据类型可以存储任何类型的多个值,不包括nil、布林数、数字、字符串、函数和其他表。使用卷曲括号建造表({}):
-- 构建一个空表,分配给变量“t”local t = {}print(t) -- {}
您可以将表用作阵列或词典。阵列使用排序列表的数字作为索引,但词典可以有数字、字符串和对象作为索引。
有关使用表的内置函数的更多信息,请参阅 table 图书馆。
阵列
一个 阵列 是一个排序列的值。阵列有用于存储数据集,例如一个拥有特殊权限的玩家群。
创建阵列
使用 Luau 表创建阵列时,宣言值在顺序排列中,用逗号分开。
-- 构建一个包含三个项目的阵列local testArray = {"A string", 3.14159, true}print(testArray)
从阵列中读取
要从阵列中读取,请在其参考后添加一对方括号,并指定内部元素的索引号([pos]):
-- 构建一个包含三个项目的阵列local testArray = {"A string", 3.14159, true}print(testArray[1]) -- 一个字符串print(testArray[2]) -- 3.14159print(testArray[3]) -- true
写入阵列
要在索引定义或重写阵列的值,请在括号中宣布索引号 ( [index] ) 后跟随 = 和值:
local testArray = {"A string", 3.14159, true}testArray[2] = 12345testArray[4] = "New string"print(testArray[2]) --12345print(testArray[4]) -- New string
遍历阵列
要循环过阵列,您可以使用 for 循环。因为阵列有数字索引,你还可以使用从 1 到阵列长度的数字循环 for (#array )。
local testArray = {"A string", 3.14159, true, "New string"}-- 使用通用循环进行循环for index, value in testArray doprint(index, value)end-- 使用阵列长度运营符进行循环(#)for index = 1, #testArray doprint(index, testArray[index])end
插入项目
有两种内置方法可将项目插入到阵列的 末端 :
- 将参考传递给 array 和 item 值传递给 Luau's table.insert() 函数。
- 使用 array[#array+1] 语法将新项目添加到阵列。
local testArray = {"A string", 3.14159}table.insert(testArray, "New string")testArray[#testArray+1] = "Another new string"print(testArray[3]) -- 新字符串print(testArray[4]) -- Another new string
要在阵列的开始和结束之间插入一个项目,请将位置值作为 table.insert() 的第二个参数包含在内。这将插入新项目,并将以下项目推到一个索引位置。
local testArray = {"First item", "Next item"}table.insert(testArray, 2, "NEW ITEM #2")print(testArray[1]) -- 第一个项目print(testArray[2]) -- 新物品 #2print(testArray[3]) -- Next item
移除项目
要从阵列中删除一个项目,请使用 table.remove() 。这将移除指定位置的项目,并将任何后续项目移回一个索引位置。
local testArray = {"First item", "Next item", "Last item"}table.remove(testArray, 2)print(testArray[1]) -- 第一个项目print(testArray[2]) -- Last item
词典
词典是阵列的扩展。词典存储一组键值对,其中键可以是任何数字、字符串或对象。
创建词典
要创建一个词典表,定义每个 键 后面的 = 和 值 。用逗号分开每个键-值对:
local testDictionary = {fruitName = "Lemon",fruitColor = "Yellow",sour = true}
词典的钥匙可以是数字、字符串和对象。例如,钥匙也可能是 Instance 。要使用对象作为钥匙,请在括号中声明键 ( [key] ):
local part = Instance.new("Part")local testDictionary = {partType = "Block",[part] = true}
从词典中读取
要从词典中阅读,请在其参考后添加一对括号,并指定键名。直接使用 ["key"] 或 .key 来引用字符串键,或者使用变量值 [key] 。
local part = Instance.new("Part")local testDictionary = {partType = "Block",[part] = true}-- 包括对字符键的引用语句print(testDictionary["partType"]) -- 块-- 或使用 . 来索引没有空格的字符串键print(testDictionary.partType) -- 块-- 忽略非字符键的引用符print(testDictionary[part]) -- true
写入词典
要定义或重写新或现有词典键的值,请在括号([key])中宣布键名称,或如果键是字符串,请使用(.key),然后使用(=)和值:
local testDictionary = {fruitName = "Lemon",sour = true}-- 更改现有键的值testDictionary["fruitName"] = "Cherry"testDictionary.sour = false-- 插入新的键值对testDictionary.fruitCount = 10print(testDictionary.fruitName) -- 樱桃print(testDictionary.sour) -- 错误print(testDictionary.fruitCount) -- 10
在词典上迭代
要循环过字典,使用 for 循环:
local testDictionary = {fruitName = "Lemon",fruitColor = "Yellow",sour = true}for key, value in testDictionary doprint(key, value)end--[[ 结果输出:fruitName Lemonsour truefruitColor Yellow]]
移除键值对
要从词典中删除或清除键值对,将其值设置为键为 nil。
local testDictionary = {fruitName = "Lemon",fruitColor = "Yellow",sour = true}testDictionary.sour = nilfor key, value in testDictionary doprint(key, value)end--[[ 结果输出:fruitName LemonfruitColor Yellow]]
作为参考的表
如果您将表存储在新变量中,Luau不会创建该表的副本。相反,变量成为 引用 或指针,指向原始表。对表的任何引用都反映了原表的任何更改:
local originalArray = {10, 20}local arrayReference = originalArrayprint("Original:", originalArray[1], originalArray[2])print("Reference:", arrayReference[1], arrayReference[2])-- 更改原始阵列中的值originalArray[1] = 1000originalArray[2] = 2000print("Reference:", arrayReference[1], arrayReference[2])--[[ 结果输出:Original: 10 20Reference: 10 20Reference: 1000 2000]]
克隆表
浅层克隆
要复制无任何嵌套表的表,Luau 提供了 table.clone() 方法。
local original = {key = "value",engine = "Roblox",playerID = 505306092}local clone = table.clone(original)
深度克隆
要复制包含嵌套表的更复杂的表,你需要使用类似于以下递归函数的函数:
-- 用于深度克隆表的函数
local function deepClone(original)
-- 为复制定义新表
local clone = table.clone(original)
-- 循环通过原始表来检查表值
-- 如果表被找到为值,深度复制到键(索引)
for key, value in original do
if type(value) == "table" then
clone[key] = deepClone(value)
end
end
-- 返回深度克隆表的最终副本
return clone
end
有了函数在位,你可以按照以下步骤进行深度复制:
local original = {key = "value",playerInfo = {playerID = 505306092,playerName = "PlayerName"},otherInfo = {{{1, 3, 5, 7, 9}}}}local clone = deepClone(original)
冻结表格
冻结一个表使其成为只读表,这对于创建不需要更改的常量值很有用。冻结是永久的;没有“解冻”或“解冻”方法。要检查表是否被冻结,请使用 table.isfrozen() 。
浅冻结
要冻结无任何嵌套表的表,Luau 提供了 table.freeze() 方法。
local target = {key = "value",engine = "Roblox",playerID = 505306092}table.freeze(target)target.playerID = 1 --> attempt to modify a readonly table
深冻结
要冻结包含嵌套表的更复杂的表,请使用类似于以下递归函数:
local function deepFreeze(target)
-- 浅冻结表
table.freeze(target)
-- 检查表的每个键并将其冻结,如果它是表
for _, value in target do
-- 确保值未冻结;如果已冻结,将发生错误
if type(value) == "table" and table.isfrozen(value) == false then
deepFreeze(v)
end
end
end
有了函数在位,你可以深冻一个表如下:
local target = {key = "value",playerInfo = {playerID = 505306092,playerName = "PlayerName"},otherInfo = {{{1, 3, 5, 7, 9}}}}deepFreeze(target)target.playerInfo.playerID = 1 --> attempt to modify a readonly table