Unlock the Secret Door
Unverified — this example may not compile as-is.
The Verse code below did not pass our automated compile check, so it isn't marked verified and is hidden from the guide listing. The explanation may still be useful, but treat the code as a draft and adapt it before use.
Unlock the Secret Door
Do you want to hide a treasure chest behind a secret door? Or maybe create a maze where players must find a key? Today, we will build a smart door. It will stay locked until a player finds the key. Then, it will click open automatically. This is a classic adventure game trick. Let’s make it happen with Verse.
What You'll Learn
- How to connect a Trigger to a Door.
- How to use a Variable to remember if the door is open.
- How to write simple Verse code for your island.
- How to test your creation in-game.
How It Works
Imagine a door is like a lunchbox. It has a latch. The latch keeps it closed. You need a key to unlock it. In Fortnite, we use a Lock Device. This is a special object that acts like a door with a lock.
But a door doesn’t open by itself. It needs a signal. Think of it like a light switch. You flip the switch, and the light turns on. In our game, we need a signal to say, "The player has the key!"
We will use a Trigger Volume. This is an invisible box in the game world. When a player walks into this box, it sends a signal. We will tell the door: "When you get this signal, unlock and open!"
We also need a way to remember if the door is already open. We use a Variable. A variable is like a sticky note. It holds a value that can change. We will call our sticky note IsDoorOpen. If it is true, the door is open. If it is false, the door is closed.
Let's Build It
First, open UEFN and create a new island. Place a Lock Device in the editor. This is your door. Name it MyDoor so we can find it easily.
Next, place a Trigger Volume near the door. Make it big enough for a player to walk into. Name it KeyTrigger.
Now, we need to add Verse code. Click on the Trigger Volume. Go to the Verse tab in the details panel. Click Create New Verse Script. This opens the code editor.
We will write code to listen for players. When a player enters the trigger, we will unlock the door.
Here is the code. Copy this into your script.
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
# This is our smart script for the door
SmartDoor := class(verse_device):
# We connect to the door device here.
# This is like plugging in a wire.
Door: lock_device = lock_device{}
# We connect to the trigger volume here.
# This is like setting up a motion sensor.
Trigger: volume_device = volume_device{}
# This variable remembers if the door is open.
# It starts as false (closed).
var IsDoorOpen: logic = false
# This runs when the game starts.
OnBegin<override>()<suspends>: void =
# We tell the trigger: "Watch for players!"
# When a player enters, run the function below.
Trigger.AgentEntersEvent.Subscribe(OnPlayerEnter)
# This function runs when a player enters the trigger.
OnPlayerEnter := func(agent: agent): void =
# Check if the door is already open.
if (IsDoorOpen == false):
# Unlock the door for this player.
# Think of this as turning the key.
Door.Unlock(agent)
# Open the door.
# Think of this as pushing the door open.
Door.Open(agent)
# Update our sticky note.
# Now the door is open!
IsDoorOpen = true
Let’s look at the code step-by-step.
First, we use using. This tells Verse we want to use Fortnite devices. It is like saying, "I need my toolbox."
Next, we define our SmartDoor class. A class is a blueprint. It tells Verse what our device is made of. We connect the Door and the Trigger to variables. This links the code to the objects in the editor.
We create IsDoorOpen. This is our variable. It starts as false. This means the door is locked at the start.
The OnBegin function runs when the level starts. It sets up a subscription. Subscribe means "wait for this event." We wait for AgentEntersEvent. An agent is a player or NPC. When they enter the volume, we call OnPlayerEnter.
The OnPlayerEnter function does the work. It checks IsDoorOpen. If it is false, we run Door.Unlock and Door.Open. Then we set IsDoorOpen to true. This stops the door from unlocking again. It only opens once.
Try It Yourself
Now, test your island! Press the Play button. Walk into the trigger volume. Did the door open?
Challenge: What if you want the door to close again after a few seconds? Or what if you want it to stay open forever?
Hint: Try changing IsDoorOpen = true to IsDoorOpen = false after a short delay. You can use a timer device for this. Or, remove the check so the door unlocks every time!
Recap
You built a smart door! You learned how to use a Trigger Volume to detect players. You used a Variable to remember the door’s state. And you wrote Verse code to unlock and open a Lock Device. Great job! You are now a Fortnite island creator.
References
- https://dev.epicgames.com/documentation/en-us/uefn/change-a-players-point-of-view-with-cameras-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/en-us/fortnite/change-a-players-point-of-view-with-cameras-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/en-us/fortnite/deserted-domination-template-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/en-us/uefn/transitioning-player-point-of-view-with-cameras-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/en-us/fortnite/verse-api/fortnitedotcom/devices/lock_device/open
Verse source files
- 01-standalone.verse · standalone
Turn this into a guided course
Add Door Open 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.