Variables and constants
Right now your device prints a fixed message. Real programs need to remember things — a score, a player's name, how many seconds are left. In Verse you do that by giving a value a name. There are two kinds of names: constants and variables.
Constants: names that never change
A constant ties a name to a value that stays put for the whole program.
Greeting := "Welcome to my island!"
MaxPlayers := 4
GreetingandMaxPlayersare the names you chose.:=means "define this name as this value."- Verse figures out the kind of value automatically:
"..."is text,4is a whole number.
Once set, a constant can't be reassigned. Try to change it and the build fails — which is a feature: it protects values that shouldn't move.
Naming rule: value names like
GreetingandMaxPlayersuse PascalCase — capitalize each word, no spaces. (Remember, types and devices use snake_case likehello_device. Two different conventions, and Verse cares.)
Variables: names that can change
When a value needs to change while the game runs, use a variable. You opt in with the word var.
var Score : int = 0
Read that as: "make a variable named Score, of type int (whole number), starting at 0."
To change it later, use set:
set Score = 10
set Score = Score + 5 # Score is now 15
- Without
set, you can't reassign — that's what keeps accidental changes from sneaking in. Score + 5is computed first, then stored back intoScore.
Two ways to declare, side by side
# Constant, type inferred from the value:
PlayerName := "Avery"
# Constant, type written out explicitly:
StartingLives : int = 3
# Variable (can change), type written out:
var CurrentLives : int = 3
:= infers the type for you. : int = states it explicitly. Both are fine; you'll see both. Variables (var) usually spell out the type.
Try it in your device
Edit your hello_device from last lesson:
OnBegin<override>()<suspends>:void=
Greeting := "Welcome!"
var Score : int = 0
set Score = Score + 100
Print(Greeting)
Print("Score is now... see the next lesson to print the number!")
Heads up:
Scoredirectly needs one more trick — string interpolation — which is the very next lesson. For now, build it and confirmWelcome!shows in the log.
Constant or variable — how to choose
Default to a constant. Only reach for var when the value genuinely needs to change over time. Fewer moving parts means fewer bugs — if something never changes, telling Verse that up front lets it catch mistakes for you.
Use a constant (:=) when… | Use a variable (var + set) when… |
|---|---|
| The value is fixed: a title, a max, a label | The value changes: a score, a timer, a count |
| You want Verse to stop accidental changes | You'll update it with set as the game runs |
Official reference:
- Constants & variables in Verse: https://dev.epicgames.com/documentation/en-us/uefn/constants-in-verse and https://dev.epicgames.com/documentation/en-us/uefn/variables-in-verse
Got Welcome! printing from a constant? Mark complete — next we'll meet the different types of value and finally print numbers.
Check your understanding
- Multiple choice1 / 4Which line creates a value you can change later?
- Spot the bug2 / 4This fails to build. Why?
MaxPlayers := 4 set MaxPlayers = 8 - Predict the output3 / 4What does this print?
var Score : int = 10 set Score = Score + 5 Print("{Score}") - Fill in the blank4 / 4Which keyword do you use to change the value of an existing variable?