Cruise Game 14 · Match the parcel to the right station

Midnight Deck Dash · Lesson 14 of 50 · Beginner to intermediate · About 35 min

The ship has more than one receiver. Sol can pass the Night Theatre with the Sun Deck tea in hand. The station accepts the parcel only when its mark matches.

A straight teak promenade with a cream cabin wall on one side and a ship railing above the moonlit ocean on the other.
What you’ll make Deliver a parcel at the station with the same mark and show feedback for a different station.

Download the projects from the course page. In Godot’s Project Manager, choose Import, select project.godot, and use Run Project (F5). Start from checkpoints/13 when the lesson builds on prior work, or copy the checkpoints/14 folder to try this lesson on its own. Read the file names in the steps before editing.

Look up S. Use the carried mark to read STATIONS[S]. → Match the position. Compare the courier and intended dock. → Log the delivery. Append S once and clear the hands. → Draw the result. Remove S from the available pickup marks.
Look up S → Match the position → Log the delivery → Draw the result
Godot checkpoint window for Match the parcel to the right station
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson dictionary lookup, typed local variables, array membership, append, ordered rules, return, String formatting.

  1. Load the lesson checkpoint

    Extract the lesson 14 checkpoint. In Godot Project Manager choose Import, then select project.godot. Open the scene from the FileSystem dock and select its Control root. Open main.gd in the Script workspace. Press F5. Sol has empty hands and the ledger expects one delivery.

    extends Control

    Why it works The scene attaches this script to a Control node. The deck includes separate pickup and delivery logic. Use F8 to stop the running scene before editing.

  2. Look up the correct receiver

    From START take Right five times and Down once to collect S. Stop at this point and locate intended_dock in _resolve_landing. Use the STATIONS table to find the current value.

    var intended_dock: Vector2i = STATIONS[carried_mark]

    Why it works Use the mark as a key. When carried_mark is S, intended_dock is the Vector2i (5, 3), the stored Sun Deck station.

  3. Test the wrong station

    With S in hand at (5, 1), press Right and then Down three times to reach Night Theatre at (6, 4). Read the feedback and check that Carried parcel still says S.

    if courier in STATIONS.values():

    Why it works A different station is in STATIONS.values(). The wrong-station branch shows a message and returns without clearing the carried mark or adding a delivery.

  4. Deliver at the matching square

    From Night Theatre press Left and then Up. Find the branch for courier == intended_dock. Step through storing delivered_mark, adding it to delivered and clearing carried_mark.

    var delivered_mark := carried_mark
    			delivered.append(delivered_mark)
    			carried_mark = ""

    Why it works delivered_mark saves a copy of S before Sol's hands become empty. The array keeps track of finished jobs, and the saved mark still lets the status look up the station name.

  5. Keep completed parcels off the floor

    Walk Up twice from the Sun Deck to revisit the pickup at (5, 1). Find the membership check in the pickup loop and verify that the ledger still shows the first delivery only.

    if mark in delivered:
    			return

    Why it works mark in delivered checks whether S was already logged. The return stops Sol from collecting a finished parcel a second time.

  6. Verify all four states on paper

    Write start, carrying, wrong station and delivered as four rows and record the expected hands and delivered count in each. Press F5 and repeat the route to compare.

    report += "\nDelivered: %d / %d" % [delivered.size(), PICKUPS.size()]

    Why it works A trace shows which line changes which state. The deck can use several station marks while sharing one delivery rule.

Watch the values change

  1. Collect S carried_mark is S; delivered is empty
    What you see. Sol carries the parcel.
  2. Step onto T the intended dock is (5, 3) but courier is (6, 4)
    What you see. Wrong station appears; Sol keeps S.
  3. Step onto S the intended dock matches; delivered gains S; hands clear
    What you see. The ledger shows Delivered: 1 / 1.
  4. Revisit the S pickup S is already recorded in the array
    What you see. The parcel stays delivered.

Predict before you run Why save delivered_mark before clearing carried_mark?

Reveal the answer

Clearing carried_mark removes the S. The local copy preserves the key for the delivered list and the destination name used in the message.

