Local and Global Variables — GDScript
Skill codes on this page
| Code | Level | Skill |
|---|---|---|
| C621 | 3–4 | local variables |
| C622 | 3–4 | constants |
| C628 | 3–4 | uses data types for local variables |
| C631 | 5–6 | global variables |
| C634 | 5–6 | uses appropriate data types for global variables |
Definition. A variable stores and labels data to be referenced and manipulated in a program. Its scope is the part of the program that can see it.
| Where it is written | Who can see it | |
|---|---|---|
| Local (C621) | Inside a function | Only that function |
| Global (C631) | Outside every function, at script level | Every function in the script |
Quick check: is the line indented inside a func? Local.
C621 — Local variables
Definition. Variables defined inside a function that can only be accessed by that function.
func calculate_fare() -> void: # C621 — local variable: only calculate_fare() can see it var fare: float = 5.30 balance -= fare
Parameters and loop variables are local too — in connect-go-dots/grid.gd, the parameter photo: Texture2D and the loop variable button exist only inside their function.
C631 — Global variables
Definition. Variables defined outside any function that can be accessed by all functions throughout the source code.
At the script level:
extends Control # C631 — global variables: every func in this script can use them var balance: float = 20.00 var is_concession: bool = false
Or using class_name with static for access from other scripts:
class_name GlobalVars static var g_var = 20 # C631 — global via class_name
An autoload singleton is the Godot-idiomatic project-wide global — if you use one, name it as your C631 evidence in validation.
In connect-go-dots — the autoload in action (shared.gd, registered as Shared):
# connect-go-dots — shared.gd (autoload "Shared") extends Node ## Handed from one screen to the next. Lives on root, so a scene change ## cannot touch it. var photo: Texture2D # C631 — global: both screens use it
Shared.photo = photo # C631: grid.gd — one screen writes it %Photo.texture = Shared.photo # C631: detail.gd — the other reads it
The ## comment is model internal documentation: it says why the variable lives where it does.
C622 — Constants
Definition. A fixed value that, once defined, cannot be altered during the execution of a program.
# C622 — constant: the daily cap never changes while the program runs const DAILY_CAP: float = 9.60
Earns the tick: const used for a value that genuinely never changes, named in CAPITALS. Showing Godot's error when you try to assign to it is a neat validation demo.
C628 and C634 — choosing the right data type
C621/C631 is that the variable exists. C628/C634 is that its data type suits the data — C628 for locals, C634 for globals.
extends Control var trips_today: int = 0 # C634 — int: you cannot take half a trip var balance: float = 20.00 # C634 — float: money needs decimal places var is_concession: bool = false # C634 — bool: only two possible answers var card_id: String = "" # C634 — String: digits never used in a calculation func charge(zone: int) -> void: var fare: float = FARE_TABLE[zone] # C628 — float: fares have cents balance -= fare
From tile-matching-game (deck.gd):
# tile-matching-game — deck.gd var key: String = card["pair"] # C628 — String: the pair label from the CSV
State the data type. var trips_today = 0 cannot earn C634 because no data type has been given for the marker to assess. Always write : int, : float, : bool or : String.
var trips_today := 0 is acceptable — GDScript works out the type for you — but name it in your comment so the marker can see it: # C634 — int: you cannot take half a trip.
Types go beyond int/float/bool/String: in connect-go-dots the global is var photo: Texture2D — same standard, # C634 — Texture2D: the value is an image.
The why in these comments doubles as your reasoning evidence (C629/C638) — see Data Types.
Check Your Understanding
var speed := 5at script level — which code does it earn as written, and what more would C634 need?
Answer
C631 (a global exists). C634 needs the type visible — write : int, or name it in the comment — plus the why.
- Are function parameters local or global?
Answer
Local (C621): they exist only inside their function.
- What three things make a C622 constant?
Answer
const, a CAPITALS name, and a value that genuinely never changes while the program runs.
