陣列簡介

*此內容是使用 AI(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 是我們在美國及其他國家地區的部分註冊與未註冊商標。