数据结构 是程序员用于存储和组织整套数据的方式。 在 Luau 中,数据结构是通过表格创建的。 表格 可以容纳任意数量的值。
本文介绍使用 数组,这种特定的表格类型,来创建一个会说话的角色。

表格
表格 是可以存储多个值的数据类型。 与只存储单个值的其他数据类型不同,表格没有固定的大小,可以存放不同类型的值。 使用表格,你可以存储玩家的物品清单或创建数千个玩家名称的列表。
数组
有不同类型的表格。 一种类型是 数组,它以特定顺序存储值列表。 要创建数组,只需创建一个变量并赋值为花括号 { }。 使用逗号在括号内分隔值,示例如下:
local myArray = {"item1", "item2", 10, workspace.Part, myVariable}
创建一个会说话的角色
为了探索数组,你将与一个不可播放角色(NPC)合作,当点击它时,会显示不同的对话。
该项目将使用一个预制的 NPC 模型,其中包含部分脚本和提示探测器,但缺乏对话。
在资源管理器中,通过右键单击 工作区 ⟩ 从文件插入 导入 NPC,选择下载的文件。
编码对话数组
这些步骤使用一个数组来存储 NPC 在玩家与其互动时所说的不同短语。
在 资源管理器 中,转到 NPC ⟩ ProximityPrompt ⟩ ChatManager。
在 ChatManager 中,在脚本所标记的位置,创建一个 空数组 以存储对话选项。
-- 使用提示时循环聊天对话local Chat = game:GetService("Chat")local prompt = script.Parentlocal npc = prompt.Parentlocal characterParts = npc.CharacterPartslocal head = characterParts.Head-- 在这里添加数组local dialogueArray = {}local function speak()local dialogue = "我有一句话要说!"Chat:Chat(head, dialogue)endprompt.Triggered:Connect(speak)在刚创建的数组的括号 {} 内,输入至少三个字符串对话,用逗号分隔。
local dialogueArray = {"嗨!", "我认识你吗?", "再见!"}
使用数组索引
数组中的每个值都分配有一个 索引 号码。 索引是根据值存储的顺序分配给值的。 第一个值的索引是 1,第二个值的索引是 2,以此类推。
某些编程语言(如 Java)从 0 开始索引。
在刚创建的数组中,"嗨!" 的索引为 1,"再见!" 的索引为 3。
| 索引 | 值 |
|---|---|
| 1 | 嗨 |
| 2 | 今天是个好日子! |
| 3 | 再见! |
使用特定的索引值
使用索引值为 NPC 分配特定的对话内容。 要使用特定索引的值,在数组名称后面直接添加索引,用方括号包裹,例如 dialogueArray[1]。
将 dialogue 变量的默认字符串值替换为索引 2。
local dialogueArray = {"嗨!", "我认识你吗?", "再见!"}local function speak()local dialogue = dialogueArray[2]Chat:Chat(head, dialogue)end进行游戏测试并点击 NPC。 第二个数组值应出现在聊天气泡中。 尝试更改代码以测试表格中的每个值。

更改对话行
当玩家与 NPC 互动时,NPC 始终会说同一句话。 这很无聊。 相反,使用变量来更新要使用的索引值。
每当玩家与 NPC 互动时,将变量值加 1,以显示下一行对话。
为了跟踪当前索引,添加一个名为 dialogueIndex 的新变量。 将变量设置为 1,以便从数组的开头开始。
local dialogueArray = {"嗨!", "我认识你吗?", "再见!"}local dialogueIndex = 1在 speak() 中,替换 dialogueArray[2] 中的索引号为你刚创建的变量。
local function speak()local dialogue = dialogueArray[dialogueIndex]Chat:Chat(head, dialogue)end在函数底部,为 dialogueIndex 加 1。 下次调用 speak() 时,对话将显示下一个字符串。
local function speak()local dialogue = dialogueArray[dialogueIndex]Chat:Chat(head, dialogue)dialogueIndex += 1end进行游戏测试并点击 NPC 以查看数组中的每个对话字符串。
请注意,一旦脚本达到数组末尾,输出窗口中会出现 错误。
你将在下一部分修复此问题,以便在显示最后一个字符串后对话从头开始。
数组大小
你可以使用数组的大小来判断何时将所需的索引重置为 1。 通过在数组名称前输入 # (没有空格)来找到数组的 大小。
例如:#dialogueArray
将数组的大小与变量的当前值进行比较,以知道何时可以从头开始。
重启对话
使用数组大小来检查何时可以循环回第一条对话。
添加一个 if 语句,检查 dialogueIndex 是否等于 #dialogueArray,即该数组的总大小。 如果是,则将 dialogueIndex 设置为 1。
local function speak()local dialogue = dialogueArray[dialogueIndex]Chat:Chat(head, dialogue)if dialogueIndex == #dialogueArray thendialogueIndex = 1enddialogueIndex += 1end如果 dialogueIndex 不是在末尾,仍然应该为 dialogueIndex 加 1。 将 dialogueIndex += 1 移到 else 语句下面。
local function speak()local dialogue = dialogueArray[dialogueIndex]Chat:Chat(head, dialogue)dialogueIndex = if dialogueIndex == #dialogueArray then 1 else dialogueIndex + 1end进行游戏测试并确认你可以循环播放并重启对话。
总结
数据结构是一组数据的存储方式。 Luau 使用表格来创建数据结构。 数组是一种能够保存有序信息列表的表格类型。 数组中的每个值被分配一个索引号码,从索引 1 开始。
该脚本使用数组为一个不可播放角色(NPC)创建一个可能的对话行列表。
完成的脚本
-- 使用提示时循环聊天对话
local Chat = game:GetService("Chat")
local prompt = script.Parent
local npc = prompt.Parent
local characterParts = npc.CharacterParts
local head = characterParts.Head
-- 在这里添加数组
local dialogueArray = {"嗨!", "我认识你吗?", "再见!"}
local dialogueIndex = 1
local function speak()
local dialogue = dialogueArray[dialogueIndex]
Chat:Chat(head, dialogue)
dialogueIndex = if dialogueIndex == #dialogueArray then 1 else dialogueIndex + 1
end
prompt.Triggered:Connect(speak)
故障排除提示
如果角色没有通过对话数组,尝试以下故障排除提示。
- 检查 if 语句以确保在 dialogueIndex 被设置回 1。 在 else 语句中,确保 dialogueIndex 自增 1。
- 在获取数组大小时,确保 #dialogueArray 后没有空格。
可选挑战
尝试以下可选挑战之一。
- 编写脚本使 NPC 的对话通过数组向后移动。 对话索引应从数组长度开始,每次减去而不是添加。
- 而不是按顺序显示对话,让 NPC 每次使用 Random.new() 显示随机行对话。 下面包含了一个参考示例脚本
local randomGenerator = Random.new()
-- 每次 NPC 被点击时显示一条新对话
local function speak()
local randomIndex = randomGenerator:NextInteger(1, #dialogueArray)
local dialogue = dialogueArray[randomIndex]
Chat:Chat(head, dialogue)
end