Complete checkpoint

Night Theatre leaves S in hand. Sun Deck records exactly one delivery and clears Sol's hands.

main.gd

extends Control

# The map has the same 7 columns and 5 rows as the first deck sketch.
const DECK_ROWS: Array[String] = [
	".......",
	".##....",
	"...#...",
	"...#...",
	".......",
]
const START := Vector2i(0, 0)
const CELL_SIZE := 62
const BOARD_ORIGIN := Vector2(38, 123)

const STATIONS: Dictionary = {
	"S": Vector2i(5, 3),
	"T": Vector2i(6, 4),
}
const STATION_NAMES: Dictionary = {
	"S": "Sun Deck",
	"T": "Night Theatre",
}
const PICKUPS: Dictionary = {
	"S": Vector2i(5, 1),
}

var courier := START
var status := "Use the arrows or tap a direction."
var turns := 0
var carried_mark := ""
var delivered: Array[String] = []

var status_label: Label
var ledger_label: Label


func _ready() -> void:
	status_label = Label.new()
	status_label.position = Vector2(535, 189)
	status_label.size = Vector2(370, 68)
	status_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	status_label.add_theme_color_override("font_color", Color("#ffea9f"))
	status_label.add_theme_font_size_override("font_size", 17)
	add_child(status_label)

	ledger_label = Label.new()
	ledger_label.position = Vector2(535, 278)
	ledger_label.size = Vector2(355, 105)
	ledger_label.add_theme_color_override("font_color", Color("#e3f5f5"))
	ledger_label.add_theme_font_size_override("font_size", 16)
	add_child(ledger_label)

	_connect_direction("Up", Vector2(675, 375), Vector2i.UP)
	_connect_direction("Left", Vector2(583, 430), Vector2i.LEFT)
	_connect_direction("Down", Vector2(675, 430), Vector2i.DOWN)
	_connect_direction("Right", Vector2(767, 430), Vector2i.RIGHT)
	_refresh()


func _make_button(caption: String, pixel: Vector2, dimensions: Vector2) -> Button:
	var button := Button.new()
	button.text = caption
	button.position = pixel
	button.size = dimensions
	button.add_theme_font_size_override("font_size", 16)
	add_child(button)
	return button


func _connect_direction(caption: String, pixel: Vector2, delta: Vector2i) -> void:
	var button := _make_button(caption, pixel, Vector2(84, 48))
	button.pressed.connect(_try_move.bind(delta))


func _draw() -> void:
	draw_rect(Rect2(Vector2.ZERO, size), Color("#081f31"))
	draw_rect(Rect2(24, 96, 469, 373), Color("#183b4d"))
	draw_rect(Rect2(513, 96, 421, 405), Color("#14374d"))
	var font := ThemeDB.fallback_font
	draw_string(font, Vector2(37, 44), "MIDNIGHT DECK DASH", HORIZONTAL_ALIGNMENT_LEFT, -1, 30, Color("#e9f0f2"))
	draw_string(font, Vector2(38, 73), "Cruise school  /  Promenade delivery shift", HORIZONTAL_ALIGNMENT_LEFT, -1, 17, Color("#adced5"))
	draw_string(font, Vector2(535, 119), "SHIPBOARD DISPATCH", HORIZONTAL_ALIGNMENT_LEFT, -1, 16, Color("#8edcd6"))
	for y in range(DECK_ROWS.size()):
		for x in range(DECK_ROWS[y].length()):
			var cell := Vector2i(x, y)
			var pixel := BOARD_ORIGIN + Vector2(cell) * CELL_SIZE
			var blocked := DECK_ROWS[y][x] == "#"
			var colour := Color("#22424a") if blocked else Color("#4a6c61")
			draw_rect(Rect2(pixel, Vector2(59, 59)), colour)
			if blocked:
				draw_line(pixel + Vector2(12, 12), pixel + Vector2(47, 47), Color("#688d90"), 4.0)
				draw_line(pixel + Vector2(12, 47), pixel + Vector2(47, 12), Color("#688d90"), 4.0)

	for raw_mark in STATIONS.keys():
		var mark := str(raw_mark)
		var station: Vector2i = STATIONS[mark]
		var dock_pixel := BOARD_ORIGIN + Vector2(station) * CELL_SIZE
		_draw_marker(font, dock_pixel, mark, true)
	for raw_mark in PICKUPS.keys():
		var mark := str(raw_mark)
		if mark == carried_mark:
			continue

		if mark in delivered:
			continue

		var parcel_cell: Vector2i = PICKUPS[mark]
		var parcel_pixel := BOARD_ORIGIN + Vector2(parcel_cell) * CELL_SIZE
		_draw_marker(font, parcel_pixel, mark, false)

	var courier_pixel := BOARD_ORIGIN + Vector2(courier) * CELL_SIZE
	var courier_center := courier_pixel + Vector2(29, 29)
	draw_circle(courier_center, 20.0, Color("#122734"))
	draw_circle(courier_center, 16.0, Color("#fff0b5"))
	draw_string(font, courier_pixel + Vector2(22, 35), "X", HORIZONTAL_ALIGNMENT_LEFT, -1, 18, Color("#10242f"))


