If/Else: Branching Race Decisions

In this lesson you'll learn to

  • βœ“Write failable conditions and if/else chains that gate gameplay logic.

Write failable conditions and if/else chains that gate gameplay logic.

πŸ“– Reference & full walkthrough: If/Else: Branching Race Decisions

🧩 Your capstone piece: Checkpoint-order validation and win checks


The Magic Door: Making Your Island Think with If/Else

Welcome, future game maker! Branching is how games make decisions. A door opens if you have a key. A trap hurts you if you step on it. In Verse we write that word for word using the if expression.

What you'll learn

  • What an if statement is and how else handles the opposite case.
  • How Verse conditions use success and failure (not just true/false).
  • How to subscribe to a Trigger Volume's events for enter and exit.
  • How to drive a Prop Mover to open and close a door with code.

How it works

Imagine a waiter asking, "Pizza or pasta?"

  • If you say "Pizza," you get pizza.
  • Else you get pasta.

An if in Verse works the same way. It checks a condition. If the condition succeeds, the first block runs. If it fails, the else block runs instead.

Verse conditions are a little special. Instead of only checking true/false, they check whether something succeeds. To test a logic value we read it with a question mark: if (DoorIsOpen?): means "if the door-is-open flag is true."

We use two devices:

  • A Trigger Volume β€” an invisible box. TriggeredEvent fires when a player walks in; EndTriggerEvent fires when they walk out.
  • A Prop Mover β€” moves a prop (the door). Begin() starts it moving toward the open position; End() sends it back.

Verse listens to the trigger and tells the prop mover what to do.

Let's build it

Set up the scene in UEFN:

  1. Place a door prop in the world.
  2. Place a Prop Mover device. Set its Target Prop to your door, pick a Translate movement, and set the open distance.
  3. Place a Trigger Volume in front of the door, big enough to walk into.
  4. Create a Verse device named smart_door, add the code below, build it, and drop it in the level.
  5. Select your Verse device and, in its Details panel, set the two @editable fields to your Prop Mover and Trigger.
# This device opens a door when a player enters a trigger,
# and closes it again when they leave β€” using if/else branching.

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

smart_door := class<concrete>(creative_device):

    # The Prop Mover that physically slides the door.
    @editable
    DoorMover : prop_mover_device = prop_mover_device{}

    # The Trigger Volume the player walks into.
    @editable
    DoorTrigger : trigger_device = trigger_device{}

    # Remember the door's state so we only move it when needed.
    var DoorIsOpen : logic = false

    # Runs once when the game starts.
    OnBegin<override>()<suspends> : void =
        # Fires when a player steps INTO the trigger.
        DoorTrigger.TriggeredEvent.Subscribe(OnPlayerEnter)
        Print("Smart Door is ready and waiting.")

    # Called automatically when a player enters the trigger.
    OnPlayerEnter(Agent : ?agent) : void =
        # if/else branch: toggle the door based on its current state.
        if (DoorIsOpen?):
            # Currently open β€” close it.
            set DoorIsOpen = false
            DoorMover.End()     # move back toward the CLOSED position
            Print("The player left β€” closing the door.")
        else:
            set DoorIsOpen = true
            DoorMover.Begin()   # move toward the OPEN position
            Print("A player arrived β€” opening the door!")```

## Try it yourself

*   **Flip the logic:** Make a *trap* door that only moves when the door is closed AND the player has been near for a while.
*   **Announce it:** Change the `Print` messages to describe your own game's story.
*   **Two triggers:** Add a second trigger so the door opens from either side. Subscribe both triggers' `TriggeredEvent` to the same `OnPlayerEnter` handler.
*   **Locked door:** Add a `var IsUnlocked : logic = false` and only open the door when `if (IsUnlocked?, not DoorIsOpen?):`.

## Recap

*   `if (Condition): ... else: ...` runs one block when the condition **succeeds** and the other when it fails.
*   A `logic` value is tested by reading it: `if (DoorIsOpen?):`.
*   To change a `var`, use `set DoorIsOpen = true` β€” never a bare `=`.
*   `TriggeredEvent` and `EndTriggerEvent` let a Trigger Volume tell your code when players enter and leave.
*   `DoorMover.Begin()` opens the door; `DoorMover.End()` closes it. That's branching in action!
πŸ”’

Keep going β€” free

You've read the intro. The rest of this lesson is free for members. Sign in to continue and track your progress.

Sign in free to continue this lesson
Pass the quiz above to chart this quest in your Journal.

Sources

/guides/verse-if-else-branching-to-make-decisions-in-your-game
Source

Biloxi Studios original lesson

Guided course

Add this lesson to your free study plan.

🧭 The Keeper’s log

Quest complete? Chart your next heading from the 🐻 North Jungle expedition.

β›΅ Take me there