Stop Pressing Buttons 50 Times: How to Automate Your Island with Verse Loops
Stop Pressing Buttons 50 Times: How to Automate Your Island with Verse Loops
Imagine you're building a boss arena. You want to spawn five waves of enemies. Do you really want to drag and drop five separate spawners, wire up five separate triggers, and pray you didn't mess up the coordinates? No. That's tedious, it's messy, and it's why we have Verse.
In this tutorial, we're going to build a Chaos Button. You press it once, and it spawns a random number of explosions across the map. No more manual wiring. No more copy-pasting logic. We're going to use Loops—the programming equivalent of "do this thing X times"—to automate the chaos.
What You'll Learn
- What a Loop is (and why it's better than hitting F5 on your code).
- How to use a For Loop to repeat actions a set number of times.
- How to use Random values inside a loop to create unpredictable gameplay.
- How to write clean, bug-free Verse code that actually works in UEFN.
How It Works
What is a Loop?
In Fortnite, if you want to run around the island three times, you don't run, stop, restart, run, stop, restart. You just say, "Run three laps."
In programming, a Loop is a structure that repeats a block of code multiple times. Instead of writing the same line of code ten times, you write it once and tell the computer, "Do this ten times."
Think of it like a Storm Timer. The storm doesn't shrink once and stop; it shrinks, waits, shrinks again, waits, and so on, until the storm phase is complete. That's a loop.
The "For Loop"
The most common type of loop is the For Loop. It's perfect for when you know exactly how many times you want to repeat something.
Pseudocode (fake code for planning) looks like this:
Repeat 5 Times {
Spawn an enemy
}
In Verse, we use a special counter variable (usually called i or index) to keep track of which repetition we're on. It's like a lap counter in a racing game.
Why Use Loops?
- Speed: Write once, repeat forever.
- Consistency: You won't forget to wire up the 7th spawner because you got tired.
- Scalability: Want 100 explosions instead of 5? Change one number, not 95 lines of code.
Let's Build It
We're building a Random Explosion Generator. When you press a button, it will spawn a random number of firecrackers (represented by simple props) at random locations near the button.
Step 1: The Setup
- Open UEFN and create a new Creative island.
- Place a Trigger Volume (a box that detects when you step on it).
- Place a Prop (a simple crate or firecracker model) somewhere nearby. This is what we'll "clone."
- Open the Verse Editor for the Trigger Volume.
Step 2: The Verse Code
Here is the complete code. Don't worry if it looks scary—we'll break it down line by line.
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }
# This is our main device. It attaches to a trigger_device placed in the level.
chaos_explosion_device := class(creative_device):
# CONFIGURATION
# How many explosions do we want at most?
# Change this value in the UEFN details panel.
@editable
ExplosionCount : int = 5
# The trigger device wired to this script (drag it in via the UEFN details panel).
@editable
Trigger : trigger_device = trigger_device{}
# This runs when the game starts
OnBegin<override>()<suspends> : void =
# Subscribe to the trigger's TriggeredEvent so we react when a player steps on it.
Trigger.TriggeredEvent.Subscribe(OnTriggered)
# This runs when a player activates the trigger.
# trigger_device passes an optional agent; we accept it as ?agent.
OnTriggered(Agent : ?agent) : void =
# Unwrap the agent; do nothing if no agent was provided.
if (A := Agent?):
# Cast the agent to a fort_character so we can read their position.
if (Character := A.GetFortCharacter[]):
spawn { RunExplosions(Character) }
# Contains the loop logic, called from OnTriggered.
RunExplosions(Character : fort_character)<suspends> : void =
# 1. Pick a random number of explosions between 1 and ExplosionCount.
# GetRandomInt(Min, Max) returns an int in [Min, Max] inclusive.
NumExplosions : int = GetRandomInt(1, ExplosionCount)
# 2. Read the character's current world position once.
OriginTransform := Character.GetTransform()
OriginPosition := OriginTransform.Translation
# 3. LOOP: Repeat the explosion logic 'NumExplosions' times.
# Verse's counted for-loop syntax: for (I := 0..NumExplosions - 1).
for (I := 0..NumExplosions - 1):
# 4. Calculate a random offset on the XY plane around the player.
OffsetX : float = GetRandomFloat(-200.0, 200.0)
OffsetY : float = GetRandomFloat(-200.0, 200.0)
# Build the spawn position by adding the offset to the origin.
# vector3 is the correct SpatialMath type for positions in UEFN Verse.
SpawnPosition : vector3 = vector3{
X := OriginPosition.X + OffsetX,
Y := OriginPosition.Y + OffsetY,
Z := OriginPosition.Z
}
# 5. Print to the output log so we know each iteration fired.
# Print() writes to the UEFN output log; no player-facing API is needed here.
Print("Boom! Explosion #{I + 1} at ({SpawnPosition.X}, {SpawnPosition.Y})")
# 6. Small delay between explosions so they feel staggered, not instant.
Sleep(0.1)```
### Breaking It Down
#### 1. The Setup (`chaos_explosion_device := class(creative_device)`)
This tells Verse, "I'm making a device that lives inside the game world." `creative_device` is the real base class for all placeable UEFN Verse devices—it's what wires your script to the editor.
#### 2. Configuration (`ExplosionCount`, `Trigger`)
These are **Variables** marked `@editable`. Think of them as **Loot Pool Settings**. You set them once in the editor (like choosing "Rare" or "Epic" loot), and the code uses those values. `Trigger` is the `trigger_device` you placed on the island, wired in via the details panel.
#### 3. `OnTriggered`
This is an **Event handler** subscribed in `OnBegin`. It's like a **Trigger**. When a player steps on the volume, `Trigger.TriggeredEvent` fires and calls this function. We unwrap the optional agent, cast it to `fort_character`, and hand off to `RunExplosions`.
#### 4. The Loop (`for (I := 0..NumExplosions - 1):`)
This is the magic part. Let's translate it to Fortnite terms:
* `0..NumExplosions - 1`: A Verse **range**. Start at lap 0, stop at the last lap.
* Verse handles incrementing `I` for you—no manual `i = i + 1` needed.
Inside the loop, we do the following:
* **Random Offset:** We call `GetRandomFloat` to generate a random X and Y value to move the spawn point around. This prevents all explosions from happening in the exact same spot.
* **vector3:** The correct UEFN SpatialMath struct for a 3-D position, built by adding the random offset to the player's origin.
* **Print:** Writes `"Boom! Explosion #1 at (…)"` to the UEFN output log each iteration so you can confirm the loop is running.
* **Sleep(0.1):** A tiny suspending delay that staggers the explosions so they feel sequential rather than all firing in a single frame.
## Try It Yourself
**Challenge:** Modify the code so that the explosions don't just appear—they **move** toward the player!
**Hint:**
1. Look up `Prop` methods in the Verse documentation.
2. Find a way to set the **Velocity** of the spawned prop.
3. Calculate the direction from the explosion location to the player's location using `GetTransform()` and vector math (or just guess a direction for now).
4. Set the velocity to push the prop toward the player.
*Don't worry if it doesn't work perfectly. That's how you learn. Break it, fix it, break it again.*
## Recap
* **Loops** let you repeat code without rewriting it.
* **For Loops** are great when you know how many times to repeat (e.g., "spawn 5 enemies").
* **Variables** inside loops (like `I`) help you track progress.
* **Randomness** inside loops creates unpredictable, fun gameplay.
Now go make some chaos. And remember: if you need to spawn 1000 enemies, you don't need 1000 lines of code. You just need one loop.
## References
- https://dev.epicgames.com/documentation/en-us/fortnite/making-music-lesson-plan-in-fortnite-creative
- https://dev.epicgames.com/documentation/en-us/fortnite-creative/lesson-plan-making-music-in-fortnite-creative
- https://dev.epicgames.com/documentation/en-us/fortnite/loop
- https://dev.epicgames.com/documentation/fortnite/verse-glossary
- https://dev.epicgames.com/documentation/en-us/fortnite/how-to-design-a-game-in-fortnite-creative
Verse source files
- 01-device.verse · device
Turn this into a guided course
Add Introduction to Loops 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.