If you're just getting started with Verse (the programming language behind Fortnite Creative and UEFN), loops are one of the first big hurdles you'll run into. The good news? Once you get the hang of the two main loop types, they'll click fast. Let's break it down like you've never written a line of code before.
What's a Loop, Anyway?
A loop is just a way of telling the computer "do this thing over and over" instead of writing the same line of code a hundred times. Verse gives you two main tools for this: loop and for. They sound similar, but they do very different jobs.
One thing that makes Verse a little unusual: loops aren't just "do this repeatedly and forget about it." They can actually hand back a result when they're done, almost like a function returning an answer. Keep that in mind — it'll make more sense once we get to comprehensions below.
The loop Keyword: Repeat Forever (Until You Say Stop)
Think of loop as a treadmill that never turns off on its own. Once you start it, it just keeps going, and going, and going — until you physically hit the stop button. In Verse, that "stop button" is the break keyword.
If you forget to include a break somewhere inside your loop, you've basically built an infinite loop, and that will freeze up your game. So break isn't optional decoration — it's essential.
Here's what it looks like:
var Counter : int = 0
loop:
set Counter += 1
if (Counter > 5):
break # Exits the loop completely
Walking through this line by line: we start a counter at zero, and every time the loop runs, we add one to it. Once the counter climbs past 5, the if check triggers and break kicks us out of the loop entirely.
A couple of things to keep in mind with loop:
- Indentation isn't just for looks. In Verse, the code inside your loop needs to be indented consistently, or the compiler won't know what's actually inside the loop and what isn't.
- It's great for "keep this running" situations. In actual game design, you'll often see
looppaired with asleep()call inside an async context — think of things like a countdown timer or a wave of enemies that spawns every few seconds. Thesleepgives the game a breather between each repeat, so it doesn't lock everything up while it waits.
The for Expression: Repeat a Known Number of Times
While loop is for "keep going until I say stop," for is for "do this a specific number of times" or "go through each item in this list one by one." It's the tool you reach for when you already know roughly how much work needs to happen.
1. Looping Through a Range of Numbers
A "range" in Verse is just a span of numbers, written with two dots (..) between the start and end. The loop hands you each number in that range, one at a time.
# This will execute 11 times (0 through 10)
for (X := 0..10):
Print("Current index: {X}")
Notice it runs 11 times, not 10 — Verse ranges include both the starting number and the ending number.
2. Looping Through a List of Things
Say you've got a list of players in your game and you want to do something to each one — heal them, teleport them, whatever. You don't need to manually track "which player number am I on." Verse lets you just grab each item directly:
Players : []player = GetSpace().GetPlayers()
for (Player : Players):
HealPlayer(Player)
This reads almost like plain English: "for each player in Players, heal that player."
3. Loops That Build Something New (Comprehensions)
Here's where Verse gets a little more interesting than your average loop. Because loops in Verse can produce a value, you can use a for loop to transform a whole list of data into a brand new list — all in one line.
# Evaluates every item, multiplies it, and stores the resulting array
MyNumbers : []int = array{1, 2, 3}
MultipliedNumbers := for (X : MyNumbers) { X * 2 }
# MultipliedNumbers now equals array{2, 4, 6}
Instead of writing a loop that manually builds a new list step by step, you just describe what you want each item to become, and Verse hands you the finished array.
The Rules for Wants You to Follow
Verse is a bit strict about how you write for loops, mainly to keep things predictable and performance-friendly. Here's what to watch out for:
- Don't reuse your loop variable's name inside the loop. Once you've named your iteration variable (like
XorPlayerabove), you can't rename or redefine it partway through that same loop. - No
varin the setup. You can't declare a mutable variable (usingvar) as part of the loop's setup section. Verse wants that part to stay predictable. - Filters need to be able to fail. If you add a condition inside the parentheses of your
forloop to filter results, that condition has to be something that's genuinely capable of failing — like checking whether an index exists in an array — not just a simple true/false check.
Quick Recap
- Use
loopwhen you want something to repeat indefinitely, and don't forget yourbreak. - Use
forwhen you're working through a range of numbers or a collection of items you already have. - Remember that
forloops can double as a way to build new data, not just repeat actions.
Got a specific loop you're trying to build — a timer, a way to filter game data, or something that transforms an array? Let me know what you're working on and I can put together a code template tailored to it.