介绍阵列

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

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

本文涵盖使用 阵列 ,一个特定的表输入,创建一个会话角色。

表格

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

阵列

有不同类型的表。一个类型是 阵列 , 它存储特定顺序的值列表。要创建一个数组,创建一个变量并将其分配给弯曲括号 { } 。用逗号将括号内的值分开,如下所示:

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

创建一个会说话的角色

要探索阵列,您将与一个不可玩的角色(NPC)合作,单击时会显示不同的对话线。

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

  1. 下载模板 NPC。

  2. 在 Explorer 中,右击 工作区 > 从文件中导入 并选择下载的文件。

编写对话阵数组

这些步骤使用阵列来存储 NPC 在玩家与它互动时说的不同短语。

  1. 探索器 中,转到 NPC > 靠近提示 > ChatManager。

  2. 在聊天管理器中,在脚本中标记的地方,创建一个 空数组 来存储对话选项。


    -- 当提示被使用时,在聊天对话中循环
    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 = "I've got one thing to say!"
    Chat:Chat(head, dialogue)
    end
    prompt.Triggered:Connect(speak)
  3. 在刚刚创建的阵列中的括号 {} 内,键入至少三条对话字符串,用逗号分开。


    local dialogueArray = {"Hi!", "Do I know you?", "Goodbye!"}

使用阵列索引

阵列中的每个值都被分配一个 index 号。索引被分配到值的顺序,在值存储的顺序。第一个值位于索引 1,第二个在索引 2,以此类推。

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

在刚刚创建的阵列中,"Hi" 位于索引 1,而 "Goodbye!" 位于索引 3。

索引
1Hi
2今天是个美好的一天!
3再见!

使用特定索引值

使用索引值将特定对话片段分配给 NPC。要使用特定索引的值,请将索引放在数组列名称后面的括号中,如 dialogueArray[1]

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


    local dialogueArray = {"Hi!", "Do I know you?", "Goodbye!"}
    local function speak()
    local dialogue = dialogueArray[2]
    Chat:Chat(head, dialogue)
    end
  2. 进行游戏测试并单击 NPC。第二个阵列值应出现在聊天泡泡中。尝试更改代码以测试表中的每个值。

更改对话线

当玩家与 NPC 互动时,NPC 总是会说同一条线。那很无聊。相反,使用变量来更新使用哪个索引值。

每当玩家与 NPC 交互时,增加变量值 1 以显示下列对话行。

  1. 要跟踪当前索引,添加名为 dialogueIndex 的新变量。将变量设置为 1 以在数组列的开始处开始。


    local dialogueArray = {"Hi!", "Do I know you?", "Goodbye!"}
    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 不在最结束,它仍应该向 dialogueIndex 添加 1。移动 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 = {"Hi!", "Do I know you?", "Goodbye!"}
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