func _draw_marker(font: Font, pixel: Vector2, mark: String, is_station: bool) -> void:
	var centre := pixel + Vector2(29, 29)
	var colour := Color("#8be2d1") if mark == "S" else Color("#f2ad98")
	if is_station:
		draw_rect(Rect2(pixel + Vector2(7, 7), Vector2(44, 44)), colour, false, 3.0)
	else:
		draw_circle(centre, 19.0, colour)
	var ink := colour if is_station else Color("#10242f")
	draw_string(font, pixel + Vector2(23, 35), mark, HORIZONTAL_ALIGNMENT_LEFT, -1, 18, ink)


func _inside(cell: Vector2i) -> bool:
	if cell.y < 0 or cell.y >= DECK_ROWS.size():
		return false
	return cell.x >= 0 and cell.x < DECK_ROWS[cell.y].length()


func _tile_at(cell: Vector2i) -> String:
	return DECK_ROWS[cell.y][cell.x]


func _try_move(delta: Vector2i) -> void:

	var candidate := courier + delta
	if not _inside(candidate):
		status = "That is outside the deck."
		_refresh()
		return
	if _tile_at(candidate) == "#":
		status = "A bulkhead blocks that square."
		_refresh()
		return

	courier = candidate

	turns += 1

	status = "Moved to column %d, row %d." % [courier.x + 1, courier.y + 1]

	_resolve_landing()

	_refresh()


func _resolve_landing() -> void:

	if carried_mark != "":
		var intended_dock: Vector2i = STATIONS[carried_mark]
		if courier == intended_dock:
			var delivered_mark := carried_mark
			delivered.append(delivered_mark)
			carried_mark = ""
			status = "%s parcel delivered." % STATION_NAMES[delivered_mark]

			return
		if courier in STATIONS.values():
			status = "Wrong station. Keep this parcel for %s." % STATION_NAMES[carried_mark]
			return

	for raw_mark in PICKUPS.keys():
		var mark := str(raw_mark)
		if courier != PICKUPS[mark]:
			continue

		if mark in delivered:
			return

		if carried_mark != "":
			status = "Hands full. Deliver %s first." % carried_mark
			return
		carried_mark = mark
		status = "Picked up %s for %s." % [mark, STATION_NAMES[mark]]
		return


func _refresh() -> void:
	status_label.text = status

	var report := "Moves: %d" % turns

	var hands := "empty" if carried_mark == "" else carried_mark
	report += "\nCarried parcel: %s" % hands

	report += "\nDelivered: %d / %d" % [delivered.size(), PICKUPS.size()]

	ledger_label.text = report
	queue_redraw()


func _input(event: InputEvent) -> void:
	if not event is InputEventKey:
		return
	var key := event as InputEventKey
	if not key.pressed or key.echo:
		return
	match key.keycode:
		KEY_UP:
			_try_move(Vector2i.UP)
		KEY_LEFT:
			_try_move(Vector2i.LEFT)
		KEY_DOWN:
			_try_move(Vector2i.DOWN)
		KEY_RIGHT:
			_try_move(Vector2i.RIGHT)

		_:
			return
	get_viewport().set_input_as_handled()

