The Magic Button: Make Your Island Respond!
The Magic Button: Make Your Island Respond!
Have you ever wanted to build a puzzle where pressing a button opens a secret door? Or maybe a game where a light turns on when you touch a switch? In Fortnite Creative, we use Verse code to make these things happen.
Today, we will build a simple "Magic Button." When a player steps on it or clicks it, a prop (like a box) will change color. It is like a magic trick that your code controls!
What You'll Learn
- How to place a Button Device in your island.
- What an Event is (it is like a notification bell).
- How to write a small script to change a prop’s color.
- How to test your creation and play your game.
How It Works
Think of a Button Device like a doorbell. When you press the button, it sends a signal. That signal is called an Event. An event is just a message that says, "Hey! Someone pressed me!"
In Verse, we write code to listen for that message. When the message arrives, our code tells another object (like a box) to do something. This is called interaction.
We will use three parts:
- The Button: The thing the player touches.
- The Box: The thing that changes.
- The Code: The brain that connects them.
Let's Build It
Follow these steps to build your Magic Button.
Step 1: Place Your Devices
- Open UEFN and create a new island.
- Go to the Device menu.
- Place a Button Device on the floor.
- Place a Cube Prop (or any box) nearby.
- Make sure both devices are enabled.
Step 2: Write the Code
We will write a script that listens for the button press. When the button is pressed, the box will turn green.
Copy this code into a new Verse file in your project.
# This is our main script. It connects the button to the box.
# We call this a "Script" because it gives instructions.
# First, we need to find our devices.
# We will look for them by their names in the editor.
my_button := ButtonDevice:Find("MyButton")
my_box := CubeProp:Find("MyBox")
# This function runs when the game starts.
Initialize <- (): void =>
# We tell the button to watch for clicks.
# When it clicks, it will run the "OnButtonPressed" function.
my_button.InteractedWithEvent.Subscribe(OnButtonPressed)
# This function runs ONLY when the button is pressed.
OnButtonPressed <- (event): void =>
# We find the box by its name.
box_to_change := CubeProp:Find("MyBox")
# If we found the box, let's change its color!
if (box_to_change != None):
# We set the color to bright green.
box_to_change.SetColor(Color(0.0, 1.0, 0.0))
# Optional: Print a message to the console to help us debug.
Print("Button pressed! The box is now green.")
Step 3: Name Your Devices
This is the most important step! The code looks for devices by name.
- Click on your Button Device in the editor.
- In the Details panel, find the Name field.
- Type exactly:
MyButton - Click on your Cube Prop.
- In the Details panel, find the Name field.
- Type exactly:
MyBox
Step 4: Test It!
- Press Play in UEFN.
- Walk up to the button.
- Interact with it (click or step on it, depending on settings).
- Watch the box turn green!
Try It Yourself
You did it! You made a device respond to a player. Now, can you make it do more?
Challenge: Try to make the box turn Red instead of Green. Or, try to make it turn Blue when you press it a second time.
Hint: Look at the SetColor line in the code. You can change the numbers inside the Color(...) brackets.
- Red is usually
Color(1.0, 0.0, 0.0) - Blue is usually
Color(0.0, 0.0, 1.0)
Can you guess what happens if you set all numbers to 1.0? (Try it and see!)
Recap
- Button Devices send an Event when a player interacts with them.
- Verse code listens for these events using
Subscribe. - We use the device Name in the editor to find them in code.
- You can change properties like Color when an event happens.
Great job, coder! You have just built your first interactive system. Keep experimenting and building amazing islands!
References
- https://dev.epicgames.com/documentation/en-us/fortnite/using-button-devices-in-fortnite-creative
- 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-1-creating-the-algorithm-in-verse
- https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-button-devices-in-fortnite-creative
- https://dev.epicgames.com/documentation/en-us/fortnite/using-button-device-design-examples-in-fortnite-creative
Verse source files
- 01-fragment.verse · fragment
Turn this into a guided course
Add Using the button device for player interaction 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.
References
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.