Random Loot Rolls with GetRandomInt and Player UI
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-secureintbetweenLowandHigh(inclusive). It's marked<transacts>, so you call it with parentheses and read the returnedintdirectly — 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 asif (UI := GetPlayerUI[Player]):.player_ui.AddWidget(Widget)and acanvas— the canvas is a container that positions widgets in itsSlots, andAddWidgeton theplayer_uidraws it on screen. We callSetText(message)on ourtext_blockto 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
- Place a Button Device in your level.
- Create a new Verse device from this
random_loot_devicescript and drag it into the level, then push Verse changes. - In the device's Details panel, drag your Button into the
LootButtonfield. - 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 returnedintdirectly.GetPlayerUI[Player]is<decides>— call with[]inside anif (UI := ...)binding to handle failure.- Build UI in code with a
text_blockinside acanvas, then draw it viaplayer_ui.AddWidget(...). - Use
SetText(message)to set widget content, and a<localizes>helper to convert astringinto amessage. - Convert the event's
agentto aplayerwithif (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.