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

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

Updated beginnerCode verified

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!

Verse source files

Check your understanding

Test yourself with an interactive quiz and track your progress + earn XP — free for members.

Turn this into a guided course

Add Verse if/else branching to make decisions in your game to your free study plan — we'll suggest related pages and stitch the lot into one compile-checked, self-guided lesson with worked examples and quizzes.

Original tutorial generated by Verse Island from the Verse/UEFN knowledge base, with references to the Epic Games sources above. Code is validated against the knowledge base.

Comments

    Sign in to vote, comment, or suggest an edit.Sign in