Guards
Require a method value and preview the early return that protects later work.
On this page
A missing HUD should stop a refresh before the method reaches it. A guard makes that prerequisite visible at the top of the method.
The problem
Section titled “The problem”PlayerController.CollectCoin cannot update score safely unless _scoreCounter and _hud are assigned. Continuing after either value is null would move the failure farther from its cause.
When to reach for it
Section titled “When to reach for it”Use Guards when a parameter, field, property, or local must be ready before the rest of a method runs. The picker uses real parsed values, so names are selected, never retyped. It also lists fields you added from the rail that are not compiled yet.
A bool field or property is guarded as a flag. if (!_isConfigured) return; at the top of a method is read as a guard on _isConfigured, the heading over its failure choices reads “If _isConfigured is false…”, and a bool you add from the Guards card gets that guard. A flag is checked on its own; it cannot join a combined check with values tested for null.
The plain failure choices include Stop with an error, Warn and skip the rest of this method, and Warn but carry on. For the early-return shape, choose the option that skips the method when the value is absent.
See it
Section titled “See it”
Door.Enter.The Guards card begins with No required values yet and Add a check. Its picker title is require a value. Values are grouped as parameters, fields, properties, and locals.
- Open a method and find the Guards card in the right rail.
- Choose Add a check. The require a value picker opens.
- Tick
_hud, or another value that must exist before the method continues. Already-required rows say already required and cannot be picked again. - Tick several values when they should be checked together. The confirm action reads Combine N values into one check.
- Choose Warn and skip the rest of this method for a non-throwing early return. A non-void method also asks What this method returns when it skips.
- Review the draft and its source-shaped preview. Use Add another value to this check to extend a combined guard. While the draft is pending, the card shows Previewing your changes with one action, Discard, which drops the draft.
- Press Compile in the toolbar. The guard is written before the protected work, together with any other pending change, and the source is parsed again.
- If a value is renamed or removed outside the editor, use Replace value or remove the check before compiling another change.
What it writes
Section titled “What it writes”When it goes wrong
Section titled “When it goes wrong”When it goes wrong
| Symptom | Check | Fix |
|---|---|---|
| The picker says Every value is already required | Compare the existing guard rows. | Edit or combine an existing check instead of adding a duplicate. |
| A guard says Needs a return | Confirm the method returns a value. | Choose the value the method returns when it skips. |
| A row says Value renamed or removed | Find the source value that replaced it. | Use Replace value, or remove the stale check. |
| The toolbar Compile is off and says Fix the pending change that needs attention before compiling | Look for a guard row with a warning word, such as Needs a return. | Apply the row fix, or remove the check, then Compile. |