数组简介

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

数据结构 是程序员存储和组织整个数据集的方式。在 Luau 中,数据结构是通过表创建的。 可以容纳任意数量的值。

本文介绍了使用 数组,一种特定的表类型,来创建一个会说话的角色。

是可以容纳多个值的数据类型。与存储单个值的其他数据类型不同,表没有固定大小,可以容纳不同类型的值。使用表,您可以存储玩家的库存物品或创建数千个玩家名称的列表。

数组

有不同类型的表。一种类型是 数组,它以特定顺序存储值的列表。要创建数组,创建一个变量并赋值为花括号 { }。在括号内用逗号分隔值,如下所示:

local myArray = {"item1", "item2", 10, workspace.Part, myVariable}

创建一个会说话的角色

为了探索数组,您将与一个非可玩角色 (NPC) 一起工作,当点击时,它会显示不同的对话。

该项目将使用一个预制的 NPC 模型,其中包括部分脚本和提示检测器,但缺少对话。

  1. 下载模板 NPC。

  2. 在资源管理器中,通过右键单击 Workspace从文件插入 导入 NPC,并选择下载的文件。

编写对话数组

这些步骤使用数组存储 NPC 在玩家与其互动时要说的不同短语。

  1. 资源管理器 中,转到 NPCProximityPromptChatManager

  2. 在 ChatManager 中,在脚本中标记的位置,创建一个 空数组 来存储对话选项。

    -- 在提示使用时循环聊天对话
    local Chat = game:GetService("Chat")
    local prompt = script.Parent
    local npc = prompt.Parent
    local characterParts = npc.CharacterParts
    local head = characterParts.Head
    -- 在这里添加数组
    local dialogueArray = {}
    local function speak()
    local dialogue = "我有一句话要说!"
    Chat:Chat(head, dialogue)
    end
    prompt.Triggered:Connect(speak)
  3. 在刚创建的数组的括号 {} 内,输入至少三个用逗号分隔的对话字符串。

    local dialogueArray = {"嗨!", "我认识你吗?", "再见!"}

使用数组索引

数组中的每个值都被分配一个 index 数字。索引是根据值存储的顺序分配给值的。第一个值的索引为 1,第二个为 2,以此类推。

一些编程语言,如 Java,从 0 开始索引。

在刚创建的数组中,"嗨!" 的索引为 1,"再见!" 的索引为 3。

索引
1
2今天是个好日子!
3再见!

使用特定索引值

使用索引值将特定的对话片段分配给 NPC。要使用特定索引的值,请在数组名称后直接添加索引,像这样 dialogueArray[1]

  1. dialogue 变量的默认字符串值替换为索引 2。

    local dialogueArray = {"嗨!", "我认识你吗?", "再见!"}
    local function speak()
    local dialogue = dialogueArray[2]
    Chat:Chat(head, dialogue)
    end
  2. 进行游戏测试并点击 NPC。第二个数组值应该出现在聊天气泡中。尝试更改代码以测试表中的每个值。

更改对话行

当玩家与 NPC 互动时,NPC 将始终说同一句话。这很无聊。相反,使用一个变量来更新要使用的索引值。

每当玩家与 NPC 互动时,将变量值加 1,以显示下一行对话。

  1. 为了跟踪当前索引,添加一个名为 dialogueIndex 的新变量。将变量设置为 1,以从数组的开头开始。

    local dialogueArray = {"嗨!", "我认识你吗?", "再见!"}
    local dialogueIndex = 1
  2. speak() 中,替换 dialogueArray[2] 中的索引数字为您刚创建的变量。

    local function speak()
    local dialogue = dialogueArray[dialogueIndex]
    Chat:Chat(head, dialogue)
    end
  3. 在函数的底部,将 1 加到 dialogueIndex。下次调用 speak() 时,对话将显示下一个字符串。

    local function speak()
    local dialogue = dialogueArray[dialogueIndex]
    Chat:Chat(head, dialogue)
    dialogueIndex += 1
    end
  4. 进行游戏测试并点击 NPC,以查看数组中的每个对话字符串。

注意,一旦脚本到达数组的末尾,输出窗口中会出现一个 错误

您将在下一部分修复此问题,以便在显示最后一个字符串后对话从头开始。

数组大小

您可以使用数组的大小来知道何时将所需的索引重置为 1。通过在数组名称前输入 #(不带空格)来查找数组的 大小

例如:#dialogueArray

检查数组的大小与变量的当前值,以知道何时该从头开始。

重启对话

使用数组大小检查何时该循环回第一个对话片段。

  1. 添加一个 if 语句,检查 dialogueIndex 是否等于 #dialogueArray,即该数组的总大小。如果是,则将 dialogueIndex 设置为 1。

    local function speak()
    local dialogue = dialogueArray[dialogueIndex]
    Chat:Chat(head, dialogue)
    if dialogueIndex == #dialogueArray then
    dialogueIndex = 1
    end
    dialogueIndex += 1
    end
  2. 如果 dialogueIndex 不在末尾,它仍然应该将 1 加到 dialogueIndex。将 dialogueIndex += 1 移到 else 语句下。

    local function speak()
    local dialogue = dialogueArray[dialogueIndex]
    Chat:Chat(head, dialogue)
    dialogueIndex = if dialogueIndex == #dialogueArray then 1 else dialogueIndex + 1
    end
  3. 进行游戏并确认您可以循环并重启对话。

总结

数据结构是存储数据集的方式。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
©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。