資料結構 是程式員儲存和整理整個資料集的方式。在 Luau 中,資料結構使用表創建。 表 可以容納任何數量的值。
本文涵蓋使用 陣列 ,一種特定的表格類輸入,來創建一個對話角色。

表格
表 是可以存儲多個值的數據類型。與其他儲存單一值的數據類型不同,表不具有固定尺寸,可以容納不同類型的值混合。使用表格,您可以將項目存儲在玩家的庫存中或創建數千個玩家名稱的列表。
陣列
有不同類型的表。一種類型是 陣列 , 它儲存值列表在特定順序中。要創建一個陣列,創建一個變量並將其指派給彎曲括號 { } 。以下是用逗號分開括號內的值:
local myArray = {"item1", "item2", 10, workspace.Part, myVariable}
創建會說話的角色
要探索陣列,您將與一個無法播放的角色(NPC)合作,當單擊時,會顯示不同的對話線。
本項目將使用預製的 NPC 模型,包括部分腳本和提示偵測器,但缺少對話。
在 Explorer 中,右鍵單擊 工作區 > 從檔案中插入 並選擇下載的檔案。
編寫對話列陣列
這些步驟使用一個陣列來儲存 NPC 在玩家與它互動時說的不同短語。
在 勘探者 中,前往 NPC > 近距離提示 > 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 = "I've got one thing to say!"Chat:Chat(head, dialogue)endprompt.Triggered:Connect(speak)在剛剛創建的陣列中的括號 {} 內,輸入至少三個對話字串,用逗號分開。
local dialogueArray = {"Hi!", "Do I know you?", "Goodbye!"}
使用陣列指數
在 array 中的每個值都被指派 index 號碼。索引會被分配到值在值存儲的順序。第一個值位於指數 1,第二個位於指數 2,等等。
一些編程語言,例如 Java,在 0 開始索引。
在剛剛創建的陣列中,"Hi" 在第 1 索引,而 "Goodbye!" 在第 3 索引。
索引 | 值 |
---|---|
1 | Hi |
2 | 今天是個美好的一天! |
3 | 再見! |
使用特定的索引值
使用索引值來將特定對話片段分配給 NPC。要使用特定指數上的值,請直接在 array 名稱後面加入括號,例如 dialogueArray[1] 。
將 dialogue 變量的預設字串值替換為指數 2。
local dialogueArray = {"Hi!", "Do I know you?", "Goodbye!"}local function speak()local dialogue = dialogueArray[2]Chat:Chat(head, dialogue)end進行遊戲測試並點擊 NPC。第二個陣列值應出現在聊天泡泡中。嘗試將代碼變更以測試表中的每個值。
變更對話線條
當玩家與 NPC 互動時,NPC 總是會說同一條線。那很無趣。取而代之,使用變量來更新要使用的索引值。
每當玩家與 NPC 互動時,增加變數值 1 以顯示下列對話行。
要跟蹤目前的索引,請新增名為 dialogueIndex 的新變量。將變量設為 1 以在陣列表的開頭開始。
local dialogueArray = {"Hi!", "Do I know you?", "Goodbye!"}local dialogueIndex = 1在 speak() 中, 替換 索引號在 dialogueArray[2] 中使用您剛剛創建的變量。
local function speak()local dialogue = dialogueArray[dialogueIndex]Chat:Chat(head, dialogue)end在函數底部,添加 1 到 dialogueIndex 。下次 speak() 被呼叫時,對話框將顯示下一個字串。
local function speak()local dialogue = dialogueArray[dialogueIndex]Chat:Chat(head, dialogue)dialogueIndex += 1end進行遊戲測試並點擊 NPC 來查看陣列中的每個對話字串。
注意一旦腳本到達陣列的末端,輸出窗口會有 錯誤 。
您將在下一個部分修復這個問題,因此在它顯示最後一個字串後,對話框將從頭開始重新啟動。
數組尺寸
您可以使用陣列的大小來知道何時需要將所需的索引重設為 1。在 array 名稱前輸入 大小 , 沒有空格, 以找到 array 的 #。
例如:#dialogueArray
檢查變數的當前值與陣列的大小來知道何時是時候從頭開始了。
重新啟動對話
使用陣列大小來檢查何時是時候回到第一個對話片段的時候。
添加 if 聲明,並檢查 if dialogueIndex 等於 #dialogueArray , 這個陣列的總大小。如果是,則將 dialogueIndex 設為 1。
local function speak()local dialogue = dialogueArray[dialogueIndex]Chat:Chat(head, dialogue)if dialogueIndex == #dialogueArray thendialogueIndex = 1enddialogueIndex += 1end如果 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 + 1end播放並確認您可以循環通過並重新啟動對話。
總結
數據結構是數據集的存儲方式。Luau 使用表來創建數據結構。陣列是一種表格,可以存儲排序列表的資訊。每個 array 內的值都會被指派一個索引號,從索引 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 的對話通過 array 返回到前進的狀態。對話指數應該從陣列開始,每次減去,而不是添加。
- 不要按順序顯示對話,而是讓 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