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
  • Greeting and MaxPlayers are the names you chose.
  • := means "define this name as this value."
  • Verse figures out the kind of value automatically: "..." is text, 4 is 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 Greeting and MaxPlayers use PascalCase — capitalize each word, no spaces. (Remember, types and devices use snake_case like hello_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 + 5 is computed first, then stored back into Score.

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: Print wants text. Printing the number Score directly needs one more trick — string interpolation — which is the very next lesson. For now, build it and confirm Welcome! 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 labelThe value changes: a score, a timer, a count
You want Verse to stop accidental changesYou'll update it with set as the game runs

Official reference:

Got Welcome! printing from a constant? Mark complete — next we'll meet the different types of value and finally print numbers.

Check your understanding

  1. Multiple choice1 / 4
    Which line creates a value you can change later?
  2. Spot the bug2 / 4
    This fails to build. Why?
    MaxPlayers := 4
    set MaxPlayers = 8
  3. Predict the output3 / 4
    What does this print?
    var Score : int = 10
    set Score = Score + 5
    Print("{Score}")
  4. Fill in the blank4 / 4
    Which keyword do you use to change the value of an existing variable?