The Magic Switch: Understanding State in Verse
Tutorial beginner

The Magic Switch: Understanding State in Verse

Updated beginner

The Magic Switch: Understanding State in Verse

Have you ever played with a light switch? It is either ON or OFF. That simple change is called state. In Verse, state is how we track what is happening in our game right now.

We will build a secret door. It only opens when you flip three switches in the right order. You will learn how to remember those flips.

What You'll Learn

  • What state means in programming.
  • How to use variables to remember player actions.
  • How to use case to check different situations.
  • How to make a door open based on your choices.

How It Works

Imagine you are playing a video game. The game needs to know what you are doing. Is your character jumping? Is the enemy sleeping? This information is the state.

A variable is like a box. You can put something in it, and you can change what is inside. For a light switch, the box holds the word "ON" or "OFF".

We use a special tool called case. Think of case like a menu at a restaurant. You look at your order (the current state). Then you pick the right action.

  1. If the order is "Burger", you give a burger.
  2. If the order is "Fries", you give fries.
  3. If the order is wrong, you ask again.

In our game, the "order" is the combination of switch positions. The "meal" is whether the door opens or stays shut.

Let's Build It

We will make a puzzle room. You need to flip Switch 1, then Switch 2, then Switch 3. If you do it right, the door opens.

First, place these items in your level:

  • Three Switch devices. Name them Switch1, Switch2, and Switch3.
  • One Prop Mover (this is your door). Name it Door.
  • One Trigger Volume (to start the check). Name it CheckZone.

Now, let’s write the Verse code.

# This script checks if the switches are in the right order.
using { /Fortnite.com/Devices }
using { /UnrealEngine.com/Temporary/SimulatedSystem }

# We create a new script. This is our game logic.
MyPuzzleScript := class(script):

    # These are our switches. We connect them to the devices in the editor.
    Switch1: SwitchDevice
    Switch2: SwitchDevice
    Switch3: SwitchDevice
    Door: PropMoverDevice

    # This variable remembers the current state.
    # Think of it as a score counter.
    CurrentScore: int = 0

    # This function runs when you enter the CheckZone.
    CheckOrder := function(): void =
        # We check the state of each switch.
        # Is Switch 1 ON?
        if Switch1.GetState() == true:
            # If yes, we add 1 to our score.
            CurrentScore = CurrentScore + 1

        # Is Switch 2 ON?
        if Switch2.GetState() == true:
            # We add another 1 to the score.
            CurrentScore = CurrentScore + 1

        # Is Switch 3 ON?
        if Switch3.GetState() == true:
            # We add the final 1 to the score.
            CurrentScore = CurrentScore + 1

        # Now we check the total score (the state).
        # If the score is 3, all switches are ON!
        if CurrentScore == 3:
            # Open the door!
            Door.Move(1.0)
            # Reset the score for next time.
            CurrentScore = 0
        else:
            # The order was wrong. Do nothing.
            # The door stays closed.
            pass

Walkthrough

  1. Variables: We made CurrentScore. It starts at 0. It changes as we check switches.
  2. GetState(): This asks the switch, "Are you on?" It returns true or false.
  3. If Statement: This is like a fork in the road. If the condition is true, we go one way.
  4. Case Logic: We didn't use a case block here, but we used if. It does the same thing. We check if the total is 3. If it is, we win!

Try It Yourself

Can you make the door close if the player gets it wrong?

Hint: Add an else block. Inside the else, use Door.Move(0.0) to close the door. Also, try resetting CurrentScore to 0 before you check the switches, so the game doesn't remember old scores from previous rounds.

Recap

State is just a way to remember what is happening. Variables hold that memory. We use if statements to check that memory. When the memory matches what we want, we make cool things happen!

References

  • https://dev.epicgames.com/documentation/en-us/fortnite/verse-glossary
  • https://dev.epicgames.com/documentation/en-us/fortnite/escape-room-10-verse-switch-state-puzzle-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/uefn/verse-glossary
  • https://dev.epicgames.com/documentation/en-us/fortnite/case-in-verse
  • https://dev.epicgames.com/community/snippets/ARdv/fortnite-state-button

Verse source files

Turn this into a guided course

Add State 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