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 | # Data Structures — GDScript |
||||||
| 3 | ||||||||
| 4 | ||||||||
| 5 | **Skill codes on this page** |
|||||||
| 6 | ||||||||
| 7 | | Code | Level | Skill | |
|||||||
| 8 | |---|---|---| |
|||||||
| 9 | | C635 | 5–6 | arrays | |
|||||||
| 10 | | C636 | 5–6 | records | |
|||||||
| 11 | | C638 | 5–6 | identifies, in the internal documentation, why the selected data types and data structures were used | |
|||||||
| 12 | ||||||||
| 13 | **Definition.** A *data structure* is a method of organising data to allow particular operations to be performed on it efficiently. |
|||||||
| 14 | ||||||||
| 15 | ## C635 — Arrays |
|||||||
| 16 | ||||||||
| 17 | **Definition.** A list of elements indexed by position. In most programming languages, the first element has index zero. |
|||||||
| 18 | ||||||||
| 19 | ```gdscript |
|||||||
| 20 | # C635 — Array[float]: fares need decimal places |
|||||||
| 21 | var fares: Array[float] = [5.30, 8.00, 11.20] |
|||||||
| 22 | ||||||||
| 23 | func show_fare() -> void: |
|||||||
| 24 | $ResultLabel.text = "Zone 1: $" + str(fares[0]) |
|||||||
| 25 | ``` |
|||||||
| 26 | ||||||||
| 27 | Typed (`Array[float]`) earns it. Bare `var fares = []` does not — no type for the marker to see. |
|||||||
| 28 | ||||||||
| 29 | **In tile-matching-game** (`board.gd`) — a typed array of your own class: |
|||||||
| 30 | ||||||||
| 31 | ```gdscript |
|||||||
| 32 | # tile-matching-game — board.gd |
|||||||
| 33 | var found: Array[Card] = [] # C635 — Array[Card]: only cards can get in |
|||||||
| 34 | for child in get_children(): |
|||||||
| 35 | if child is Card: |
|||||||
| 36 | found.append(child) |
|||||||
| 37 | ``` |
|||||||
| 38 | ||||||||
| 39 | **Two-dimensional array** — a grid organised into rows and columns, accessed with two indices: |
|||||||
| 40 | ||||||||
| 41 | ```gdscript |
|||||||
| 42 | # C635 — 2D array: fare by [zone][day type], 0 = weekday, 1 = weekend |
|||||||
| 43 | var fare_grid: Array = [ |
|||||||
| 44 | [5.30, 3.60], |
|||||||
| 45 | [8.00, 5.40], |
|||||||
| 46 | ] |
|||||||
| 47 | ||||||||
| 48 | var weekend_zone2: float = fare_grid[1][1] |
|||||||
| 49 | ``` |
|||||||
| 50 | ||||||||
| 51 | ## C636 — Records |
|||||||
| 52 | ||||||||
| 53 | **Definitions.** |
|||||||
| 54 | ||||||||
| 55 | - *Record:* a complete set of fields relating to an entity, such as a person. |
|||||||
| 56 | ||||||||
| 57 | - *Field:* a single data item in a record (e.g. `FamilyName`). |
|||||||
| 58 | ||||||||
| 59 | GDScript has no `record` keyword — a **Dictionary** is the everyday record, one key per field: |
|||||||
| 60 | ||||||||
| 61 | ```gdscript |
|||||||
| 62 | # C636 — record: one journey, fields of different types |
|||||||
| 63 | var journey: Dictionary = { |
|||||||
| 64 | "date": "2026-08-11", |
|||||||
| 65 | "zone": 2, |
|||||||
| 66 | "fare": 8.00, |
|||||||
| 67 | "concession": false, |
|||||||
| 68 | } |
|||||||
| 69 | ||||||||
| 70 | print(journey["fare"]) # access one field |
|||||||
| 71 | ``` |
|||||||
| 72 | ||||||||
| 73 | **In tile-matching-game** (`deck.gd`) — each CSV row becomes a record, and the deck is an array of them: |
|||||||
| 74 | ||||||||
| 75 | ```gdscript |
|||||||
| 76 | # tile-matching-game — deck.gd |
|||||||
| 77 | cards.append({ |
|||||||
| 78 | "pair": pair, # C636 — fields: which card matches which |
|||||||
| 79 | "text": row[1].strip_edges(), |
|||||||
| 80 | "image": row[2].strip_edges() if row.size() > 2 else "", |
|||||||
| 81 | }) |
|||||||
| 82 | ``` |
|||||||
| 83 | ||||||||
| 84 | An **array of records** is a table — the shape most SAT data layers need: |
|||||||
| 85 | ||||||||
| 86 | ```gdscript |
|||||||
| 87 | # C635 + C636 — a table: array of journey records |
|||||||
| 88 | var journeys: Array[Dictionary] = [] |
|||||||
| 89 | ||||||||
| 90 | func add_journey(zone: int, fare: float) -> void: |
|||||||
| 91 | journeys.append({"zone": zone, "fare": fare}) |
|||||||
| 92 | ``` |
|||||||
| 93 | ||||||||
| 94 | A **custom class** is the stronger record — typed fields the editor can check — and doubles as level 9–10 evidence (see [OOP Concepts](/sd/C06/OOP%20Concepts)). |
|||||||
| 95 | ||||||||
| 96 | **In club-ladder** (`team.gd`) — the class-as-record, real: its own doc comment says "A Team is plain data." |
|||||||
| 97 | ||||||||
| 98 | ```gdscript |
|||||||
| 99 | # club-ladder — team.gd |
|||||||
| 100 | class_name Team |
|||||||
| 101 | extends RefCounted |
|||||||
| 102 | ||||||||
| 103 | var team_name := "" # C636 — the fields of the record |
|||||||
| 104 | var wins := 0 |
|||||||
| 105 | var draws := 0 |
|||||||
| 106 | var losses := 0 |
|||||||
| 107 | ``` |
|||||||
| 108 | ||||||||
| 109 | An `Array[Team]` (`season.gd`) is then the table — the class version of the array-of-dictionaries above. |
|||||||
| 110 | ||||||||
| 111 | ## C638 — Why these types and structures |
|||||||
| 112 | ||||||||
| 113 | Level 5–6 asks you to **identify** the reason in internal documentation — one line on each structure: |
|||||||
| 114 | ||||||||
| 115 | ```gdscript |
|||||||
| 116 | # C638 — Array[Dictionary]: journeys arrive one at a time in order (array), |
|||||||
| 117 | # and each journey has mixed-type fields (record) |
|||||||
| 118 | var journeys: Array[Dictionary] = [] |
|||||||
| 119 | ``` |
|||||||
| 120 | ||||||||
| 121 | The "why" comes from how the structure behaves — see the input/storage/output table in [Data Type Characteristics](/sd/C06/Data%20Type%20Characteristics). |
|||||||
| 122 | ||||||||
| 123 | **Model documentation in the wild:** the header comment of `tile-matching-game/deck.gd` identifies the whole design — one CSV row per card face, rows sharing a `pair` value match — before any code runs. That is C638 done as a file-level comment. |
|||||||
| 124 | ||||||||
| 125 | ## Check Your Understanding |
|||||||
| 126 | ||||||||
| 127 | 1. Why does `var scores = []` fail C635? |
|||||||
| 128 | ||||||||
| 129 | >| ### Answer |
|||||||
| 130 | >| It is untyped — `Array[int]` (or a comment naming the type) is required, so the marker can assess the choice. |
|||||||
| 131 | ||||||||
| 132 | 2. Name two ways to build a record in GDScript. |
|||||||
| 133 | ||||||||
| 134 | >| ### Answer |
|||||||
| 135 | >| A Dictionary with one key per field, or a custom class with typed fields (the stronger version). |
|||||||
| 136 | ||||||||
| 137 | 3. What everyday shape is an "array of records"? |
|||||||
| 138 | ||||||||
| 139 | >| ### Answer |
|||||||
| 140 | >| A table — one record per row, like journeys or teams. |
|||||||
