Blame
|
1 | <!-- Generated from applied-computing-au vic/unit3-4/sat/C06-2026/C06-Reference-Godot by port-reference-godot-to-wiki.py — do not hand-edit; re-run the port. --> |
||||||
|
2 | # Local and Global Variables — GDScript |
||||||
| 3 | ||||||||
| 4 | ||||||||
| 5 | **Skill codes on this page** |
|||||||
| 6 | ||||||||
| 7 | | Code | Level | Skill | |
|||||||
| 8 | |---|---|---| |
|||||||
| 9 | | C621 | 3–4 | local variables | |
|||||||
| 10 | | C622 | 3–4 | constants | |
|||||||
| 11 | | C628 | 3–4 | uses data types for local variables | |
|||||||
| 12 | | C631 | 5–6 | global variables | |
|||||||
| 13 | | C634 | 5–6 | uses appropriate data types for global variables | |
|||||||
| 14 | ||||||||
| 15 | **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. |
|||||||
| 16 | ||||||||
| 17 | | | Where it is written | Who can see it | |
|||||||
| 18 | |---|---|---| |
|||||||
| 19 | | **Local (C621)** | Inside a function | Only that function | |
|||||||
| 20 | | **Global (C631)** | Outside every function, at script level | Every function in the script | |
|||||||
| 21 | ||||||||
| 22 | **Quick check:** is the line indented inside a `func`? Local. |
|||||||
| 23 | ||||||||
| 24 | ## C621 — Local variables |
|||||||
| 25 | ||||||||
| 26 | **Definition.** Variables defined inside a function that can only be accessed by that function. |
|||||||
| 27 | ||||||||
| 28 | ```gdscript |
|||||||
| 29 | func calculate_fare() -> void: |
|||||||
| 30 | # C621 — local variable: only calculate_fare() can see it |
|||||||
| 31 | var fare: float = 5.30 |
|||||||
| 32 | balance -= fare |
|||||||
| 33 | ``` |
|||||||
| 34 | ||||||||
| 35 | 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. |
|||||||
| 36 | ||||||||
| 37 | ## C631 — Global variables |
|||||||
| 38 | ||||||||
| 39 | **Definition.** Variables defined outside any function that can be accessed by all functions throughout the source code. |
|||||||
| 40 | ||||||||
| 41 | At the script level: |
|||||||
| 42 | ||||||||
| 43 | ```gdscript |
|||||||
| 44 | extends Control |
|||||||
| 45 | ||||||||
| 46 | # C631 — global variables: every func in this script can use them |
|||||||
| 47 | var balance: float = 20.00 |
|||||||
| 48 | var is_concession: bool = false |
|||||||
| 49 | ``` |
|||||||
| 50 | ||||||||
| 51 | Or using `class_name` with `static` for access from other scripts: |
|||||||
| 52 | ||||||||
| 53 | ```gdscript |
|||||||
| 54 | class_name GlobalVars |
|||||||
| 55 | ||||||||
| 56 | static var g_var = 20 # C631 — global via class_name |
|||||||
| 57 | ``` |
|||||||
| 58 | ||||||||
| 59 | An **autoload singleton** is the Godot-idiomatic project-wide global — if you use one, name it as your C631 evidence in validation. |
|||||||
| 60 | ||||||||
| 61 | **In connect-go-dots** — the autoload in action (`shared.gd`, registered as `Shared`): |
|||||||
| 62 | ||||||||
| 63 | ```gdscript |
|||||||
| 64 | # connect-go-dots — shared.gd (autoload "Shared") |
|||||||
| 65 | extends Node |
|||||||
| 66 | ||||||||
| 67 | ## Handed from one screen to the next. Lives on root, so a scene change |
|||||||
| 68 | ## cannot touch it. |
|||||||
| 69 | var photo: Texture2D # C631 — global: both screens use it |
|||||||
| 70 | ``` |
|||||||
| 71 | ||||||||
| 72 | ```gdscript |
|||||||
| 73 | Shared.photo = photo # C631: grid.gd — one screen writes it |
|||||||
| 74 | %Photo.texture = Shared.photo # C631: detail.gd — the other reads it |
|||||||
| 75 | ``` |
|||||||
| 76 | ||||||||
| 77 | The `##` comment is model internal documentation: it says *why* the variable lives where it does. |
|||||||
| 78 | ||||||||
| 79 | ## C622 — Constants |
|||||||
| 80 | ||||||||
| 81 | **Definition.** A fixed value that, once defined, cannot be altered during the execution of a program. |
|||||||
| 82 | ||||||||
| 83 | ```gdscript |
|||||||
| 84 | # C622 — constant: the daily cap never changes while the program runs |
|||||||
| 85 | const DAILY_CAP: float = 9.60 |
|||||||
| 86 | ``` |
|||||||
| 87 | ||||||||
| 88 | **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. |
|||||||
| 89 | ||||||||
| 90 | ## C628 and C634 — choosing the right data type |
|||||||
| 91 | ||||||||
| 92 | C621/C631 is that the variable **exists**. C628/C634 is that its data type **suits the data** — C628 for locals, C634 for globals. |
|||||||
| 93 | ||||||||
| 94 | ```gdscript |
|||||||
| 95 | extends Control |
|||||||
| 96 | ||||||||
| 97 | var trips_today: int = 0 # C634 — int: you cannot take half a trip |
|||||||
| 98 | var balance: float = 20.00 # C634 — float: money needs decimal places |
|||||||
| 99 | var is_concession: bool = false # C634 — bool: only two possible answers |
|||||||
| 100 | var card_id: String = "" # C634 — String: digits never used in a calculation |
|||||||
| 101 | ||||||||
| 102 | func charge(zone: int) -> void: |
|||||||
| 103 | var fare: float = FARE_TABLE[zone] # C628 — float: fares have cents |
|||||||
| 104 | balance -= fare |
|||||||
| 105 | ``` |
|||||||
| 106 | ||||||||
| 107 | From tile-matching-game (`deck.gd`): |
|||||||
| 108 | ||||||||
| 109 | ```gdscript |
|||||||
| 110 | # tile-matching-game — deck.gd |
|||||||
| 111 | var key: String = card["pair"] # C628 — String: the pair label from the CSV |
|||||||
| 112 | ``` |
|||||||
| 113 | ||||||||
| 114 | **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`. |
|||||||
| 115 | ||||||||
| 116 | `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`. |
|||||||
| 117 | ||||||||
| 118 | 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`. |
|||||||
| 119 | ||||||||
| 120 | The **why** in these comments doubles as your reasoning evidence (C629/C638) — see [Data Types](/sd/C06/Data%20Types). |
|||||||
| 121 | ||||||||
| 122 | ## Check Your Understanding |
|||||||
| 123 | ||||||||
| 124 | 1. `var speed := 5` at script level — which code does it earn as written, and what more would C634 need? |
|||||||
| 125 | ||||||||
| 126 | >| ### Answer |
|||||||
| 127 | >| C631 (a global exists). C634 needs the type visible — write `: int`, or name it in the comment — plus the *why*. |
|||||||
| 128 | ||||||||
| 129 | 2. Are function parameters local or global? |
|||||||
| 130 | ||||||||
| 131 | >| ### Answer |
|||||||
| 132 | >| Local (C621): they exist only inside their function. |
|||||||
| 133 | ||||||||
| 134 | 3. What three things make a C622 constant? |
|||||||
| 135 | ||||||||
| 136 | >| ### Answer |
|||||||
| 137 | >| `const`, a CAPITALS name, and a value that genuinely never changes while the program runs. |
|||||||
