This page contains examples of Lua code as used in the Dialogue System.
Lua is a complete, full-featured language. It makes the Dialogue System extremely powerful. Using Lua, you can add very sophisticated behavior to your games. But even using only two operations – test for equality ("==") and assignment ("=") – you can make very complex, interactive dialogue without having to do anything else in Lua.
If you don't want to touch Lua code at all, you can still use the point-and-click Lua Wizards.
If you're familiar with C# or UnityScript, Lua has a few syntax differences to be aware of:
Operator | C#/UnityScript | Lua | Lua Example |
---|---|---|---|
Not equal | != | ~= | Quest["Kill_5_Rats"].State ~= "success" |
Logical AND | && | and | Variable["HasCoke"] and Variable["HasMentos"] |
Logical OR | || | or | (Actor["Player"].Age > 21) or Item["Beverage"].IsNonalcoholic |
String concatenation | + | .. | Actor["Player"].FullTitle = "Dread Lord " .. Actor["Player"].Name .. " of Skull Island" |
if | if(x==y) | if (x==y) (blank space req'd) | if (x == y) then foo() end |
The default Lua implementation that ships with the Dialogue System is LuaInterpreter. This is a fast, lightweight C# implementation of Lua. However, it does not parse one valid Lua syntax: X and not Y
, as shown below:
You can usually work around this limit by modifying your syntax slightly. If you must use and not
, you can switch to NLua (see Using NLua).
Here are some examples that you could use in a dialogue entry's Conditions field. The entry will only be considered if the condition is true.
Check if a Boolean variable has been set true
(note the use of double equals "==" to test equality):
Variable["flippedSwitch"] == true
Check if a quest is in a specific state:
CurrentQuestState("Kill 5 Rats") == "success"
Check if the player is at least 21 and has at least one beer:
(Actor["Player"].Age >= 21) and (Item["Beer"].Count > 0)
You can also use conditions like the examples above in a trigger's Trigger Conditions. In triggers, you can also add quest conditions, which are more convenient for checking quest states than writing Lua conditions.
Here are some examples of setting values. You could use them in a dialogue entry's Script field or in the Lua On Dialogue Event component.
Set a variable (note the use of single equals "=" for assignment):
Variable["spokeToKing"] = true; Variable["Gold"] = Variable["Gold"] + 50
Set a quest to a specific state:
SetQuestState("Kill 5 Rats", "active")
Combining two strings (using Lua's '..' notation):
Actor["Player"].FullTitle = "Dread Lord " .. Actor["Player"].Name .. " of Skull Island"
The Dialogue System exposes several useful functions, including:
The Dialogue System includes Lua functions that synchronize variables and quest states to all clients using Unity Networking's High Level API (UNET HLAPI). For instructions, see:
To interface Lua with your own scripts, see:
This also includes exposing your C# method to Lua so you can use them in dialogue entry Conditions and Script fields. For instructions, see:
<< How to Use Lua in Scripts | Lua Console >>