Run game actions from a conversation#
A conversation becomes part of the game when it changes something: a reward, an open door, a quest that starts. A Command is a named list of Instructions that a Script runs with @run. You'll make the guard hand over a gate pass and a few coins, then open the gate while the conversation waits.
Before you begin#
Complete Introduce your characters, which created the Village Compendium, and Start a conversation near an NPC. You'll also need a game object named Gate Door that blocks the entrance, and an AudioClip of a creaking gate.
Create a new Script named Gate Pass and assign it to the guard's Dialogue component.
Store the player's progress#
Commands change game state, so that state needs a home. A Global Variable asset keeps values that every Event and Script in the game can read and write.
- Right-click a folder in the Project window and choose
Create→Visual Script→Variable. - Name the asset
Progress. - Add a Bool entry tagged
has-gate-pass, set to off. - Add a Number entry tagged
gold, set to10.
The guides that follow add more entries to Progress. See Global Variables in the Visual Script manual for saving them between sessions.
Define a Command#
Open the Village Compendium, click Add Command…, and set Command to give-gate-pass. Then add one Instruction to it:
| Instruction | Configuration |
|---|---|
| Set Bool | Set the has-gate-pass entry of Progress, Value on. |
A Command identifier accepts letters, digits, hyphens (-), and underscores (_). Like an Actor, it's matched exactly, including capitalization.
Run it from the Script#
Write the conversation in Gate Pass, with @run and the Command identifier on its own line:
Guard: You're the one who drove off that wolf. I owe you.
Guard: Take this. Show it at the gate, and nobody in Ashford will stop you.
@run give-gate-pass
You received a gate pass.When the Script reaches @run, it looks up give-gate-pass in the Compendiums, starts its Instructions, and moves to the next line straight away.
A missing Command does nothing
If no Compendium defines the identifier, @run skips it without an error, and the conversation carries on. When an action doesn't happen, check the identifier's spelling in both places first.
Wait for a Command to finish#
Opening the gate takes a moment, and the guard's next line should wait for it. That Command also needs Gate Door, which is a scene object, and a Compendium asset can't reference objects in a scene. Define it on the guard's Dialogue component instead:
- Select the guard and expand the Local Compendium of its Dialogue component.
- Add a Command named
open-gate. - Add these Instructions in order:
| Instruction | Configuration |
|---|---|
| Play Sound Effect | The creaking gate clip. |
| Wait for Seconds | Seconds 1.5. |
| Set Active | Game Object Gate Door, Is Active off. |
Then add the Command to the Script with await:
Guard: You're the one who drove off that wolf. I owe you.
Guard: Take this. Show it at the gate, and nobody in Ashford will stop you.
@run give-gate-pass
You received a gate pass.
Guard: Let me get that for you.
@run await open-gate
Guard: Welcome to Ashford.With await, the conversation pauses at @run until every Instruction in the Command has finished. Welcome to Ashford. appears only after the door is gone. Without await, it would appear while the gate is still creaking.
Use await when the next line depends on the result, such as a door that must be open or a camera that must finish moving. Leave it out for effects that can overlap the next line, such as a sound.
Local for this scene, global for everything else
open-gate only makes sense on this guard, so it lives on his component. give-gate-pass works anywhere, so any Script can use it from the Village Compendium. When two definitions share an identifier, the component's Local Compendium wins.
Pass a value to a Command#
The guard also hands over a few coins. You could write a give-gold-5 Command, but then every amount would need its own Command. Instead, write the amount on the @run line as metadata, and let one Command read it.
- In the
VillageCompendium, add a Command namedgive-gold. - Add an Increment Number Instruction.
- Set Set to the
goldentry ofProgress. - Set Value to the numeric Active Line Metadata Signal, with Dialogue set to Source and Metadata set to
amount.
Then pass the amount from the Script:
Guard: You're the one who drove off that wolf. I owe you.
Guard: Take this. Show it at the gate, and nobody in Ashford will stop you.
@run give-gate-pass
You received a gate pass.
Guard: And a few coins for your trouble.
@run await give-gold [amount: 5]
Guard: Let me get that for you.
@run await open-gate
Guard: Welcome to Ashford.Inside a Command, Source is the game object that holds the running Dialogue component, so the Signal reads the line that guard is playing right now. The await keeps @run give-gold as the active line until the Command finishes, so the metadata is still there when Increment Number reads it.
A missing key reads as zero
Active Line Metadata reads whole numbers and decimals, such as 2.5. When the active line has no such key, or its value isn't a number, the Signal returns 0, so a misspelled key gives the player nothing rather than an error.
The same Command now covers every reward in the game: [amount: 5] here, [amount: 50] for a quest.
Verify the result#
- Select
Progressin the Project window so you can watch its values. - Enter Play mode and talk to the guard.
- After You received a gate pass., confirm that
has-gate-passis on. - After And a few coins for your trouble., confirm that
goldchanged from10to15. - Confirm that the gate creaks, the door disappears, and only then does the guard say Welcome to Ashford.
Where to go next#
- Lock dialogue behind a requirement — read
has-gate-passandgoldback to change the conversation. - Compendiums — Commands,
@run, and@run awaitin detail. - Language syntax — the metadata format and its reserved keys.