Dialogue System for Unity - Lua 教學

Dialogue System for Unity 是一套支援 Unity 的對話系統外掛程式,使用 Lua 可以製作各種互動功能。

 

Dialogue System 使用一種稱為 Lua 的腳本語言,它提供控制對話流程與任務狀態的強大功能,而在大多數情況下,您可以使用 point-and-click 選單而不用自行撰寫腳本。您可以在這些地方使用 Lua :

  • Conditions 與 Script 欄位。
  • In [lua(code)] tags within the text of dialogue entries 與 Quest 欄位 ( Markup Tags)
  • Dialogue System Trigger
  • 在您自已撰寫的腳本使用 Lua / DialogueLua 類別 ( 官方教學 )
  • Dialogue Editor 的 Watches 分頁標籤與 Lua Console in-game component.

 

遊戲運行時 Dialogue System 將您的對話資料庫視為 Read-only 唯讀,它將資料庫的數值載入到 Lua 中,遊戲進行時可以檢查與變更目前的數值。

 

大部份可手動輸入 Lua code 的欄位點擊 ... 按鈕,可切換為 point-and-click 點擊模式。在此模式可使用下拉菜單,無需輸入任何代碼:

按下 '+' 按鈕增加新的 Conditions 或 Actions ,按下 Revert 按鈕來取消,若按下 Apply 按鈕來套用您的選擇,將會關閉下拉式選單,並且自動轉換成 Lua code 腳本。

注意! 如果變數包含 "." 例如 "Global.Temperature" ,則將會顯示於子選單。

例如,下拉式選單將會有一個子選單 named Global containing an item named Temperature.

 

撰寫 Lua code

您將會發現Lua code 類似 C# 或 UnityScript,瀏覽 www.lua.org 檢視更多資訊。它們的語法有些不同:

Operator Unity C# Lua Lua 範例
不等於 != ~= Quest["Kill_5_Rats"].State ~= "success"
AND && and Variable["HasCoke"] and Variable["HasMentos"]
OR || or (Actor["Player"].Age > 21) or Item["Beverage"].IsNonalcoholic
連接字串 + .. Actor["Player"].FullTitle = "Dread Lord " .. Actor["Player"].Name .. " of Skull Island"
if if(x==y) if (x==y) if (x == y) then foo() end 注意有空白喔!!!
註解 // – 這是註解

 

指定變數

Variable["spokeToKing"] = true;

Variable["Gold"] = Variable["Gold"] + 50

 

檢查變數

檢查一個布林變數是否為 true (注意!使用雙等於 "==" 來判定是否相等)

Variable["flippedSwitch"] == true

 

檢查 Quest State 任務狀態

CurrentQuestState("Kill 5 Rats") == "success"

 

Check Actor 與 Item 欄位

檢查玩家角色是否大於 24 歲,而且啤酒的數量大於 1

(Actor["Player"].Age >= 21) and (Item["Beer"].Count > 0)

 

合併字串

合併 2 個字串 (使用 Lua 的 '..' notation):

Actor["Player"].FullTitle = "Dread Lord " .. Actor["Player"].Name .. " of Skull Island"

 

特殊資料與功能

Table 類似 Unity C# 的陣列,以下的 Dialogue Systemspecific tables 可用於您的 Lua 腳本:

Actor[]

Item[]

Quest[]

Location[]

Conversation[]

Variable[]

 

特殊變數

Dialogue System 也會在 Variable[] table 管理以下這些變數:

Variable["Alert"]  
Variable["Actor"]  
Variable["Conversant"]  
Variable["ActorIndex"]  
Variable["ConversantIndex"]  

 

變數 Variable["Actor"] 與 Variable["Conversant"] 包含參與者的顯示名稱。

變數 Variable["ActorIndex"] 與 Variable["ConversantIndex"] 是 Lua Actor[] table 表中的索引。

You can use them in your dialogue entries' Conditions and Script fields. For example, to check if the current conversant is at least 21 years old:

您可以在對話的 Conditions 與 Script 欄位使用它們,例如,檢查演員是否超過 21 歲:

Actor[ Variable["ConversantIndex"] ].Age >= 21