Loop and Break for Player UI Control
What you'll learn
You'll learn how the loop expression repeats a block indefinitely and how break exits it the instant a condition is met. To prove the technique end to end, you'll drive real player-UI calls (GetPlayerUI[...], AddWidget, and a text_block inside a canvas) so the loop actually does something visible.
How it works
loop:repeats its block forever. Without abreakorreturn, it never ends — an infinite loop.breakimmediately leaves the nearest enclosingloop. This is the whole point: stop as soon as you've found what you need instead of scanning the rest.- We index into an array with
Items[Index]— array indexing is fallible, so it must live inside a failure context (here, anif). If the index runs past the end, theiffails and webreakout gracefully. GetPlayerUI[Player]is<decides>(fallible), so we bind it with[]inside anif. Once we have theplayer_ui,AddWidgetshows our text on screen.
We compare item names with = (Verse equality — not ==) and increment our index with set Index = Index + 1 (there is no ++).
Let's build it
Press the button; the device loops through the item list, and the first time it matches "BananaOfTheGods" it pushes a text widget to every player and breaks. If it walks off the end of the list without a match, it shows a "not found" message and breaks too.
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }
# Device that scans a list with loop/break and updates player UI.
loop_break_ui_device := class<concrete>(creative_device):
# Button the player presses to start the scan.
@editable
TriggerButton : button_device = button_device{}
# The item names we scan through, in order.
ItemsToScan : []string = array{"Apple", "Banana", "BananaOfTheGods", "Bacon"}
# The name we are hunting for.
Target : string = "BananaOfTheGods"
OnBegin<override>()<suspends>: void =
# Fire ScanItems whenever the button is pressed.
TriggerButton.InteractedWithEvent.Subscribe(OnButtonPressed)
# Handler must take the interacting agent (button event payload).
OnButtonPressed(Agent : agent): void =
# Track where we are in the list. No ++ in Verse; use `set`.
var Index : int = 0
loop:
# Array indexing is fallible; if we run past the end the if fails.
if (ItemName := ItemsToScan[Index]):
if (ItemName = Target):
# Found it — update the UI and leave the loop early.
ShowMessage("Found {ItemName}!")
break
# Not this one; advance and keep looping.
set Index = Index + 1
else:
# Reached the end without a match.
ShowMessage("Target not found.")
break
# Adds a text_block (inside a canvas) to every player's screen.
ShowMessage(Text : string): void =
for (Player : GetPlayspace().GetPlayers()):
# GetPlayerUI is <decides>; bind it with [] in a failure context.
if (PlayerUI := GetPlayerUI[Player]):
TextWidget := text_block{DefaultText := StringToMessage(Text)}
Root := canvas{Slots := array{
canvas_slot{Widget := TextWidget}
}}
PlayerUI.AddWidget(Root)
# Helper to build a message from a string via interpolation.
StringToMessage<localizes>(Value : string): message = "{Value}"
Try it yourself
- Place a Button device in your level and assign it to
TriggerButton. - Build the Verse code and press the button in a playtest — you should see "Found BananaOfTheGods!" appear on screen.
- Change
Targetto"Steak"(a value not in the list) and rebuild — now you'll see "Target not found." instead, because the loop reaches the end and takes theelsebranch. - Add a
Print("Checking {ItemName}")line before the=comparison to watch the loop stop early once it hits the match.
Recap
loop:runs forever until youbreakorreturn.breakexits the nearestloopimmediately — perfect for early exit on a match.- Array indexing (
Arr[i]) andGetPlayerUI[Player]are fallible: bind them in anif, and use the failure to detect "end of list" or "no UI". - Mutate the index with
set Index = Index + 1and compare strings with=. - Real UI happened here: we built a
text_blockin acanvasand calledAddWidgeton theplayer_ui.
Check your understanding
Test yourself with an interactive quiz and track your progress + earn XP — free for members.
Turn this into a guided course
Add Controlled Repetition with Loop and Break in Verse 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.
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.