Variables and Data Types in Verse
In this lesson you'll learn to
- Explain what a variable is and why programs need them
- Identify the three main data types in Verse: int, float, and string
- Declare and update variables inside a real Verse device script
- Connect variable changes to visible gameplay events on a Fortnite island
🎮 What Is a Variable?
Imagine you have a scoreboard in your Fortnite island. Every time a player scores a point, the number goes UP. That number is not stuck at one value — it changes. In Verse, we use a variable to hold a value that can change during the game.
Think of a variable like a labeled box:
- The label is the name you give it (like
Score). - The stuff inside is the value (like
0, then1, then2…). - You can swap what's inside the box anytime!
🧠 Variable = a named box that stores one piece of information, and that information can change while the game is running.
📦 Constants — The Box That Never Opens Again
Sometimes you want a value that never changes. Like the number of lives players start with — it's always 3. We call that a constant.
🧠 Constant = a named box that is sealed shut. You set it once and it stays that way forever.
In Verse, you write a constant with := and you never change it after that first line.
🗂️ Data Types — What Kind of Stuff Is in the Box?
Not all boxes hold the same kind of stuff. In Verse, every variable has a data type — that just means "what kind of information is stored here?"
Here are the three main types you'll use:
| Data Type | What It Holds | Real-Life Example |
|---|---|---|
| int | Whole numbers (no decimals) | Score: 0, 5, 100 |
| float | Numbers with decimals | Speed: 1.5, 9.8 |
| string | Words or text | Player name: "Hero" |
- int is short for integer — a fancy word for a whole number like
3or42. - float is a number with a decimal point, like
3.14or0.5. - string is text wrapped in quotes, like
"Game Over"or"Player 1".
🎯 Game tip: Your player's score is an
int. A timer that counts down by half-seconds uses afloat. A welcome message on screen is astring!
✍️ How to Write Variables in Verse
Here is the recipe for making a variable in Verse:
Name : type = StartingValue
For a variable that can change, you add the word var in front:
var Name : type = StartingValue
The := symbol means "set this right now." You'll use it to change a var later in your code.
Let's look at real examples:
var Score : int = 0 # A whole-number box that starts at zero
var TimerSeconds : float = 30.0 # A decimal-number box starting at 30
var WelcomeMessage : string = "Welcome to my island!" # A text box
Notice the # — anything after # is a comment. Comments are notes for YOU. The computer ignores them completely!
🔗 How This Connects to Your Island
In UEFN, you build a Verse device — think of it like a special prop that runs your code. When the game starts, your device wakes up and runs its OnBegin function. That's where your variables spring to life!
When a player steps on a trigger, your code can change the variable:
set Score = Score + 1— adds 1 to the score box!
You can then use that score to do things like grant items or activate devices. Your variable is the brain keeping track of everything. 🧠
Worked Example
🏗️ Building a Score Tracker Device
The scenario: A player steps on a trigger pad. Each time they do, their score goes up by 1. When the score hits 3, a message prints — they WIN! We'll use a trigger_device in UEFN and write a Verse script to track the score.
Step 1: In UEFN, create a new Verse device file. Here's the complete script:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /EpicGames.com/Temporary/Diagnostics }
# This is our custom device — it tracks a player's score
score_tracker_device := class(creative_device):
# Hook up a Trigger Device in UEFN's detail panel
@editable
TriggerPad : trigger_device = trigger_device{}
# A variable to hold the current score — starts at zero
# 'var' means this value CAN change during the game
var Score : int = 0
# A constant — the score needed to win never changes
WinScore : int = 3
# OnBegin runs automatically when the game starts
OnBegin<override>()<suspends> : void =
# Tell the trigger to call our function each time it fires
TriggerPad.TriggeredEvent.Subscribe(OnPlayerTriggered)
# This function runs every time a player steps on the trigger
OnPlayerTriggered(Agent : agent) : void =
# Add 1 to the Score box
set Score = Score + 1
# Print the new score so we can see it in the log
Print("Score is now: {Score}")
# Check if the player has reached the winning score
if (Score >= WinScore):
Print("🎉 You Win! Great job!")
🔍 Walkthrough — Line by Line
| Line | What it does |
|---|---|
using { /Fortnite.com/Devices } |
Tells Verse we want to use Fortnite devices like triggers |
score_tracker_device := class(creative_device): |
Creates our custom device |
@editable |
Makes TriggerPad appear in the UEFN editor so you can drag-and-drop a trigger onto it |
var Score : int = 0 |
Makes a changeable box called Score that holds whole numbers, starting at 0 |
WinScore : int = 3 |
A constant — no var, so it can never be changed by accident |
OnBegin<override>()<suspends> : void = |
Runs once when the game starts |
TriggerPad.TriggeredEvent.Subscribe(OnPlayerTriggered) |
Listens for the trigger and calls our function when it fires |
set Score = Score + 1 |
Opens the Score box and puts in the old value plus one |
Print("Score is now: {Score}") |
Shows the score in the Output Log — great for testing! |
if (Score >= WinScore): |
Checks if score is 3 or more |
Print("🎉 You Win!") |
Celebrates the win! |
✅ Try it: In UEFN, place a Trigger Device on your island. Assign it to
TriggerPadin the device details. Hit Launch Session and walk over the trigger three times. Watch the Output Log — you should see the score climb to 3 and then see the win message!
Try It Yourself
🛠️ Your Turn — Add a Lives Counter!
You just made a score tracker. Now let's add a lives system!
Your challenge:
- Add a new
varvariable calledLivesof typeint. Start it at 3. - Add a second
@editabletrigger calledDangerZoneof typetrigger_device. - Subscribe to
DangerZone.TriggeredEventwith a new function calledOnDangerTriggered. - Inside
OnDangerTriggered, subtract 1 fromLivesusingset Lives = Lives - 1. - Print the new lives count with
Print("Lives left: {Lives}"). - Add an
ifcheck — ifLives <= 0, print"💀 Game Over!".
In UEFN: Place a second Trigger Device somewhere dangerous on your island (like lava or a trap area). Assign it to DangerZone. Play the island and walk into the danger zone three times!
💡 Hint: Subtracting in Verse looks just like adding, but with a
-sign instead of+. Theifcheck for lives running out looks just like the win check in the example — just change the variable name and the message!
🌟 Bonus challenge: Can you add a
stringvariable calledPlayerStatusthat starts as"Alive"and changes to"Defeated"when lives hit zero?
Take on the Challenge
Pass the 4-question challenge to master this lesson and chart the quest in your journal.
Quick Quiz
4 quick questions. Pick an answer to see if you've got it.
-
1.
-
2.
-
3.
-
4.
Recap
🌟 Great Work — Here's What You Learned!
A variable is a named box that stores information that can change during your game — like a score that goes up when a player does something. A constant is a sealed box set once and never changed. Every variable has a data type: int for whole numbers, float for decimal numbers, and string for text. You used all of these inside a real Verse device that tracked a player's score on a Fortnite island — and that's genuinely awesome programming! 🎉
Sources
/docs/documentation/en-us/fortnite/basics-of-writing-code-1-basic-programming-terms-in-verse /docs/documentation/en-us/uefn/learn-the-basics-of-writing-code-in-verse /docs/documentation/en-us/fortnite/learn-the-basics-of-writing-code-in-verse© Biloxi Studios Inc. — original Verse Island content.
Turn this into a guided course
Add Variables and Data Types 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.
🧭 The Keeper's log
Quest complete? Chart your next heading from the Verse Basics expedition.