Random Loot Rolls with GetRandomInt and Player UI
Tutorial intermediate compiles

Random Loot Rolls with GetRandomInt and Player UI

Updated intermediate Code verified

What you'll learn

You'll learn how to generate random integers with Verse's built-in GetRandomInt, and how to actually put that number on a player's screen. Instead of relying on a placed Text Block device, we build the widget entirely in code with a text_block, wrap it in a canvas, and add it to the player's player_ui. This is the real end-to-end pattern for dynamic loot rolls, dice, or randomized stats.

How it works

Three APIs do the heavy lifting:

  • GetRandomInt(Low, High) — from /Verse.org/Random. It returns a uniformly distributed, cryptographically-secure int between Low and High (inclusive). It's marked <transacts>, so you call it with parentheses and read the returned int directly — it is not a <decides> function.
  • GetPlayerUI[Player] — from /UnrealEngine.com/Temporary/UI. Its signature is <transacts><decides>, meaning it can fail. Because it's fallible, you call it with square brackets inside a failure context such as if (UI := GetPlayerUI[Player]):.
  • player_ui.AddWidget(Widget) and a canvas — the canvas is a container that positions widgets in its Slots, and AddWidget on the player_ui draws it on screen. We call SetText(message) on our text_block to update its content.

The key correctness detail: GetRandomInt returns a value (call with ()), while GetPlayerUI is a failable lookup (call with [] inside an if).

Let's build it

This device listens for a button press. On each press it rolls a number between 1 and 100, updates a code-built text widget, and adds it to the pressing player's UI.

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/Random }
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }

# Rolls a random loot value and shows it on the pressing player's screen.
random_loot_device := class<concrete>(creative_device):

    # Drag your placed Button Device into this field in the Details panel.
    @editable
    LootButton : button_device = button_device{}

    OnBegin<override>()<suspends>: void =
        # Device events subscribe WITHOUT parentheses on the event.
        LootButton.InteractedWithEvent.Subscribe(OnLootButtonPressed)

    # InteractedWithEvent hands us the agent who pressed the button.
    OnLootButtonPressed(Agent: agent): void =
        # Roll a value 1..100. GetRandomInt is <transacts> -> call with () and read the int.
        RandomValue : int = GetRandomInt(1, 100)

        # We need a player (not just an agent) to access the Player UI.
        if (Player := player[Agent]):
            ShowLoot(Player, RandomValue)

    ShowLoot(Player: player, Value: int): void =
        # Build the on-screen text widget entirely in code.
        LootText : text_block = text_block{}
        LootText.SetText(StringToMessage("Loot Rolled: {Value}"))

        # Wrap it in a canvas slot so it can be positioned.
        LootCanvas : canvas = canvas:
            Slots := array:
                canvas_slot:
                    Widget := LootText

        # GetPlayerUI is <transacts><decides> -> call with [] inside an if.
        if (UI := GetPlayerUI[Player]):
            UI.AddWidget(LootCanvas)
            Print("Player rolled loot value {Value}")

    # Helper to turn a string literal into the message type SetText expects.
    StringToMessage<localizes>(Text: string): message = "{Text}"

Try it yourself

  1. Place a Button Device in your level.
  2. Create a new Verse device from this random_loot_device script and drag it into the level, then push Verse changes.
  3. In the device's Details panel, drag your Button into the LootButton field.
  4. Launch a session and press the button — a "Loot Rolled: N" widget appears on screen, with a fresh random number each press.

Recap

  • GetRandomInt(Low, High) is <transacts> — call with () and use the returned int directly.
  • GetPlayerUI[Player] is <decides> — call with [] inside an if (UI := ...) binding to handle failure.
  • Build UI in code with a text_block inside a canvas, then draw it via player_ui.AddWidget(...).
  • Use SetText(message) to set widget content, and a <localizes> helper to convert a string into a message.
  • Convert the event's agent to a player with if (Player := player[Agent]): before touching 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 Implementing Random Number Generation with GetRandomInt 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