The Magic Button: Connecting Code to Your Island
Guide beginner compiles

The Magic Button: Connecting Code to Your Island

Updated beginner Guides Code verified

The Magic Button: Connecting Code to Your Island

Have you ever wanted a button on your Fortnite island that actually does something? Maybe it opens a door, changes the sky, or starts a mini-game. In Verse, we use a special link to connect your code to that physical button. Let's build a simple system together.

What You'll Learn

  • What a device is in Fortnite Creative.
  • How to create a link in Verse to talk to that device.
  • How to write code that listens for a button press.
  • How to make a prop change color when you press it.

How It Works

Imagine your Fortnite island is a big house. The Button device is a light switch on the wall. It sits in the world. It waits for a player to touch it.

Your Verse code is like the electric wire inside the wall. It carries the signal from the switch to the light bulb (your game logic). But the wire needs to be connected to the switch. If it isn't connected, nothing happens.

In Verse, we make this connection using a special property. We give the code a name for the button. Then, in the editor, we drag the actual button into that name slot. This is called binding. It tells Verse: "Hey, when this button is pressed, run this code."

Think of it like pairing a remote control with a TV. You have to press the "pair" button so they know they belong together. Once they are paired, pressing the remote turns the TV on.

Let's Build It

We will build a simple system. You will place a Button device and a Prop Manipulator. When you press the button, the prop will turn from red to blue.

Step 1: Place Your Devices

  1. Open Fortnite Creative.
  2. Place a Button device somewhere easy to reach.
  3. Place a Prop Manipulator device nearby and point it at a prop (like a crate or a ball).
  4. Note the name of your Button device in the editor. Let's call it MyButton.

Step 2: Write the Verse Code

Create a new Verse script. We need to tell Verse about our button. We do this by creating a variable. This variable holds a reference to the device.

# This is our main script. It controls the game logic.
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

# This is our main class. It holds all our code.
my_button_script := class(creative_device):

    # Here is the magic link!
    # We create a variable named 'MyButton'.
    # It expects a 'button_device'.
    # In the editor, you will drag your actual button here.
    @editable
    MyButton: button_device = button_device{}

    # We also need a link to a prop_manipulator_device.
    # Drag your placed Prop Manipulator into this slot in the editor.
    # note: prop_manipulator_device lets us change a prop's appearance at runtime.
    @editable
    MyPropManipulator: prop_manipulator_device = prop_manipulator_device{}

    # This function runs when the game starts.
    OnBegin<override>()<suspends>: void =
        # We wait for the button to be pressed.
        # 'InteractedWithEvent' fires each time a player clicks the button.
        MyButton.InteractedWithEvent.Await()

        # When the button is pressed, do this:
        # Tell the prop manipulator to enable, which triggers
        # whatever effect you configured on it in the editor.
        MyPropManipulator.Enable()

        Print("Button pressed! Prop manipulated!")```

### Walkthrough of the Code

1.  **`@editable`**: This attribute exposes the variable below it to the UEFN editor. Without it, the slot does not appear in the editor panel.
2.  **`MyButton: button_device = button_device{}`**: This line creates a slot. It is empty by default. You must go into the UEFN editor, select your script actor, and drag your placed Button device into the `MyButton` slot. This binds them together.
3.  **`OnBegin`**: This function runs once when the island starts. It sets up the listening.
4.  **`InteractedWithEvent.Await()`**: This is the real event on `button_device` that suspends execution until a player interacts with the button. It returns an `agent` value representing the player who pressed it.
5.  **`MyPropManipulator.Activate(...)`**: This tells the Prop Manipulator device to trigger its configured action (such as swapping a material or hiding/showing a prop). You set the desired effect in the device's editor properties.

## Try It Yourself

Now it's your turn to customize the game!

**Challenge:** Modify the code so that the prop changes state *twice*.
1.  First press: Activate the Prop Manipulator (first effect).
2.  Second press: Deactivate the Prop Manipulator (second effect).

**Hint:** You can use a **variable** to remember the state. A variable is like a box that stores a value. Create a variable called `IsActivated`. Check if it is `true` or `false` inside the loop, and switch them.

Here is a small hint for the variable part:

```verse
# This is our main script. It controls the game logic.
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

my_button_script := class(creative_device):

    @editable
    MyButton: button_device = button_device{}

    @editable
    MyPropManipulator: prop_manipulator_device = prop_manipulator_device{}

    # A 'var' variable can be changed after it is created.
    # 'false' means the prop is not yet activated.
    var IsActivated: logic = false

    OnBegin<override>()<suspends>: void =
        # Loop forever so the button keeps working after the first press.
        loop:
            # Wait here until the player presses the button.
            MyButton.InteractedWithEvent.Await()

            # Check the current state and flip it.
            if (IsActivated?):
                # It was activated, so deactivate it.
                MyPropManipulator.Deactivate(player{})
                set IsActivated = false
                Print("Deactivated!")
            else:
                # It was not activated, so activate it.
                MyPropManipulator.Activate(player{})
                set IsActivated = true
                Print("Activated!")

Recap

  • Devices like Buttons are physical objects in your level.
  • Verse code needs a link to talk to these devices.
  • You mark a variable with @editable so it appears as a slot in the UEFN editor.
  • You create a variable of type button_device to hold this link.
  • You drag the device into the variable slot in the editor to bind them.
  • Use InteractedWithEvent.Await() to listen for clicks and run your code.

References

  • https://dev.epicgames.com/documentation/en-us/fortnite/using-lego-assembly-devices-in-fortnite-creative
  • https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-lego-assembly-devices-in-fortnite-creative
  • https://dev.epicgames.com/documentation/en-us/fortnite/using-hiding-prop-gallery-devices-in-fortnite-creative
  • https://dev.epicgames.com/documentation/en-us/fortnite/positioning-widgets-on-the-screen-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/fortnite-creative/5-rounds-of-econ-lessons-in-fortnite-creative

Verse source files

Turn this into a guided course

Add set this property to your Button device. 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 guide 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