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 | # Instructions and Operators — GDScript |
||||||
| 3 | ||||||||
| 4 | ||||||||
| 5 | **Skill codes on this page** |
|||||||
| 6 | ||||||||
| 7 | | Code | Level | Skill | |
|||||||
| 8 | |---|---|---| |
|||||||
| 9 | | C611 | 1–2 | instructions | |
|||||||
| 10 | | C612 | 1–2 | arithmetic operators | |
|||||||
| 11 | | C623 | 3–4 | logical operators | |
|||||||
| 12 | | C624 | 3–4 | conditional operators | |
|||||||
| 13 | ||||||||
| 14 | ## C611 — Instructions |
|||||||
| 15 | ||||||||
| 16 | **Definition.** A unit of code that can be executed by a compiler or interpreter. |
|||||||
| 17 | ||||||||
| 18 | Label a short piece of code that *does something*: takes input, calculates, produces output. |
|||||||
| 19 | ||||||||
| 20 | ```gdscript |
|||||||
| 21 | trips_today += 1 # C611 — instruction: updates the trip counter |
|||||||
| 22 | ``` |
|||||||
| 23 | ||||||||
| 24 | From a studied project: |
|||||||
| 25 | ||||||||
| 26 | ```gdscript |
|||||||
| 27 | # connect-go-dots — grid.gd |
|||||||
| 28 | Shared.photo = photo # C611 — instruction: stores the chosen photo |
|||||||
| 29 | ``` |
|||||||
| 30 | ||||||||
| 31 | **Earns the tick:** any labelled statement that executes and has an effect. This is the floor of the ladder — every student should have it. |
|||||||
| 32 | ||||||||
| 33 | ## C612 — Arithmetic operators |
|||||||
| 34 | ||||||||
| 35 | **Definition.** A symbol that performs basic mathematical operations such as addition, subtraction, multiplication and division. |
|||||||
| 36 | ||||||||
| 37 | GDScript: `+` `-` `*` `/` `%` (remainder) and `**` (power). |
|||||||
| 38 | ||||||||
| 39 | ```gdscript |
|||||||
| 40 | # C612 — arithmetic operators |
|||||||
| 41 | var total: float = fare * trips + 0.50 # * multiply, + add |
|||||||
| 42 | var change: float = payment - total # - subtract |
|||||||
| 43 | var per_trip: float = total / trips # / divide |
|||||||
| 44 | var minutes_left: int = seconds % 60 # % remainder |
|||||||
| 45 | ``` |
|||||||
| 46 | ||||||||
| 47 | **In tile-matching-game** (`game.gd`) — working out the board: |
|||||||
| 48 | ||||||||
| 49 | ```gdscript |
|||||||
| 50 | # tile-matching-game — game.gd |
|||||||
| 51 | var total_cards := rows * columns # C612 — * multiply |
|||||||
| 52 | if total_cards % 2 != 0: # C612 — % remainder: an odd board cannot hold pairs |
|||||||
| 53 | return |
|||||||
| 54 | pairs_to_find = total_cards / 2 # C612 — / divide (int ÷ int stays int) |
|||||||
| 55 | ``` |
|||||||
| 56 | ||||||||
| 57 | **Watch:** integer division truncates — `7 / 2` is `3`, but `7.0 / 2` is `3.5`. Being able to say why is C629 territory. |
|||||||
| 58 | ||||||||
| 59 | ## C623 — Logical operators |
|||||||
| 60 | ||||||||
| 61 | **Definition.** A Boolean operator used to combine expressions, such as AND, OR. |
|||||||
| 62 | ||||||||
| 63 | GDScript uses the words `and`, `or`, `not` (the symbols `&&`, `||`, `!` also work — prefer the words). |
|||||||
| 64 | ||||||||
| 65 | ```gdscript |
|||||||
| 66 | # C623 — logical operators |
|||||||
| 67 | if is_concession and zone <= 2: # and: both must be true |
|||||||
| 68 | fare *= 0.5 |
|||||||
| 69 | ||||||||
| 70 | if not card_registered or balance < 0.0: # not, or |
|||||||
| 71 | show_warning() |
|||||||
| 72 | ``` |
|||||||
| 73 | ||||||||
| 74 | **In tile-matching-game** (`game.gd`): |
|||||||
| 75 | ||||||||
| 76 | ```gdscript |
|||||||
| 77 | # tile-matching-game — game.gd |
|||||||
| 78 | if is_busy or card.is_face_up or card.is_matched: # C623 — or: any reason to ignore the click |
|||||||
| 79 | return |
|||||||
| 80 | ||||||||
| 81 | if pairs_to_find > 0 and score >= pairs_to_find: # C623 — and: both needed to win |
|||||||
| 82 | message_label.text = "All pairs found in %d turns!" % turns |
|||||||
| 83 | ``` |
|||||||
| 84 | ||||||||
| 85 | **Earns the tick:** a condition that *combines* expressions. A bare `if balance >= fare:` is C624 only — no logical operator in sight. |
|||||||
| 86 | ||||||||
| 87 | ## C624 — Conditional operators |
|||||||
| 88 | ||||||||
| 89 | **Definition.** A symbol used to compare two values within a condition: equals `==`, not equals `!=`, greater than `>`, less than `<`, greater than or equal `>=`, less than or equal `<=`. Used inside selection and iteration conditions to decide whether a branch runs or a loop continues. |
|||||||
| 90 | ||||||||
| 91 | ```gdscript |
|||||||
| 92 | # C624 — conditional operators |
|||||||
| 93 | if balance >= fare: # >= at least enough money |
|||||||
| 94 | touch_on() |
|||||||
| 95 | elif balance == 0.0: # == exactly empty |
|||||||
| 96 | show_topup_prompt() |
|||||||
| 97 | ``` |
|||||||
| 98 | ||||||||
| 99 | **In tile-matching-game** (`game.gd`) — the heart of the game is one comparison: |
|||||||
| 100 | ||||||||
| 101 | ```gdscript |
|||||||
| 102 | # tile-matching-game — game.gd |
|||||||
| 103 | if first_card.pair_id == second_card.pair_id: # C624 — ==: the cards match |
|||||||
| 104 | _handle_match() |
|||||||
| 105 | ``` |
|||||||
| 106 | ||||||||
| 107 | **Not to be confused with** the ternary conditional *expression* (`0.5 if is_concession else 1.0` in GDScript) — that is shorthand for selection, not a comparison. |
|||||||
| 108 | ||||||||
| 109 | **Watch:** `=` assigns, `==` compares. GDScript will refuse `if balance = 0.0:` — read the error, don't fight it. |
|||||||
| 110 | ||||||||
| 111 | ## Check Your Understanding |
|||||||
| 112 | ||||||||
| 113 | 1. In `if balance >= fare and is_concession:` — which two operator codes could you label, and on which symbols? |
|||||||
| 114 | ||||||||
| 115 | >| ### Answer |
|||||||
| 116 | >| C624 on `>=` (conditional operator), C623 on `and` (logical operator). |
|||||||
| 117 | ||||||||
| 118 | 2. Why will `if balance = 0.0:` not run? |
|||||||
| 119 | ||||||||
| 120 | >| ### Answer |
|||||||
| 121 | >| `=` assigns; comparing needs `==`. Godot reports a parse error. |
|||||||
| 122 | ||||||||
| 123 | 3. In `total_cards % 2 != 0` — what does `%` do, and which code is it? |
|||||||
| 124 | ||||||||
| 125 | >| ### Answer |
|||||||
| 126 | >| `%` gives the remainder after division — C612 (arithmetic). The `!=` beside it is C624. |
|||||||
