Loop and Break for Player UI Control
Tutorial beginner compiles

Loop and Break for Player UI Control

Updated beginner Code verified

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 a break or return, it never ends — an infinite loop.
  • break immediately leaves the nearest enclosing loop. 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, an if). If the index runs past the end, the if fails and we break out gracefully.
  • GetPlayerUI[Player] is <decides> (fallible), so we bind it with [] inside an if. Once we have the player_ui, AddWidget shows 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

  1. Place a Button device in your level and assign it to TriggerButton.
  2. Build the Verse code and press the button in a playtest — you should see "Found BananaOfTheGods!" appear on screen.
  3. Change Target to "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 the else branch.
  4. 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 you break or return.
  • break exits the nearest loop immediately — perfect for early exit on a match.
  • Array indexing (Arr[i]) and GetPlayerUI[Player] are fallible: bind them in an if, and use the failure to detect "end of list" or "no UI".
  • Mutate the index with set Index = Index + 1 and compare strings with =.
  • Real UI happened here: we built a text_block in a canvas and called AddWidget on the player_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.

Comments

    Sign in to vote, comment, or suggest an edit. Sign in