Stop the Chaos: How to Lock Your Puzzle Once It’s Solved
Tutorial beginner compiles

Stop the Chaos: How to Lock Your Puzzle Once It’s Solved

Updated beginner Code verified

Stop the Chaos: How to Lock Your Puzzle Once It's Solved

So you've built a light puzzle. Players mash buttons, lights flash, and eventually, the loot drops. It's glorious. But then a rogue player solves it, grabs the item, and immediately starts turning all the lights off and on again to mess with their teammates. Or worse, they keep clicking the buttons, breaking your logic and resetting the puzzle mid-game.

It's like letting someone edit the storm timer while the storm is active. You need to lock the door. In this tutorial, we're going to learn how to unsubscribe from player inputs once the puzzle is solved, ensuring that once the job is done, the buttons go silent. No more button mashing. Just victory.

What You'll Learn

  • Events as Triggers: Understanding how devices "shout" when something happens.
  • Subscriptions: How your code listens to those shouts.
  • Cancelable Handles: The "unsubscribe" button for code.
  • Locking Logic: Using a state variable to prevent further interaction.

How It Works

In Fortnite UEFN, devices don't just sit there. They have Events. An event is like a trigger volume that fires code when a player touches it, but instead of physical collision, it's digital. When a player interacts with a Button Device, the device fires an InteractedWithEvent.

Your Verse script subscribes to this event. Think of subscribing like agreeing to get notifications on your phone. Every time the button is clicked, your phone (your script) buzzes, and it runs the code you wrote to handle that click.

But here's the problem: if you stay subscribed forever, your phone keeps buzzing even after the notification is irrelevant. If the puzzle is solved, you don't want the buttons to buzz anymore. You want to unsubscribe.

In Verse, when you subscribe to an event, you get back a special object called a Cancelable Handle. This handle is like a remote control for that specific subscription. If you press the "Cancel" button on that remote, the device stops sending notifications to your script. The event still happens in the game world (the player can still click the button), but your code ignores it completely.

We'll combine this with a simple Boolean (a variable that is either True or False) to track if the puzzle is solved. Once it's True, we hit cancel on our event subscriptions, and the buttons effectively become dead weight.

Let's Build It

We're going to create a simple Verse script attached to a puzzle device. It will listen for button clicks, check if the combination is right, and if it is, it will "mute" the buttons so they can't be clicked again.

The Setup

  1. Place a Button Device in your level.
  2. Place a Tagged Light or a Prop that changes color when solved (our "goal").
  3. Create a new Verse script in UEFN.
  4. Attach the script to the Button Device (or a parent device that controls them).

The Code

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

# Define our main script structure
tagged_lights_puzzle := class(creative_device):

    # 1. References to Button Devices placed in the level.
    # Wire these up in the UEFN editor Content Browser properties panel.
    @editable
    Button1 : button_device = button_device{}

    @editable
    Button2 : button_device = button_device{}

    # 2. The "Remote Controls" for our subscriptions.
    # We store these here so we can cancel them later.
    # Think of these as the "mute buttons" for each input.
    var ButtonSubscriptions : []cancelable = array{}

    # 3. The "State" of our puzzle.
    # False = Puzzle is active. True = Puzzle is solved.
    var IsSolved : logic = false

    # This function runs when the script starts.
    OnBegin<override>()<suspends> : void =
        # Subscribe to both buttons' interaction events.
        SubscribeToButtons()

    # Function to set up the listening.
    SubscribeToButtons() : void =
        # Subscribe to each button's InteractedWithEvent.
        # Subscribe() accepts a handler function and returns a cancelable handle.
        Handle1 := Button1.InteractedWithEvent.Subscribe(HandleButtonPress)
        Handle2 := Button2.InteractedWithEvent.Subscribe(HandleButtonPress)

        # Store both handles so we can cancel them when the puzzle is solved.
        set ButtonSubscriptions = array{Handle1, Handle2}

    # This function is called EVERY time either button is clicked.
    HandleButtonPress(Agent : agent) : void =
        # CHECK: Is the puzzle already solved?
        if (IsSolved = true):
            # If yes, ignore the click. Do nothing.
            return

        # --- PUZZLE LOGIC GOES HERE ---
        # For this example, clicking either button solves the puzzle immediately.
        # In a real puzzle, you'd check combinations here.

        Print("Puzzle Solved! Locking buttons...")

        # 1. Update State.
        set IsSolved = true

        # 2. Trigger the "Win" condition (e.g., spawn loot, change light color).
        # Here we just log it, but you'd trigger your item granter or prop mover.
        Print("Loot spawned! (Simulated)")

        # 3. UNSUBSCRIBE: Cancel every stored event handle.
        # This is the magic step. We loop through all saved handles
        # and tell each one to stop listening.
        for (Handle : ButtonSubscriptions):
            Handle.Cancel()

        Print("All buttons unsubscribed. No more clicks allowed.")```

### Walkthrough: What Just Happened?

1.  **`cancelable` Handles:** In Verse, `Subscribe()` doesn't just start listening; it gives you a `cancelable` object. This is your insurance policy. It lets you stop the listening at any time.
2.  **The Array:** We used an `array` to store these handles. Why? Because if you have 10 buttons, you need somewhere to keep every remote. When it's time to unsubscribe, we loop through the whole array and cancel each one.
3.  **The Check:** Inside `HandleButtonPress`, the very first thing we do is check `if (IsSolved)`. This is a safety net. Even if we forget to unsubscribe, this check prevents any logic from running.
4.  **The Cancel:** Once the puzzle is solved, we call `Handle.Cancel()` on every stored handle. From this moment on, if the player clicks either button, the `InteractedWithEvent` fires, but your script doesn't care. It's like unplugging the speaker. The button can scream all it wants, but no one hears it.

## Try It Yourself

**Challenge:** The code above assumes you know how to get the button device. In a real level, you might have multiple buttons.

1.  Add two Button Devices to your level.
2.  Modify the script to subscribe to **both** buttons.
3.  Make it so that clicking **either** button solves the puzzle.
4.  Ensure that once solved, **both** buttons are unsubscribed.

**Hint:** You'll need to store two handles in your `ButtonSubscriptions` array. When the puzzle is solved, you'll need to loop through the array and cancel **every** handle, not just the one that was clicked. Look up how to iterate over an array in Verse!

## Recap

*   **Events** are how devices notify your code of actions.
*   **Subscribing** makes your code listen to those events.
*   **Cancelable Handles** are the remote controls for your subscriptions.
*   **Unsubscribing** (calling `.Cancel()`) stops your code from reacting to events, effectively "locking" the device from further input in your script.

Now go build some puzzles that stay solved. Your players (and your sanity) will thank you.

## References

*   https://dev.epicgames.com/documentation/en-us/fortnite/tagged-lights-1-creating-the-algorithm-in-verse
*   https://dev.epicgames.com/documentation/en-us/uefn/tagged-lights-1-creating-the-algorithm-in-verse
*   https://dev.epicgames.com/documentation/en-us/fortnite/tagged-lights-5-detecting-when-the-puzzle-is-solved-in-verse
*   https://dev.epicgames.com/documentation/en-us/uefn/tagged-lights-5-detecting-when-the-puzzle-is-solved-in-verse
*   https://dev.epicgames.com/documentation/en-us/fortnite/lights-and-bridges-02-puzzle-component-in-fortnite

Verse source files

Turn this into a guided course

Add 6. How do you disable player interaction after the puzzle is solved? 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