Instructions and Operators — GDScript

Skill codes on this page

Code Level Skill
C611 1–2 instructions
C612 1–2 arithmetic operators
C623 3–4 logical operators
C624 3–4 conditional operators

C611 — Instructions

Definition. A unit of code that can be executed by a compiler or interpreter.

Label a short piece of code that does something: takes input, calculates, produces output.

trips_today += 1  # C611 — instruction: updates the trip counter

From a studied project:

# connect-go-dots — grid.gd
Shared.photo = photo   # C611 — instruction: stores the chosen photo

Earns the tick: any labelled statement that executes and has an effect. This is the floor of the ladder — every student should have it.

C612 — Arithmetic operators

Definition. A symbol that performs basic mathematical operations such as addition, subtraction, multiplication and division.

GDScript: + - * / % (remainder) and ** (power).

# C612 — arithmetic operators
var total: float = fare * trips + 0.50   # * multiply, + add
var change: float = payment - total      # - subtract
var per_trip: float = total / trips      # / divide
var minutes_left: int = seconds % 60     # % remainder

In tile-matching-game (game.gd) — working out the board:

# tile-matching-game — game.gd
var total_cards := rows * columns   # C612 — * multiply
if total_cards % 2 != 0:            # C612 — % remainder: an odd board cannot hold pairs
    return
pairs_to_find = total_cards / 2     # C612 — / divide (int ÷ int stays int)

Watch: integer division truncates — 7 / 2 is 3, but 7.0 / 2 is 3.5. Being able to say why is C629 territory.

C623 — Logical operators

Definition. A Boolean operator used to combine expressions, such as AND, OR.

GDScript uses the words and, or, not (the symbols &&, ||, ! also work — prefer the words).

# C623 — logical operators
if is_concession and zone <= 2:      # and: both must be true
    fare *= 0.5

if not card_registered or balance < 0.0:   # not, or
    show_warning()

In tile-matching-game (game.gd):

# tile-matching-game — game.gd
if is_busy or card.is_face_up or card.is_matched:   # C623 — or: any reason to ignore the click
    return

if pairs_to_find > 0 and score >= pairs_to_find:    # C623 — and: both needed to win
    message_label.text = "All pairs found in %d turns!" % turns

Earns the tick: a condition that combines expressions. A bare if balance >= fare: is C624 only — no logical operator in sight.

C624 — Conditional operators

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.

# C624 — conditional operators
if balance >= fare:      # >= at least enough money
    touch_on()
elif balance == 0.0:     # == exactly empty
    show_topup_prompt()

In tile-matching-game (game.gd) — the heart of the game is one comparison:

# tile-matching-game — game.gd
if first_card.pair_id == second_card.pair_id:   # C624 — ==: the cards match
    _handle_match()

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.

Watch: = assigns, == compares. GDScript will refuse if balance = 0.0: — read the error, don't fight it.

Check Your Understanding

  1. In if balance >= fare and is_concession: — which two operator codes could you label, and on which symbols?
Answer

C624 on >= (conditional operator), C623 on and (logical operator).

  1. Why will if balance = 0.0: not run?
Answer

= assigns; comparing needs ==. Godot reports a parse error.

  1. In total_cards % 2 != 0 — what does % do, and which code is it?
Answer

% gives the remainder after division — C612 (arithmetic). The != beside it is C624.