How to Build a "Collect 10 Coins" Door with Verse
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.
How to Build a "Collect 10 Coins" Door with Verse
Do you want to build a secret door that only opens when players find ten coins? You can do this easily! It is like a treasure hunt. The door stays locked. Then it unlocks when the player collects enough items. We will use Verse to make this happen. You will learn how to count things in your game. Let's start building!
What You'll Learn
- What a stat is.
- How to use a Stat Counter device.
- How to link devices together.
- How to make a door open on its own.
How It Works
Imagine you are playing a game of hide-and-seek. You need to find ten hidden tags. You cannot leave until you find them all. This is what we are building.
We need two main tools. The first tool is the Stat Creator. Think of this as a scoreboard. It keeps track of how many coins you have found. The second tool is the Stat Counter. Think of this as a referee. It watches the scoreboard. When the number reaches ten, the referee blows a whistle.
The whistle triggers the door to open. This is called an event. An event is something that happens in the game. It is like a domino effect. One thing happens, then the next thing happens. We will connect these devices with invisible wires. These wires are called connections. They tell the devices what to do.
Let's Build It
First, place your devices in the World Editor. You need a Player Spawner, a Stat Creator, a Stat Counter, a Door, and some Coins.
- Place the Player Spawner. This is where players start.
- Place the Stat Creator. Name it
CoinCounter. - Place the Stat Counter. Name it
GoalChecker. - Place a Door. Name it
SecretDoor. - Place some Coins. Use Item Granter devices for these.
Now, let's add the Verse code. This code tells the GoalChecker what to look for. Open the Verse editor for the GoalChecker device.
# This is the main script for the Goal Checker
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
# We create a new device called GoalChecker
# It is a Verse device that lives in the level
goal_checker_device := class(creative_device):
# This is the Stat Counter device we want to watch.
# Drag it into this slot in the Details panel in UEFN.
@editable
StatCounter : player_counter_device = player_counter_device{}
# This is the door we want to open.
# Drag it into this slot in the Details panel in UEFN.
@editable
Door : lock_device = lock_device{}
# This function runs when the game starts
OnBegin<override>()<suspends> : void =
# We wait for the Stat Counter to signal that its
# target value has been reached by any player.
# CountSucceedsEvent fires when the watched
# stat hits the goal set in the device's properties.
StatCounter.CountSucceedsEvent.Await()
# When the stat is reached, open the door!
# lock_device.Open requires an agent, so we use None.
Door.Open(None)```
Let's look at the code line by line.
The first line says `using { /Fortnite.com/Devices }`. This lets our code talk to Fortnite devices. It is like getting your tools ready.
Next, we define `goal_checker_device`. This is our **actor**. An actor is anything that exists in the game world. It has properties. We mark `StatCounter` with `@editable`. This means you can drag the real Stat Counter device from your level into this slot inside the UEFN Details panel.
We mark `Door` with `@editable` too. This finds the door in our level. It gives our code a handle to the door.
In `OnBegin`, we wait for the target to be reached. The Stat Counter device itself holds the goal number — set it to **10** in the device's properties panel in UEFN. The stat name must match the name in the Stat Creator device. You do not need to pass those values from code; the device reads them directly.
Finally, we call `Await()` on `TargetValueReachedEvent`. This pauses the function right there. When the counter hits 10, the event fires and the code continues. The very next line calls `Door.Open()`. The door opens!
Now, go back to the editor. Connect the devices visually.
1. Connect the **Coin Granter** to the **Stat Creator**. Set it to add 1 to `CoinsFound`.
2. Connect the **Stat Counter** to the **Door**. Set the trigger to "Open".
Play your game! Find ten coins. Watch the door swing open. You did it!
## Try It Yourself
Can you make the door close again after ten seconds? Hint: Look for a "Wait" device or a "Timer" in the Verse code. You can use `Sleep(10.0)` to pause the game for ten seconds. Then use `Door.Close()` to shut it.
## Recap
You built a smart door! You used a **Stat Creator** to count coins. You used a **Stat Counter** to watch the count. You used Verse to connect them. Now you can make gates, traps, and rewards. Programming is just connecting ideas. Keep building!
## References
* https://dev.epicgames.com/documentation/en-us/fortnite/using-stat-counter-devices-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite-creative/stat-counter-design-examples-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite/using-devices-in-fortnite
* https://dev.epicgames.com/documentation/en-us/fortnite/stat-counter-design-examples-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite/stat-creator-design-examples
Verse source files
- 01-device.verse · device
Turn this into a guided course
Add using-stat-counter-devices-in-fortnite-creative 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.