GDScript line by line

Follow the line numbers, including the indent at the start of each line. Find the code the game will run first, then look for the function that reacts when you tap or change something.

Read main.gd line by line
  1. extends ControlGive the lesson scene a Control base so it can lay out interface nodes, draw the floor plan and handle player input.
  2. # The map has the same 7 columns and 5 rows as the first deck sketch.A comment for the reader. Godot skips it when it runs the code.
  3. const DECK_ROWS: Array[String] = [Keep the deck layout as an ordered array of strings. Each string is one row. A dot is walkable; a # is a bulkhead.
  4. ".......",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  5. ".##....",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  6. "...#...",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  7. "...#...",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  8. ".......",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  9. ]Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next.
  10. const START := Vector2i(0, 0)Name the opening square. Vector2i stores a pair of whole numbers, with column first and row second.
  11. const CELL_SIZE := 62Space tile origins 62 drawing pixels apart. This is a screen measurement; Sol's position is still measured in tiles.
  12. const BOARD_ORIGIN := Vector2(38, 123)Place the top-left board tile at screen pixel (38, 123), below the course banner.
  13. const STATIONS: Dictionary = {Store each receiving square under its parcel's mark. Reuse this table for the drawing and for delivery checks.
  14. "S": Vector2i(5, 3),Put the Sun Deck receiver in column 5 and row 3 when counted from zero.
  15. "T": Vector2i(6, 4),Put the Night Theatre receiver in column 6 and row 4 when counted from zero.
  16. }Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next.
  17. const STATION_NAMES: Dictionary = {Look up a full station name using a short parcel mark, so dispatch messages can use words rather than single letters.
  18. "S": "Sun Deck",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  19. "T": "Night Theatre",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  20. }Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next.
  21. const PICKUPS: Dictionary = {Keep the original location of each parcel in a table keyed by its mark. Restart can show these locations again without rebuilding the table.
  22. "S": Vector2i(5, 1),Place the Sun Deck parcel on the square (5, 1). Sol must walk to this square to pick it up.
  23. }Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next.
  24. var courier := STARTStart Sol on the named starting square. courier is a variable because it will change when a move succeeds.
  25. var status := "Use the arrows or tap a direction."Creates a variable named status. The expression uses the text inside the quotes. Godot infers the type from the first value.
  26. var turns := 0Open the shift with zero accepted moves. Godot infers an integer from the zero.
  27. var carried_mark := ""Use an empty string to represent empty hands. A one-letter mark replaces it when Sol takes a parcel.
  28. var delivered: Array[String] = []Start with a typed list of delivered parcel marks. This becomes a history of completed jobs for the shift.
  29. var status_label: LabelName the on-screen label that will display the latest action feedback after _ready creates it.
  30. var ledger_label: LabelName the label that will display the current move count, carried parcel, deliveries and rating.
  31. func _ready() -> void:Godot calls this scene setup method after adding the Control node. Build labels and buttons here once.
  32. status_label = Label.new()Stores the new value in status_label. The expression creates a new Label object.
  33. status_label.position = Vector2(535, 189)Updates status_label.position, which controls the node's local x and y location. The expression packs x and y into one value.
  34. status_label.size = Vector2(370, 68)Updates status_label.size, which controls the control's width and height. The expression packs x and y into one value.
  35. status_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTAllow feedback such as the wrong-station message to wrap onto the next line rather than overflowing the dispatch panel.
  36. status_label.add_theme_color_override("font_color", Color("#ffea9f"))Calls status_label.add_theme_color_override with the values in its parentheses.
  37. status_label.add_theme_font_size_override("font_size", 17)Calls status_label.add_theme_font_size_override with the values in its parentheses.
  38. add_child(status_label)Add the feedback label to the running scene so Godot can show it on screen.
  39. ledger_label = Label.new()Stores the new value in ledger_label. The expression creates a new Label object.
  40. ledger_label.position = Vector2(535, 278)Updates ledger_label.position, which controls the node's local x and y location. The expression packs x and y into one value.
  41. ledger_label.size = Vector2(355, 105)Updates ledger_label.size, which controls the control's width and height. The expression packs x and y into one value.
  42. ledger_label.add_theme_color_override("font_color", Color("#e3f5f5"))Calls ledger_label.add_theme_color_override with the values in its parentheses.
  43. ledger_label.add_theme_font_size_override("font_size", 16)Calls ledger_label.add_theme_font_size_override with the values in its parentheses.
  44. add_child(ledger_label)Add the ledger to the Control scene so players can read the latest course state.
  45. _connect_direction("Up", Vector2(675, 375), Vector2i.UP)Create the Up touch button. Vector2(675, 375) is the label's pixel position; Vector2i.UP is the step of one tile north.
  46. _connect_direction("Left", Vector2(583, 430), Vector2i.LEFT)Create a Left button that sends one tile west to the shared move function.
  47. _connect_direction("Down", Vector2(675, 430), Vector2i.DOWN)Create a Down button that sends one tile south to the shared move function.
  48. _connect_direction("Right", Vector2(767, 430), Vector2i.RIGHT)Create a Right button that sends one tile east to the shared move function.
  49. _refresh()Calls _refresh with the values in its parentheses.
  50. func _make_button(caption: String, pixel: Vector2, dimensions: Vector2) -> Button:Defines make button, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  51. var button := Button.new()Creates a variable named button. The expression creates a new Button object. Godot infers the type from the first value.
  52. button.text = captionUpdates button.text, which controls the words a player sees. The expression reads caption.
  53. button.position = pixelUpdates button.position, which controls the node's local x and y location. The expression reads pixel.
  54. button.size = dimensionsUpdates button.size, which controls the control's width and height. The expression reads dimensions.
  55. button.add_theme_font_size_override("font_size", 16)Calls button.add_theme_font_size_override with the values in its parentheses.
  56. add_child(button)Adds the new node to the scene tree so that Godot can run and draw it.
  57. return buttonFinishes the function and hands its result back to the caller. The expression reads button.
  58. func _connect_direction(caption: String, pixel: Vector2, delta: Vector2i) -> void:Defines connect direction, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  59. var button := _make_button(caption, pixel, Vector2(84, 48))Creates a variable named button. The expression calls _make_button with the values in its parentheses. Godot infers the type from the first value.
  60. button.pressed.connect(_try_move.bind(delta))Attach this button's own direction to the shared movement callback. A press now calls _try_move with the saved delta.
  61. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  62. draw_rect(Rect2(Vector2.ZERO, size), Color("#081f31"))Paints a rectangle at the given position and size.
  63. draw_rect(Rect2(24, 96, 469, 373), Color("#183b4d"))Paints a rectangle at the given position and size.
  64. draw_rect(Rect2(513, 96, 421, 405), Color("#14374d"))Paints a rectangle at the given position and size.
  65. var font := ThemeDB.fallback_fontCreates a variable named font. The expression reads ThemeDB.fallback_font. Godot infers the type from the first value.
  66. draw_string(font, Vector2(37, 44), "MIDNIGHT DECK DASH", HORIZONTAL_ALIGNMENT_LEFT, -1, 30, Color("#e9f0f2"))Paints a piece of text with a font at a position measured from the node's origin.
  67. draw_string(font, Vector2(38, 73), "Cruise school / Promenade delivery shift", HORIZONTAL_ALIGNMENT_LEFT, -1, 17, Color("#adced5"))Paints a piece of text with a font at a position measured from the node's origin.
  68. draw_string(font, Vector2(535, 119), "SHIPBOARD DISPATCH", HORIZONTAL_ALIGNMENT_LEFT, -1, 16, Color("#8edcd6"))Paints a piece of text with a font at a position measured from the node's origin.
  69. for y in range(DECK_ROWS.size()):Draw one row for each index from 0 to the row count minus one.
  70. for x in range(DECK_ROWS[y].length()):Inside each row, visit its own columns. Looking up each row's length helps if a later room has rows of different lengths.
  71. var cell := Vector2i(x, y)Bundle the current column and row into a game square.
  72. var pixel := BOARD_ORIGIN + Vector2(cell) * CELL_SIZEMultiply the tile address by the spacing, then add the screen offset to get its drawing location.
  73. var blocked := DECK_ROWS[y][x] == "#"Set a true-or-false flag by comparing the characters. Use it to paint a crossed tile when the cell is a bulkhead.
  74. var colour := Color("#22424a") if blocked else Color("#4a6c61")Pick one paint colour for a bulkhead and another for a hallway after reading blocked.
  75. draw_rect(Rect2(pixel, Vector2(59, 59)), colour)Paints a rectangle at the given position and size.
  76. if blocked:Checks blocked. The indented instructions below run only if this test passes.
  77. draw_line(pixel + Vector2(12, 12), pixel + Vector2(47, 47), Color("#688d90"), 4.0)Paints a line between two points with the chosen colour and width.
  78. draw_line(pixel + Vector2(12, 47), pixel + Vector2(47, 12), Color("#688d90"), 4.0)Paints a line between two points with the chosen colour and width.
  79. for raw_mark in STATIONS.keys():Repeats the indented instructions for each value in STATIONS.keys().
  80. var mark := str(raw_mark)Creates a variable named mark. The expression turns a value into text for a player message. Godot infers the type from the first value.
  81. var station: Vector2i = STATIONS[mark]Look up the current dock's square by the shared station mark before drawing its outlined marker.
  82. var dock_pixel := BOARD_ORIGIN + Vector2(station) * CELL_SIZECreates a variable named dock_pixel. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value.
  83. _draw_marker(font, dock_pixel, mark, true)Calls _draw_marker with the values in its parentheses.
  84. for raw_mark in PICKUPS.keys():Repeats the indented instructions for each value in PICKUPS.keys().
  85. var mark := str(raw_mark)Creates a variable named mark. The expression turns a value into text for a player message. Godot infers the type from the first value.
  86. if mark == carried_mark:Skip drawing the pickup circle if Sol already has this parcel in hand.
  87. continueSkips the rest of this turn through the loop and moves on to the next item.
  88. if mark in delivered:Check the completed list for this letter. The next instruction prevents the finished parcel appearing as an available pickup.
  89. continueSkips the rest of this turn through the loop and moves on to the next item.
  90. var parcel_cell: Vector2i = PICKUPS[mark]Read this parcel's original square. It can be drawn at this location whenever it is unclaimed.
  91. var parcel_pixel := BOARD_ORIGIN + Vector2(parcel_cell) * CELL_SIZECreates a variable named parcel_pixel. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value.
  92. _draw_marker(font, parcel_pixel, mark, false)Calls _draw_marker with the values in its parentheses.
  93. var courier_pixel := BOARD_ORIGIN + Vector2(courier) * CELL_SIZEMove the courier's drawn symbol to the pixel origin of its current game square.
  94. var courier_center := courier_pixel + Vector2(29, 29)Creates a variable named courier_center. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value.
  95. draw_circle(courier_center, 20.0, Color("#122734"))Paints a filled circle or outline in this node's local coordinates.
  96. draw_circle(courier_center, 16.0, Color("#fff0b5"))Paints a filled circle or outline in this node's local coordinates.
  97. draw_string(font, courier_pixel + Vector2(22, 35), "X", HORIZONTAL_ALIGNMENT_LEFT, -1, 18, Color("#10242f"))Paints a piece of text with a font at a position measured from the node's origin.
  98. func _draw_marker(font: Font, pixel: Vector2, mark: String, is_station: bool) -> void:Defines draw marker, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  99. var centre := pixel + Vector2(29, 29)Creates a variable named centre. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value.
  100. var colour := Color("#8be2d1") if mark == "S" else Color("#f2ad98")Creates a variable named colour. The expression builds a colour from these values. Godot infers the type from the first value.
  101. if is_station:Draw an outlined square for a station. The else branch paints a solid circle for a pickup, so shape identifies the marker's job.
  102. draw_rect(Rect2(pixel + Vector2(7, 7), Vector2(44, 44)), colour, false, 3.0)Paints a rectangle at the given position and size.
  103. else:Runs the indented fallback instructions if none of the earlier tests passed.
  104. draw_circle(centre, 19.0, colour)Paints a filled circle or outline in this node's local coordinates.
  105. var ink := colour if is_station else Color("#10242f")Creates a variable named ink. The expression chooses one of two values by checking the condition in the middle. Godot infers the type from the first value.
  106. draw_string(font, pixel + Vector2(23, 35), mark, HORIZONTAL_ALIGNMENT_LEFT, -1, 18, ink)Paints a piece of text with a font at a position measured from the node's origin.
  107. func _inside(cell: Vector2i) -> bool:Return true only for a tile address that exists in the floor plan.
  108. if cell.y < 0 or cell.y >= DECK_ROWS.size():Reject rows before the first row and at or beyond the number of rows. The row is checked before it is indexed.
  109. return falseFinishes the function and hands its result back to the caller. The expression uses the yes-or-no value false.
  110. return cell.x >= 0 and cell.x < DECK_ROWS[cell.y].length()Now that the row exists, accept the column only when it lies between zero and the row's last character.
  111. func _tile_at(cell: Vector2i) -> String:Defines tile at, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  112. return DECK_ROWS[cell.y][cell.x]Use the row first and the column second to retrieve one tile character.
  113. func _try_move(delta: Vector2i) -> void:Use one movement rule for both input methods. delta is the proposed step.
  114. var candidate := courier + deltaCalculate the possible next square. Hold it in a temporary variable until both movement guards approve.
  115. if not _inside(candidate):Check the candidate's address before reading the map. The return below leaves the courier unchanged if the address is outside.
  116. status = "That is outside the deck."Stores the new value in status. The expression uses the text inside the quotes.
  117. _refresh()Calls _refresh with the values in its parentheses.
  118. returnFinishes the function right here. Any later instructions in this function are skipped.
  119. if _tile_at(candidate) == "#":Once you know the address exists, read its character. A # is a bulkhead and blocks this step.
  120. status = "A bulkhead blocks that square."Stores the new value in status. The expression uses the text inside the quotes.
  121. _refresh()Calls _refresh with the values in its parentheses.
  122. returnFinishes the function right here. Any later instructions in this function are skipped.
  123. courier = candidateCommit the checked move by replacing Sol's current square with the candidate.
  124. turns += 1Add one and store it back in turns. Only legal moves reach this line.
  125. status = "Moved to column %d, row %d." % [courier.x + 1, courier.y + 1]Stores the new value in status. The expression uses the text inside the quotes.
  126. _resolve_landing()Now that Sol has entered the square, check whether a parcel can be delivered or picked up.
  127. _refresh()Calls _refresh with the values in its parentheses.
  128. func _resolve_landing() -> void:Apply parcel rules using the current square and hand state. The movement guards have already approved this landing.
  129. if carried_mark != "":Check for a parcel in hand. The next branch determines whether the game should deliver it or block another pickup.
  130. var intended_dock: Vector2i = STATIONS[carried_mark]Use the parcel mark in Sol's hands to find its one matching receiving square.
  131. if courier == intended_dock:Deliver only when Sol's current tile equals the correct recipient tile for the carried parcel.
  132. var delivered_mark := carried_markMake a local copy of the mark before changing Sol's hands. The copy can still be used to record and name the delivery.
  133. delivered.append(delivered_mark)Add the delivered parcel's mark to the finished list so the same parcel cannot be taken again.
  134. carried_mark = ""Empty Sol's hands. The next parcel pickup can now succeed.
  135. status = "%s parcel delivered." % STATION_NAMES[delivered_mark]Stores the new value in status. The expression uses the text inside the quotes.
  136. returnFinishes the function right here. Any later instructions in this function are skipped.
  137. if courier in STATIONS.values():If Sol is at a station after missing the intended-square test, it must be a different receiver. Keep the parcel and show the destination name.
  138. status = "Wrong station. Keep this parcel for %s." % STATION_NAMES[carried_mark]Stores the new value in status. The expression uses the text inside the quotes.
  139. returnFinishes the function right here. Any later instructions in this function are skipped.
  140. for raw_mark in PICKUPS.keys():Repeats the indented instructions for each value in PICKUPS.keys().
  141. var mark := str(raw_mark)Creates a variable named mark. The expression turns a value into text for a player message. Godot infers the type from the first value.
  142. if courier != PICKUPS[mark]:Skip this job when its pickup square is different from Sol's current square. The loop will check other marks.
  143. continueSkips the rest of this turn through the loop and moves on to the next item.
  144. if mark in delivered:Check the completed list for this letter. The next instruction prevents the finished parcel appearing as an available pickup.
  145. returnFinishes the function right here. Any later instructions in this function are skipped.
  146. if carried_mark != "":Check for a parcel in hand. The next branch determines whether the game should deliver it or block another pickup.
  147. status = "Hands full. Deliver %s first." % carried_markStores the new value in status. The expression uses the text inside the quotes.
  148. returnFinishes the function right here. Any later instructions in this function are skipped.
  149. carried_mark = markPick up the matching parcel. The drawing loop will hide its circle while Sol holds it.
  150. status = "Picked up %s for %s." % [mark, STATION_NAMES[mark]]Stores the new value in status. The expression uses the text inside the quotes.
  151. returnFinishes the function right here. Any later instructions in this function are skipped.
  152. func _refresh() -> void:Defines refresh, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  153. status_label.text = statusCopy the latest feedback text into the visible label.
  154. var report := "Moves: %d" % turnsCreates a variable named report. The expression uses the text inside the quotes. Godot infers the type from the first value.
  155. var hands := "empty" if carried_mark == "" else carried_markTranslate the empty-string data into a readable word; otherwise show the parcel letter.
  156. report += "\nCarried parcel: %s" % handsReads report, adds to its old value, then stores the result back in the same place.
  157. report += "\nDelivered: %d / %d" % [delivered.size(), PICKUPS.size()]Reads report, adds to its old value, then stores the result back in the same place.
  158. ledger_label.text = reportPresent the current dispatch report in the visible ledger label.
  159. queue_redraw()Ask Godot to run _draw again using the new game state.
  160. func _input(event: InputEvent) -> void:Receive input events from Godot and translate recognised keyboard actions into the same rules the touch buttons call.
  161. if not event is InputEventKey:Pass on non-keyboard input. On-screen Button nodes handle touch presses through their own pressed signals.
  162. returnFinishes the function right here. Any later instructions in this function are skipped.
  163. var key := event as InputEventKeyTreat the checked event as a keyboard event so its pressed, echo and keycode data are available.
  164. if not key.pressed or key.echo:Ignore key releases and automatic repeats. A distinct press therefore asks for one move.
  165. returnFinishes the function right here. Any later instructions in this function are skipped.
  166. match key.keycode:Compare the key code against the branches below and run the matching command.
  167. KEY_UP:Starts the instructions for this branch. The indented lines belong together.
  168. _try_move(Vector2i.UP)Calls _try_move with the values in its parentheses.
  169. KEY_LEFT:Starts the instructions for this branch. The indented lines belong together.
  170. _try_move(Vector2i.LEFT)Calls _try_move with the values in its parentheses.
  171. KEY_DOWN:Starts the instructions for this branch. The indented lines belong together.
  172. _try_move(Vector2i.DOWN)Calls _try_move with the values in its parentheses.
  173. KEY_RIGHT:Starts the instructions for this branch. The indented lines belong together.
  174. _try_move(Vector2i.RIGHT)Calls _try_move with the values in its parentheses.
  175. _:Handles every match value that the branches above did not handle.
  176. returnFinishes the function right here. Any later instructions in this function are skipped.
  177. get_viewport().set_input_as_handled()Mark a recognised key as already used so later input callbacks do not reuse that event.

Find and fix a bug

If the wrong station accepts S, check for the condition courier == intended_dock. Checking only courier in STATIONS.values() would accept any station.

Try a change

Ages 10–12: try the wrong-then-right route and write the final count. Ages 13–16: use a paper trace to explain how the two early returns prevent a single landing from changing both delivery and pickup state.