Guard clause
Understand early exits, choose between a right-rail guard and a canvas branch, then build a door's refusal paths.
On this page
A guard clause ends a method early when it should do no more work. In the Platformer sample, the goal door refuses a missing player, a locked door, or a repeated completion before it finishes the level.
For a required reference or a null-safe call, start with the right rail’s Guards card. It keeps repeated safety checks off the canvas so the game’s work is easier to follow. A gameplay decision or a check with extra work on its refusal path can use explicit BRANCH and EXIT nodes.
The steps below teach that explicit early-exit flow using the sample’s Door class. You can follow them without reading C#. After Compile, recognized checks may appear in the method’s or node’s GUARDS card instead of as separate branches; their behavior stays in the script.
In the sample
Section titled “In the sample”The goal’s trigger fires more than once while the player stands in it. The door has to ignore a missing player, refuse entry while it is locked (with a message in the Console), and do nothing once the level is already complete. Only a valid first entry finishes the level.
When this applies
Section titled “When this applies”- A trigger or collision can fire again and again, and only the first one should count.
- Something the method needs may be missing, such as a player that has already left the scene.
- The object has states where it should refuse, such as a locked door or a pickup that is already spent.
Build the entry node and the first guard
Create TryEnter
Open Tools > Round Trip > Graph view, press Cmd/Ctrl+O and choose Door. Press the + on the Methods section of the left rail. A name box holding
NewMethodappears; typeTryEnterand press Enter. The canvas shows one magenta entry node titled TryEnter with an Exec pin.
6 stepsFull size ↗Add the player input
In the right rail’s Inputs card, press +. A row with a type dropdown and a name box appears. Choose PlayerController as the type and type
playeras the name. A magenta player pin appears on the entry node.
4 stepsFull size ↗First guard: a missing player
First guard, a missing player. Right-click blank canvas. The All actions menu opens with a Search box at the top. Under flow control, pick branch. A yellow node with BRANCH in its header appears, with Exec and Condition on its left and true and false on its right. Drag from the entry node’s Exec pin to the branch’s Exec pin. A white wire joins them.
5 stepsFull size ↗Feed the first guard's condition
Right-click again and type
Equals, then pick the Equals row with the = icon. A red EQUALS node appears with Left and Right inputs and a Result output. Drag the entry node’s player pin onto Left; a purple wire appears. Click the empty Right pin and typenull. Drag Result onto the branch’s Condition. The red wire is the color of a yes or no value.
9 stepsFull size ↗Stop on a missing player
Right-click and pick return under flow control. A magenta node headed EXIT appears with one Exec input. Drag the branch’s true output to it. A missing player now stops here.
5 stepsFull size ↗
Add the locked-door guard
Second guard: a locked door
Place another branch and drag the first branch’s false output to its Exec. Right-click, type
IsUnlocked, and press get on its row. A red IsUnlocked pill appears.Place another Equals and wire the pill into Left. Click Right, then tick and untick its checkbox to commit
false. Wire Result into the new branch’s Condition.
16 stepsFull size ↗
Finish the refusal paths and completion route
Log the locked-door message
Right-click and pick Log under logging. A teal Log node appears with a message pin. Drag the second branch’s true output to its Exec. Click message and type
"player reached the goal, but it is still locked.", including the double quotes.Place a return and wire the Log node’s Exec output into it. This message explains the refusal without continuing to the completion work.
11 stepsFull size ↗Third guard: already complete
Place a third branch and wire the second branch’s false into its Exec. Right-click, type
IsCompleted, and press get. Drag the red pill straight onto Condition; a yes or no value needs no comparison node.Wire true to a new return with nothing in between. Repeated triggers after completion now stop quietly.
13 stepsFull size ↗Mark the level complete
Right-click, type
IsCompleted, and press set. A dark IsCompleted node appears with SET in its header and a Value checkbox. Tick it, then wire the third branch’s false into its Exec.
7 stepsFull size ↗Refresh the HUD and play the sound
Get a _hud pill. Drag from its output pin onto empty canvas and choose RefreshComplete from the member menu. Its Target is already wired from the pill. Wire the SET node’s Exec output into this call.
Do the same with an _audio pill and PlayCompletion, continuing the white wire from RefreshComplete.
15 stepsFull size ↗Read the player's game object
Drag from the entry node’s player pin and press get on gameObject. The purple gameObject pill lands to the right, its wire running under the cards.
4 stepsFull size ↗Deactivate the player and log
Drag from the gameObject pill and choose SetActive. Click its value pin, then tick and untick the checkbox to commit
false. Wire PlayCompletion’s Exec output into SetActive.Place a Log, wire SetActive’s Exec output into it, and type
"goal reached: level complete."into message, including the quotes.
15 stepsFull size ↗Compile
Press Compile in the toolbar. Look in GUARDS for any recognized checks lifted from the canvas.
For a reference the class needs, such as an unassigned _hud, the right rail’s Guards card can add the stop for you. Press Add a check, pick the value in the Require a value list, and choose what happens under If _hud isn’t set…, for example Warn and skip the rest of this method.
What it writes
Section titled “What it writes”If you also work in C#, this is the sample’s Enter method, the finished shape the steps build toward. Your TryEnter writes the same three early exits in the same order, whether a check reads back as a branch or in GUARDS. An early-exit branch is written as an if with the rest of the method after it, so the completion work stays outside the refusal blocks.
Keep each reason on its own node. Folding every reason into one long condition would hide which guard owns the locked-door message.
Round Trip also reads the plainest guard on a class value as a guard: an early exit at the top of a method, on a bool field or property, with nothing else on its route. It is listed in the method’s Guards card as a guard on that bool, and its style editor is headed If _isConfigured is false… for a bool named _isConfigured. Edits made in that card wait for the toolbar Compile; the card’s own banner offers only Discard. The locked-door guard in Enter stays an ordinary branch, because its route logs before it returns.
When it goes wrong
Section titled “When it goes wrong”When it goes wrong
| Symptom | Check | Fix |
|---|---|---|
| The completion nodes still run for a missing player. | Follow the white wire from the first BRANCH node's true output. | Drag that true output to a magenta EXIT node placed from return under flow control. |
| The locked message appears after the level is complete. | Look at which yellow node's true output leads to the Log node. | Keep the Log node on the true route of the IsUnlocked guard, then Compile. |
- Branch: read true and false outputs.
- Return and throw: end a route explicitly.
- Method calls: inspect the completion calls after the guards.



