Midnight Deck Dash · Lesson 24 of 50 · Intermediate · About 22 min
Below the shopfronts, the crew uses service hatches to reach the far end quickly. Give your courier the same trick and handle arrivals in a clear order.

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/23 when the lesson builds on prior work, or copy the checkpoints/24 folder to try this lesson on its own. Read the file names in the steps before editing.


Build it step by step
Language and tools in this lesson dictionary lookup, keys, values, paired coordinates, conditional, single hop.
-
Open this checkpoint in Godot
Unzip checkpoint 24. Open Godot Project Manager, choose Import and select its project.godot. Double-click main.gd in FileSystem, then find the ≋ hatch symbol in the legend. Press F6 if the main scene is open, or F5 to run the project.
Why it works Each checkpoint is a complete playable exercise. Saving an extra copy before editing gives you a quick way back when an experiment goes wrong.
-
Build a two-way address book
Open route_rules.gd. Read the hatches dictionary in first_deck. Sketch an arrow from coordinate (1, 3) to (7, 3) and a second arrow pointing back.
var hatches := {Vector2i(1, 3): Vector2i(7, 3), Vector2i(7, 3): Vector2i(1, 3)}Why it works A coordinate key maps to one destination value. You need two entries for a pair to work from both ends.
-
Trigger the shortcut on entry
Find the hatch condition in advance. Read it after the floor guard and before the tick increases. Point to the action check that prevents Wait on a hatch from triggering it.
if action != "WAIT" and deck["hatches"].has(arrival):Why it works Only entering a valid hatch by moving performs the hop. Waiting on the arrival square leaves you there, rather than reversing the shortcut.
-
Use one lookup and then continue
Follow the assignment to arrival. Confirm there is no loop around that lookup. Find the later line that saves arrival as the player's current square.
arrival = deck["hatches"][arrival]Why it works The destination is also an entry in the address book. Repeating a lookup there would send the player back. One lookup models one trip.
-
Draw a marker without changing movement
In main.gd find the elif that tests whether a cell has a hatch. Change the on-screen marker in your own copy, then show that the travel rule still works.
elif deck["hatches"].has(cell):Why it works Artwork reads from the same deck data as the rules. Changing a pictured symbol does not change either of the paired coordinates.
-
Take a one-turn shortcut
Reset. Tap South twice. Count how many turns it took to get from (1, 2) across to (7, 3). Tap Wait and check that your character stays on the far end.
Why it works This checkpoint checks trolley contact at the landing square. Keeping a single, explained order makes later hints reproduce the same result.
Watch the values change
-
South from (1, 1) to (1, 2)
What you see. An ordinary floor move spends one turn. -
South onto (1, 3) destination remaps to (7, 3)
What you see. The courier appears on the far hatch in one turn. -
Wait on (7, 3) step is (0, 0) and action is WAIT
What you see. The courier stays on the far hatch while time advances.
Predict before you run If the code looked up the hatch destination over and over while the cell was a hatch, what would happen?
Reveal the answer
The pair would send the value back and forth forever. Use one if check for each move.
Complete checkpoint
South, South crosses from the left to the right service hatch. The player remains on the right if they wait.
main.gd
extends Control
const Rules = preload("res://route_rules.gd")
const LABEL_BLUE := Color("#b4d9e5")
const PAPER := Color("#fff1ca")
var deck: Dictionary
var available: Array[Dictionary]
var active := 0
var state: Dictionary
var heading: Label
var board: Label
var status: Label
var folder: VBoxContainer
func _ready() -> void:
var backdrop := ColorRect.new()
backdrop.color = Color("#071f30")
backdrop.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
backdrop.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(backdrop)
folder = VBoxContainer.new()
folder.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
folder.offset_left = 22
folder.offset_right = -22
folder.offset_top = 14
folder.offset_bottom = -12
folder.add_theme_constant_override("separation", 8)
add_child(folder)
heading = make_label("", 27, PAPER)
make_label("MIDNIGHT DECK DASH / TRAINING DECK", 15, LABEL_BLUE)
board = make_label("", 28, PAPER)
var mono := SystemFont.new()
mono.font_names = PackedStringArray(["DejaVu Sans Mono", "Courier New", "monospace"])
board.add_theme_font_override("font", mono)
make_label("@ You O Trolley a Parcel A Delivery X Departure # Wall ≋ Hatch", 14, LABEL_BLUE)
status = make_label("", 17, PAPER)
status.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
status.custom_minimum_size.y = 48
var controls := HBoxContainer.new()
controls.add_theme_constant_override("separation", 6)
folder.add_child(controls)
make_button(controls, "North", _move.bind("N"))
make_button(controls, "West", _move.bind("W"))
make_button(controls, "South", _move.bind("S"))
make_button(controls, "East", _move.bind("E"))
make_button(controls, "Wait", _move.bind("WAIT"))
make_button(controls, "Reset", _reset)
available = [Rules.first_deck()]
_reset()
func make_label(words: String, px: int, ink: Color) -> Label:
var label := Label.new()
label.text = words
label.add_theme_font_size_override("font_size", px)
label.add_theme_color_override("font_color", ink)
folder.add_child(label)
return label
func make_button(parent: HBoxContainer, words: String, callback: Callable) -> void:
var button := Button.new()
button.text = words
button.custom_minimum_size = Vector2(70, 40)
button.pressed.connect(callback)
parent.add_child(button)
func _reset() -> void:
deck = available[active]
state = Rules.initial_state(deck)
refresh()
func _move(action: String) -> void:
Rules.advance(deck, state, action)
refresh()
func refresh() -> void:
heading.text = deck["title"]
var view := ""
var cart := Rules.cart_cell(deck, state["tick"])
for row in range(deck["height"]):
for col in range(deck["width"]):
var cell := Vector2i(col, row)
var mark := "."
if deck["walls"].has(cell):
mark = "#"
elif cell == deck["exit"]:
mark = "X"
elif deck["pickups"].has(cell) and deck["pickups"][cell] not in state["delivered"] and state["carrying"] != deck["pickups"][cell]:
mark = deck["pickups"][cell]
elif deck["drops"].has(cell):
mark = deck["drops"][cell].to_upper()
elif deck["hatches"].has(cell):
mark = "≋"
if cell == cart:
mark = "O"
if cell == state["cell"]:
mark = "!" if state["caught"] else "@"
view += mark + " "
view += "
"
board.text = view.strip_edges()
var cargo: String = state["carrying"].to_upper() if state["carrying"] != "" else "Empty"
status.text = "Turn %d • Cargo: %s • %s" % [state["tick"], cargo, state["message"]]
route_rules.gd
extends RefCounted
const DELTAS := {
"N": Vector2i(0, -1), "E": Vector2i(1, 0),
"S": Vector2i(0, 1), "W": Vector2i(-1, 0),
"WAIT": Vector2i.ZERO
}
static func first_deck() -> Dictionary:
var walls: Dictionary = {}
for row in range(5):
for col in range(9):
var cell := Vector2i(col, row)
if row == 0 or row == 4 or col == 0 or col == 8 or cell in [Vector2i(2, 2), Vector2i(6, 2)]:
walls[cell] = true
var hatches := {Vector2i(1, 3): Vector2i(7, 3), Vector2i(7, 3): Vector2i(1, 3)}
return {
"title": "Moonlit Promenade", "width": 9, "height": 5,
"start": Vector2i(1, 1), "walls": walls,
"pickups": {Vector2i(4, 1): "a"}, "drops": {Vector2i(6, 1): "a"},
"exit": Vector2i(7, 1), "hatches": hatches,
"cart": [Vector2i(3, 1), Vector2i(4, 1), Vector2i(4, 2), Vector2i(3, 2)]
}
static func initial_state(deck: Dictionary) -> Dictionary:
return {
"cell": deck["start"], "tick": 0, "carrying": "",
"delivered": [], "caught": false, "complete": false,
"message": "Deliver the parcel, then reach X."
}
static func cart_cell(deck: Dictionary, tick: int) -> Vector2i:
var patrol: Array = deck["cart"]
return patrol[tick % patrol.size()]
static func on_floor(deck: Dictionary, cell: Vector2i) -> bool:
return cell.x >= 0 and cell.y >= 0 and cell.x < deck["width"] and cell.y < deck["height"] and not deck["walls"].has(cell)
static func advance(deck: Dictionary, state: Dictionary, action: String) -> void:
if state["caught"] or state["complete"]:
return
if not DELTAS.has(action):
return
var origin: Vector2i = state["cell"]
var step: Vector2i = DELTAS[action]
var arrival := origin + step
if not on_floor(deck, arrival):
state["message"] = "Bulkhead: no turn spent."
return
if action != "WAIT" and deck["hatches"].has(arrival):
arrival = deck["hatches"][arrival]
var old_cart := cart_cell(deck, state["tick"])
state["cell"] = arrival
state["tick"] += 1
var new_cart := cart_cell(deck, state["tick"])
var overtake := old_cart == arrival and new_cart == origin
if arrival == new_cart or overtake:
state["caught"] = true
state["message"] = "Trolley in the lane. Reset to try again."
return
apply_delivery(deck, state)
static func apply_delivery(deck: Dictionary, state: Dictionary) -> void:
var cell: Vector2i = state["cell"]
var ready: String = deck["pickups"].get(cell, "")
var destination: String = deck["drops"].get(cell, "")
if state["carrying"] == "" and ready != "" and ready not in state["delivered"]:
state["carrying"] = ready
state["message"] = "Parcel %s aboard." % ready.to_upper()
elif state["carrying"] != "" and state["carrying"] == destination:
state["delivered"].append(destination)
state["carrying"] = ""
state["message"] = "Parcel delivered to %s." % destination.to_upper()
else:
state["message"] = "Turn %d." % state["tick"]
if cell == deck["exit"] and state["delivered"].size() == deck["pickups"].size():
state["complete"] = true
state["message"] = "All parcels delivered. Departure ready!"
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
-
extends ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules. -
const Rules = preload("res://route_rules.gd")Creates a constant named Rules. The expression prepares a resource at a fixed project path when the script is loaded. -
const LABEL_BLUE := Color("#b4d9e5")Creates a constant named LABEL_BLUE. The expression builds a colour from these values. Godot infers the type from the first value. -
const PAPER := Color("#fff1ca")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value. -
var deck: DictionaryDeclares deck for later use. A colon followed by a type says which kind of value belongs here. -
var available: Array[Dictionary]Declares available for later use. A colon followed by a type says which kind of value belongs here. -
var active := 0Creates a variable named active. The expression uses the whole number 0. Godot infers the type from the first value. -
var state: DictionaryDeclares state for later use. A colon followed by a type says which kind of value belongs here. -
var heading: LabelDeclares heading for later use. A colon followed by a type says which kind of value belongs here. -
var board: LabelDeclares board for later use. A colon followed by a type says which kind of value belongs here. -
var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here. -
var folder: VBoxContainerDeclares folder for later use. A colon followed by a type says which kind of value belongs here. -
func _ready() -> void:Godot calls this once after the scene and its children are ready. Use it to set up the room and connect controls. -
var backdrop := ColorRect.new()Creates a variable named backdrop. The expression creates a new ColorRect object. Godot infers the type from the first value. -
backdrop.color = Color("#071f30")Stores the new value in backdrop.color. The expression builds a colour from these values. -
backdrop.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Positions this Control relative to the bounds of its parent. -
backdrop.mouse_filter = Control.MOUSE_FILTER_IGNOREUpdates backdrop.mouse_filter, which controls how this control participates in pointer and touch input. The expression reads Control.MOUSE_FILTER_IGNORE. -
add_child(backdrop)Adds the new node to the scene tree so that Godot can run and draw it. -
folder = VBoxContainer.new()Stores the new value in folder. The expression creates a new VBoxContainer object. -
folder.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Positions this Control relative to the bounds of its parent. -
folder.offset_left = 22Stores the new value in folder.offset_left. The expression uses the whole number 22. -
folder.offset_right = -22Stores the new value in folder.offset_right. The expression reads -22. -
folder.offset_top = 14Stores the new value in folder.offset_top. The expression uses the whole number 14. -
folder.offset_bottom = -12Stores the new value in folder.offset_bottom. The expression reads -12. -
folder.add_theme_constant_override("separation", 8)Calls folder.add_theme_constant_override with the values in its parentheses. -
add_child(folder)Adds the new node to the scene tree so that Godot can run and draw it. -
heading = make_label("", 27, PAPER)Stores the new value in heading. The expression calls make_label with the values in its parentheses. -
make_label("MIDNIGHT DECK DASH / TRAINING DECK", 15, LABEL_BLUE)Calls make_label with the values in its parentheses. -
board = make_label("", 28, PAPER)Stores the new value in board. The expression calls make_label with the values in its parentheses. -
var mono := SystemFont.new()Creates a variable named mono. The expression creates a new SystemFont object. Godot infers the type from the first value. -
mono.font_names = PackedStringArray(["DejaVu Sans Mono", "Courier New", "monospace"])Stores the new value in mono.font_names. The expression calls PackedStringArray with the values in its parentheses. -
board.add_theme_font_override("font", mono)Calls board.add_theme_font_override with the values in its parentheses. -
make_label("@ You O Trolley a Parcel A Delivery X Departure # Wall ≋ Hatch", 14, LABEL_BLUE)Calls make_label with the values in its parentheses. -
status = make_label("", 17, PAPER)Stores the new value in status. The expression calls make_label with the values in its parentheses. -
status.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTStores the new value in status.autowrap_mode. The expression reads TextServer.AUTOWRAP_WORD_SMART. -
status.custom_minimum_size.y = 48Stores the new value in status.custom_minimum_size.y. The expression uses the whole number 48. -
var controls := HBoxContainer.new()Creates a variable named controls. The expression creates a new HBoxContainer object. Godot infers the type from the first value. -
controls.add_theme_constant_override("separation", 6)Calls controls.add_theme_constant_override with the values in its parentheses. -
folder.add_child(controls)Adds the new node to the scene tree so that Godot can run and draw it. -
make_button(controls, "North", _move.bind("N"))Calls make_button with the values in its parentheses. -
make_button(controls, "West", _move.bind("W"))Calls make_button with the values in its parentheses. -
make_button(controls, "South", _move.bind("S"))Calls make_button with the values in its parentheses. -
make_button(controls, "East", _move.bind("E"))Calls make_button with the values in its parentheses. -
make_button(controls, "Wait", _move.bind("WAIT"))Calls make_button with the values in its parentheses. -
make_button(controls, "Reset", _reset)Calls make_button with the values in its parentheses. -
available = [Rules.first_deck()]Stores the new value in available. The expression makes a list whose items keep their order. -
_reset()Calls _reset with the values in its parentheses. -
func make_label(words: String, px: int, ink: Color) -> Label:Defines make label, 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. -
var label := Label.new()Creates a variable named label. The expression creates a new Label object. Godot infers the type from the first value. -
label.text = wordsUpdates label.text, which controls the words a player sees. The expression reads words. -
label.add_theme_font_size_override("font_size", px)Calls label.add_theme_font_size_override with the values in its parentheses. -
label.add_theme_color_override("font_color", ink)Calls label.add_theme_color_override with the values in its parentheses. -
folder.add_child(label)Adds the new node to the scene tree so that Godot can run and draw it. -
return labelFinishes the function and hands its result back to the caller. The expression reads label. -
func make_button(parent: HBoxContainer, words: String, callback: Callable) -> void: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. -
var button := Button.new()Creates a variable named button. The expression creates a new Button object. Godot infers the type from the first value. -
button.text = wordsUpdates button.text, which controls the words a player sees. The expression reads words. -
button.custom_minimum_size = Vector2(70, 40)Updates button.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value. -
button.pressed.connect(callback)Links a signal to the function that should run when the event happens. -
parent.add_child(button)Adds the new node to the scene tree so that Godot can run and draw it. -
func _reset() -> void:Defines reset, 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. -
deck = available[active]Stores the new value in deck. The expression looks up the item or key written between the square brackets. -
state = Rules.initial_state(deck)Stores the new value in state. The expression calls Rules.initial_state with the values in its parentheses. -
refresh()Calls refresh with the values in its parentheses. -
func _move(action: String) -> void:Defines move, 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. -
Rules.advance(deck, state, action)Calls Rules.advance with the values in its parentheses. -
refresh()Calls refresh with the values in its parentheses. -
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. -
heading.text = deck["title"]Updates heading.text, which controls the words a player sees. The expression looks up the item or key written between the square brackets. -
var view := ""Creates a variable named view. The expression uses the text inside the quotes. Godot infers the type from the first value. -
var cart := Rules.cart_cell(deck, state["tick"])Creates a variable named cart. The expression calls Rules.cart_cell with the values in its parentheses. Godot infers the type from the first value. -
for row in range(deck["height"]):Repeats the indented instructions for each value in range(deck["height"]). -
for col in range(deck["width"]):Repeats the indented instructions for each value in range(deck["width"]). -
var cell := Vector2i(col, row)Creates a variable named cell. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value. -
var mark := "."Creates a variable named mark. The expression uses the text inside the quotes. Godot infers the type from the first value. -
if deck["walls"].has(cell):Checks deck["walls"].has(cell). The indented instructions below run only if this test passes. -
mark = "#"Stores the new value in mark. The expression uses the text inside the quotes. -
elif cell == deck["exit"]:If the earlier branches did not run, checks cell == deck["exit"] next. -
mark = "X"Stores the new value in mark. The expression uses the text inside the quotes. -
elif deck["pickups"].has(cell) and deck["pickups"][cell] not in state["delivered"] and state["carrying"] != deck["pickups"][cell]:If the earlier branches did not run, checks deck["pickups"].has(cell) and deck["pickups"][cell] not in state["delivered"] and state["carrying"] != deck["pickups"][cell] next. -
mark = deck["pickups"][cell]Stores the new value in mark. The expression looks up the item or key written between the square brackets. -
elif deck["drops"].has(cell):If the earlier branches did not run, checks deck["drops"].has(cell) next. -
mark = deck["drops"][cell].to_upper()Stores the new value in mark. The expression looks up the item or key written between the square brackets. -
elif deck["hatches"].has(cell):If the earlier branches did not run, checks deck["hatches"].has(cell) next. -
mark = "≋"Stores the new value in mark. The expression uses the text inside the quotes. -
if cell == cart:Checks cell == cart. The indented instructions below run only if this test passes. -
mark = "O"Stores the new value in mark. The expression uses the text inside the quotes. -
if cell == state["cell"]:Checks cell == state["cell"]. The indented instructions below run only if this test passes. -
mark = "!" if state["caught"] else "@"Stores the new value in mark. The expression chooses one of two values by checking the condition in the middle. -
view += mark + " "Reads view, adds to its old value, then stores the result back in the same place. -
view += "Reads view, adds to its old value, then stores the result back in the same place. -
"Continues this part of the script. Read the expression on this line in the context of the nearby declaration or function. -
board.text = view.strip_edges()Updates board.text, which controls the words a player sees. The expression removes spacing at the start and end of the text. -
var cargo: String = state["carrying"].to_upper() if state["carrying"] != "" else "Empty"Creates a variable named cargo. The expression chooses one of two values by checking the condition in the middle. The type after the colon says what kind of value it can hold. -
status.text = "Turn %d • Cargo: %s • %s" % [state["tick"], cargo, state["message"]]Updates status.text, which controls the words a player sees. The expression uses the text inside the quotes.
Read route_rules.gd line by line
-
extends RefCountedStarts with Godot's RefCounted type. This script gets the abilities of that type and adds the game's own rules. -
const DELTAS := {Creates a constant named DELTAS. The expression makes a dictionary that looks up values by key. Godot infers the type from the first value. -
"N": Vector2i(0, -1), "E": Vector2i(1, 0),Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"S": Vector2i(0, 1), "W": Vector2i(-1, 0),Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"WAIT": Vector2i.ZEROContinues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
}Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
static func first_deck() -> Dictionary:Defines first deck, 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. -
var walls: Dictionary = {}Creates a variable named walls. The expression makes a dictionary that looks up values by key. The type after the colon says what kind of value it can hold. -
for row in range(5):Repeats the indented instructions for each value in range(5). -
for col in range(9):Repeats the indented instructions for each value in range(9). -
var cell := Vector2i(col, row)Creates a variable named cell. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value. -
if row == 0 or row == 4 or col == 0 or col == 8 or cell in [Vector2i(2, 2), Vector2i(6, 2)]:Checks row == 0 or row == 4 or col == 0 or col == 8 or cell in [Vector2i(2, 2), Vector2i(6, 2)]. The indented instructions below run only if this test passes. -
walls[cell] = trueStores the new value in walls[cell]. The expression uses the yes-or-no value true. -
var hatches := {Vector2i(1, 3): Vector2i(7, 3), Vector2i(7, 3): Vector2i(1, 3)}Creates a variable named hatches. The expression makes a dictionary that looks up values by key. Godot infers the type from the first value. -
return {Finishes the function and hands its result back to the caller. The expression makes a dictionary that looks up values by key. -
"title": "Moonlit Promenade", "width": 9, "height": 5,Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"start": Vector2i(1, 1), "walls": walls,Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"pickups": {Vector2i(4, 1): "a"}, "drops": {Vector2i(6, 1): "a"},Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"exit": Vector2i(7, 1), "hatches": hatches,Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"cart": [Vector2i(3, 1), Vector2i(4, 1), Vector2i(4, 2), Vector2i(3, 2)]Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
}Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
static func initial_state(deck: Dictionary) -> Dictionary:Defines initial state, 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. -
return {Finishes the function and hands its result back to the caller. The expression makes a dictionary that looks up values by key. -
"cell": deck["start"], "tick": 0, "carrying": "",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"delivered": [], "caught": false, "complete": false,Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"message": "Deliver the parcel, then reach X."Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
}Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
static func cart_cell(deck: Dictionary, tick: int) -> Vector2i:Defines cart cell, 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. -
var patrol: Array = deck["cart"]Creates a variable named patrol. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold. -
return patrol[tick % patrol.size()]Finishes the function and hands its result back to the caller. The expression looks up the item or key written between the square brackets. -
static func on_floor(deck: Dictionary, cell: Vector2i) -> bool:Defines on floor, 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. -
return cell.x >= 0 and cell.y >= 0 and cell.x < deck["width"] and cell.y < deck["height"] and not deck["walls"].has(cell)Finishes the function and hands its result back to the caller. The expression evaluates the test to see whether it is true or false. -
static func advance(deck: Dictionary, state: Dictionary, action: String) -> void:Defines advance, 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. -
if state["caught"] or state["complete"]:Checks state["caught"] or state["complete"]. The indented instructions below run only if this test passes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
if not DELTAS.has(action):Checks not DELTAS.has(action). The indented instructions below run only if this test passes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
var origin: Vector2i = state["cell"]Creates a variable named origin. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold. -
var step: Vector2i = DELTAS[action]Creates a variable named step. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold. -
var arrival := origin + stepCreates a variable named arrival. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value. -
if not on_floor(deck, arrival):Checks not on_floor(deck, arrival). The indented instructions below run only if this test passes. -
state["message"] = "Bulkhead: no turn spent."Stores the new value in state["message"]. The expression uses the text inside the quotes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
if action != "WAIT" and deck["hatches"].has(arrival):Checks action != "WAIT" and deck["hatches"].has(arrival). The indented instructions below run only if this test passes. -
arrival = deck["hatches"][arrival]Stores the new value in arrival. The expression looks up the item or key written between the square brackets. -
var old_cart := cart_cell(deck, state["tick"])Creates a variable named old_cart. The expression calls cart_cell with the values in its parentheses. Godot infers the type from the first value. -
state["cell"] = arrivalStores the new value in state["cell"]. The expression reads arrival. -
state["tick"] += 1Reads state["tick"], adds to its old value, then stores the result back in the same place. -
var new_cart := cart_cell(deck, state["tick"])Creates a variable named new_cart. The expression calls cart_cell with the values in its parentheses. Godot infers the type from the first value. -
var overtake := old_cart == arrival and new_cart == originCreates a variable named overtake. The expression evaluates the test to see whether it is true or false. Godot infers the type from the first value. -
if arrival == new_cart or overtake:Checks arrival == new_cart or overtake. The indented instructions below run only if this test passes. -
state["caught"] = trueStores the new value in state["caught"]. The expression uses the yes-or-no value true. -
state["message"] = "Trolley in the lane. Reset to try again."Stores the new value in state["message"]. The expression uses the text inside the quotes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
apply_delivery(deck, state)Calls apply_delivery with the values in its parentheses. -
static func apply_delivery(deck: Dictionary, state: Dictionary) -> void:Defines apply delivery, 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. -
var cell: Vector2i = state["cell"]Creates a variable named cell. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold. -
var ready: String = deck["pickups"].get(cell, "")Creates a variable named ready. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold. -
var destination: String = deck["drops"].get(cell, "")Creates a variable named destination. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold. -
if state["carrying"] == "" and ready != "" and ready not in state["delivered"]:Checks state["carrying"] == "" and ready != "" and ready not in state["delivered"]. The indented instructions below run only if this test passes. -
state["carrying"] = readyStores the new value in state["carrying"]. The expression reads ready. -
state["message"] = "Parcel %s aboard." % ready.to_upper()Stores the new value in state["message"]. The expression uses the text inside the quotes. -
elif state["carrying"] != "" and state["carrying"] == destination:If the earlier branches did not run, checks state["carrying"] != "" and state["carrying"] == destination next. -
state["delivered"].append(destination)Continues this part of the script. Read the expression on this line in the context of the nearby declaration or function. -
state["carrying"] = ""Stores the new value in state["carrying"]. The expression uses the text inside the quotes. -
state["message"] = "Parcel delivered to %s." % destination.to_upper()Stores the new value in state["message"]. The expression uses the text inside the quotes. -
else:Runs the indented fallback instructions if none of the earlier tests passed. -
state["message"] = "Turn %d." % state["tick"]Stores the new value in state["message"]. The expression uses the text inside the quotes. -
if cell == deck["exit"] and state["delivered"].size() == deck["pickups"].size():Checks cell == deck["exit"] and state["delivered"].size() == deck["pickups"].size(). The indented instructions below run only if this test passes. -
state["complete"] = trueStores the new value in state["complete"]. The expression uses the yes-or-no value true. -
state["message"] = "All parcels delivered. Departure ready!"Stores the new value in state["message"]. The expression uses the text inside the quotes.
Find and fix a bug
If the player jumps back after tapping Wait, add the action != "WAIT" condition before looking up the hatch.
Try a change
Move the hatch pair to two new open squares. Walk through both directions and record which route saves the most turns.