Dice Rolls and Surprises: Mastering the Random Number Generator
Dice Rolls and Surprises: Mastering the Random Number Generator
Do you want your game to feel alive? Sometimes you want a door to open at random. Sometimes you want a monster to appear in a random spot. This is where the Random Number Generator device comes in. It is like a digital dice roll for your island.
We will build a simple "Mystery Box" game. You will place a box. When a player touches it, the box rolls a number. That number decides what prize the player gets. It could be a sword, a shield, or a funny hat! You will learn how to make your game unpredictable and fun.
What You'll Learn
- How to find the Random Number Generator device in your Creative inventory.
- How to set a Range for the random numbers.
- How to use Volumes to link the random number to specific items.
- How to create a surprise reward system for players.
How It Works
Imagine you have a bag of marbles. Some are red, some are blue. If you reach in without looking, you get a random color. The Random Number Generator (or RNG) device does something similar. It picks a number you didn't expect.
Here is the secret sauce: Volumes.
A Volume is like an invisible bubble or a box in the game world. You can draw these boxes around your prizes. The RNG device can create its own volumes. Each volume gets a number. If the RNG rolls the number "1", it activates the first volume. If it rolls "2", it activates the second volume.
Think of it like a slot machine. You pull the lever. The wheels spin. They stop on a number. That number tells the machine which prize to drop. You don't know the result until the game plays. This makes every playthrough different!
Let's Build It
We will build a "Surprise Spinner." We will use Verse to control the logic. Don't worry if you have never written code. We will take it one step at a time.
First, place your Random Number Generator device. Then, place two Item Granters (these give items to players). Name one "Sword" and one "Shield".
Now, look at the settings for the RNG device. You will see a section for Volumes. We will use Verse to make this work smoothly.
Here is the code to make your Surprise Spinner work. Copy this into a Verse file attached to your RNG device.
# This is the main script for our Surprise Spinner
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
# This is the main class for our device
surprise_spinner := class(creative_device):
# These are the items we can give away
# We link them to the Item Granters in the editor
@editable
sword_granter : item_granter_device = item_granter_device{}
@editable
shield_granter : item_granter_device = item_granter_device{}
# This is the trigger a player walks into to spin the wheel
# We link it to a Trigger device placed in the editor
@editable
spin_trigger : trigger_device = trigger_device{}
# This function runs automatically when the game starts
OnBegin<override>()<suspends> : void =
# We subscribe to the trigger so we know when a player steps on it
spin_trigger.TriggeredEvent.Subscribe(OnSpinTriggered)
# This function runs when a player triggers the device
OnSpinTriggered(Agent : ?agent) : void =
# We roll a random integer between 1 and 2 inclusive
# 1 means Sword, 2 means Shield
# GetRandomInt returns a value in the range [Low, High]
Result : int = GetRandomInt(1, 2)
# The 'Result' is the number the RNG picked
if (Result = 1):
# If it rolled a 1, give the sword!
if (ActualAgent := Agent?):
sword_granter.GrantItem(ActualAgent)
else if (Result = 2):
# If it rolled a 2, give the shield!
if (ActualAgent := Agent?):
shield_granter.GrantItem(ActualAgent)```
### Walkthrough of the Code
1. **`surprise_spinner := class(creative_device)`**: This tells Verse that our script is attached to a device that starts when the game begins.
2. **`sword_granter` and `shield_granter`**: These are variables. A **variable** is a box that holds a value. Here, the box holds a reference to the Item Granter devices you placed in the editor.
3. **`spin_trigger`**: This variable holds a **Trigger** device. A player walks into it to start the spin. The `@editable` tag lets you connect it to a real device in the editor.
4. **`OnBegin<override>()<suspends>`**: This code runs first when the game starts. It subscribes to the trigger so the game listens for a player to step on it.
5. **`OnSpinTriggered(Agent : agent)`**: This code runs when a player hits the trigger. The `Agent` is the player who touched it.
6. **`GetRandomInt(1, 2)`**: This picks a whole number between 1 and 2. It comes from the `/Verse.org/Random` module.
7. **`if (Result = 1)`**: This checks if the result is 1. If yes, it grants the sword to the player.
8. **`else if (Result = 2)`**: This checks if the result is 2. If yes, it grants the shield to the player.
## Try It Yourself
Now it is your turn to be creative!
**Challenge:** Add a third option to your Surprise Spinner.
**Hint:**
1. Place a third **Item Granter** in the editor. Name it "Hat".
2. In the Verse code, change the `GetRandomInt` range from `(1, 2)` to `(1, 3)`.
3. Add a new `else if` block to check if the result is `3`.
4. Inside that block, activate the "Hat" granter.
Can you make it so the player has a 1 in 3 chance of getting each item? Give it a try!
## Recap
You just learned how to use the **Random Number Generator** device. You learned that it picks a random number within a set range. You also learned how to use Verse to check that number and give the player a different reward each time. This makes your game feel fresh and exciting. Remember, randomness is like a surprise party. You never know exactly when it will happen, but it is always fun!
## References
* https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-random-number-generator-devices-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite/using-random-number-generator-devices-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite-creative/hud-message-device-design-example-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite/random-sentry-fight-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite/hud-message-device-design-example-in-fortnite-creative
Verse source files
- 01-device.verse · device
Turn this into a guided course
Add using-random-number-generator-devices-in-fortnite-creative 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.
References
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.