Skip to content

P1.2 — Values & Variables

What you'll learn

  • What a value is — a single piece of data the program works with.
  • What a variable is — a named store that holds a value and can be changed.
  • How to create a variable with var and put a value in it with = (assignment).
  • How to change a variable later, and how to use one inside print.

How it applies

  • A variable lets the program remember. Without somewhere to store it, a value the program computed is gone the instant the line finishes. A variable holds onto it so later lines can use it — a score, a name, a count.
  • Change it in one place, not everywhere. When a value is kept in a variable and used by name, changing the variable updates every use at once. Repeating the raw value instead means fixing it in many places later, and missing one.
  • A misspelled name is a different store. Names are how the program finds a variable; score and scrore are two unrelated names. A typo silently makes (or fails to find) the wrong one — a common first-week bug.
  • = is "put into," not "is equal to." Reading = as the math "equals" is the single most common beginner misunderstanding, and it makes later code (especially decisions) confusing until it is corrected.

Concepts

Values

A value is one piece of data — the actual stuff a program works with. 7 is a value. "Vex" is a value (text, in quotes). true is a value. On their own, values are fleeting: a line like 7 + 3 computes the value 10, but if nothing stores it, the 10 vanishes and the next line cannot use it.

Variables: named stores

A variable is a named container that holds a value so you can use it later and change it. You create one with the keyword var, give it a name, and put a value in it:

var score := 0

This reads as: "make a variable called score, and put the value 0 in it." The := is how you create a variable and give it a starting value in one step. From now on, writing score anywhere means "the value currently stored in score."

var player_name := "Vex"
print(player_name)        # shows: Vex

Notice print(player_name) has no quotes around player_name. With quotes, print("player_name") would show the literal text player_name; without quotes, it shows the value stored in the variable, Vex. Quotes mean "this exact text"; no quotes means "look up this name."

Changing a variable

A variable's whole point is that it can change. After it exists, you assign a new value with = (no var, no colon — var is only for creating it the first time):

var score := 0      # create it, starts at 0
score = 10          # change it: now 0 is replaced by 10
score = 25          # change it again: now 25
print(score)        # shows: 25

The = here means "put the value on the right into the variable on the left." It is an instruction, not a statement of fact. This matters: a line like score = score + 10 is perfectly sensible as an instruction — "take the current score, add 10, and put the result back into score" — even though it would be nonsense as a math equation. Read = as "becomes," not "equals."

Example

Watch a value accumulate, step by step:

var score := 0       # score is 0
score = score + 10   # right side is 0 + 10 = 10; that goes back into score → 10
score = score + 5    # right side is 10 + 5 = 15; back into score → 15
print(score)         # shows: 15

Each line replaces the old value with a new one computed from it. If you read score = score + 10 as an equation it looks impossible (a number cannot equal itself plus ten); read as an instruction — "score becomes score plus ten" — it is exactly right. This is the heart of why = is assignment, not equality.

Naming variables

A variable name should say what it holds: score, player_name, is_alive. The convention in GDScript is lowercase words joined by underscores (max_health, not maxHealth or MaxHealth). Names are exact and case-sensitive: score and Score would be two different variables, and scrore is a typo the program will treat as an entirely separate (probably undefined) name.

Walkthrough

Use the script/run setup from P1.1; put the code in _ready().

  1. Create a variable var score := 0 and print(score). Run; confirm it shows 0.
  2. Below that, write score = 100 and print(score) again. Run; confirm the second print shows 100 — the variable changed.
  3. Write score = score + 50 and print once more. Predict the number before you run (150), then run and check. This is the "becomes" idea in action.
  4. Make a text variable var player_name := "Vex" and print(player_name). Then deliberately print it with quotes — print("player_name") — and compare: one shows Vex, the other shows the literal word player_name. That is the quotes-versus-name distinction.

Optional sanity check

Misspell a variable name on purpose — print scrore instead of score — and run. The editor complains that it does not know that name. That is names being exact: the program can only find a variable by its precise spelling. Fix the spelling and it works.

Self-check quiz

Q1 — What is a variable?

A. A fixed value that never changes. B. A named store that holds a value, which you can read by name and change later. C. A line that prints text. D. A type of error.

Reveal answer

B. A variable is a named container for a value; you refer to it by name and can assign it a new value over time. A describes a constant (a value that does not change). C is what print does. D is unrelated.

Q2 — score = score + 10 — why is this a valid instruction and not a contradiction?

A. It is invalid; a value cannot equal itself plus ten. B. Because = means "becomes": take the current score, add 10, and store the result back in score. C. Because GDScript ignores the left side. D. Because score is text.

Reveal answer

B. = is assignment — "put the right-hand value into the left-hand variable" — so the line computes score + 10 and stores it back in score. A reads = as math equality, the exact misconception this chapter corrects. C and D invent behavior.

Q3 — var player_name := \"Vex\". What does print(player_name) show, versus print(\"player_name\")?

A. Both show Vex. B. print(player_name) shows Vex; print(\"player_name\") shows the literal text player_name. C. Both show player_name. D. print(player_name) causes an error.

Reveal answer

B. Without quotes, player_name is a name the program looks up, giving the stored value Vex; with quotes, it is literal text and is shown as-is. A and C miss the quotes-versus-name distinction. D is wrong — reading a variable you created is fine.

Integration question

Q4 — open

A program needs to track a player's gold. It starts at 0, the player earns 30, then spends 20. Using a variable and the idea that = means "becomes," write the sequence of instructions in plain terms (you may sketch the code) and explain what value gold holds after each line — then say why using a variable here is better than just printing the numbers 0, 30, 10 directly.

Reveal expected answer

Create the variable and update it: var gold := 0 (gold holds 0); gold = gold + 30 (right side is 0 + 30 = 30, stored back → gold holds 30); gold = gold - 20 (30 − 20 = 10, stored back → gold holds 10). Each assignment replaces gold's value with one computed from its current value, because = means "becomes," not "equals." Using a variable is better than printing the literals 0, 30, 10 because the variable remembers the running total: later code can read gold to check whether the player can afford something, display it, or keep adjusting it — and if the earning or spending amounts change, the variable recomputes the correct total automatically, whereas hard-coded numbers would all have to be found and fixed by hand. The variable turns a one-off display into a value the rest of the program can use and trust.