Cabin Crew Express · Lesson 41 of 50 · Publishing · About 45 min
Ari is planning a calmer cabin pass. A passenger in row 2 needs a blanket before water can go to row 4.

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/40 when the lesson builds on prior work, or copy the checkpoints/41 folder to try this lesson on its own. Read the file names in the steps before editing. Keep the checkpoint ZIP intact and make edits in an extracted working copy.


Build it step by step
Language and tools in this lesson Vector3i, bit flags, while, Dictionary keys, breadth-first search.
-
Sketch the cabin service
Draw a line for aisle rows 0 to 4. Row 0 is the galley. Mark a blanket at 2A and water at 4D. Start with an empty cart. A plan ends with Ari back at row 0.
Why it works A clear end condition tells the solver when the whole shift is finished.
-
Open the planning lab
Import checkpoint 41. Open the main scene and choose main.gd in FileSystem. Press F5 and compare the two missions. The buttons reuse one search with different rules.
Why it works Trying a ready-made mission first gives you a known comparison before editing.
-
Encode a small service state
Read minimum_turns(). Its Vector3i has the aisle row, the set of carried items, and the set of completed orders. Bits 1 and 2 record blanket and water.
var first := Vector3i(0, 0, 0)Why it works Carry and served live in separate fields. A passenger can be served even when the cart is empty.
-
Explore one turn at a time
Read the loop in minimum_turns(). Each candidate from successors() moves one row, loads one supply, or serves one matching order. The queue takes the oldest entry first.
var state: Vector3i = pending.pop_front()Why it works All actions cost one turn. The first solved state is a shortest route because the queue tests earlier turns before later turns.
-
Test the impossible case
Compare a normal route with the sealed-aisle rule. Edit FINAL_ROW or the blocked-row button argument, then run again. Compare with your paper route.
Why it works A readable failure is useful evidence. A missing route may make a mission unwinnable regardless of the turn target.
-
Author a flight in your copy
Back up the reference project. Open levels/flights.json in a text editor. In the copy, replace one training flight and keep its group. Try rows 4, par 12 and orders [[2, 0, 2, true], [4, 3, 0, false]]. Each order is [row, seat index, item index, priority]. Seat indexes 0 to 3 mean A to D. Item indexes 0 to 2 mean Water, Meal and Blanket. Change name and brief, then run the game and check its route hint and turn target.
Why it works Editing one known-working record isolates your changes. The lab models two fixed orders, while the reference game checks its complete data.
Watch the values change
-
Start at the galley Row 0, carried 0, served 0.
What you see. Loading blanket produces a new state. -
Reach row 2 with a blanket The blanket bit is present and its order is outstanding.
What you see. Serving clears the carried bit and sets the served bit. -
Return with all requests served The served field equals 3 and Ari reaches row 0.
What you see. The search returns the fewest turns. -
Seal the aisle at row 3 The far passenger remains beyond a blocked row.
What you see. The search runs out of states.
Predict before you run Will a route that serves 2A and 4D but stops at row 4 pass?
Reveal the answer
No. The solved test also requires returning to the galley at row 0.
Complete checkpoint
Evening pass: 12 turns. Blocked aisle: no complete service route.
main.gd
extends Control
const Lab = preload("res://release_lab.gd")
const TITLE = "Design and verify a cabin mission"
var ui: VBoxContainer
func _ready() -> void:
ui = VBoxContainer.new()
ui.set_script(Lab)
add_child(ui)
ui.start(TITLE)
build()
const FINAL_ROW := 4
func build() -> void:
ui.say("Find a shortest pass to 2A and 4D and return to the galley.")
ui.option("Solve the evening pass", run.bind(-1))
ui.option("Check a blocked aisle", run.bind(3))
func run(blocked_row: int) -> void:
var turns := minimum_turns(blocked_row)
ui.say("Shortest complete service: %d turns." % turns if turns >= 0 else "No complete service route.")
func minimum_turns(blocked_row: int) -> int:
var first := Vector3i(0, 0, 0)
var pending: Array[Vector3i] = [first]
var distance := {}
distance[first] = 0
while not pending.is_empty():
var state: Vector3i = pending.pop_front()
if state.x == 0 and state.z == 3:
return distance[state]
for candidate in successors(state, blocked_row):
if not distance.has(candidate):
distance[candidate] = distance[state] + 1
pending.append(candidate)
return -1
func successors(state: Vector3i, blocked_row: int) -> Array[Vector3i]:
var options: Array[Vector3i] = []
for row in [state.x - 1, state.x + 1]:
if row >= 0 and row <= FINAL_ROW and row != blocked_row:
options.append(Vector3i(row, state.y, state.z))
if state.x == 0:
for item in [1, 2]:
if ((state.y | state.z) & item) == 0:
options.append(Vector3i(0, state.y | item, state.z))
if state.x == 2 and (state.y & 1) != 0:
options.append(Vector3i(2, state.y & ~1, state.z | 1))
if state.x == 4 and (state.z & 1) != 0 and (state.y & 2) != 0:
options.append(Vector3i(4, state.y & ~2, state.z | 2))
return options
release_lab.gd
extends VBoxContainer
var output: Label
func start(title_text: String) -> void:
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
offset_left = 32.0
offset_top = 28.0
offset_right = -32.0
offset_bottom = -28.0
add_theme_constant_override("separation", 12)
var background := ColorRect.new()
background.color = Color("#14283d")
background.mouse_filter = Control.MOUSE_FILTER_IGNORE
get_parent().add_child(background)
background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
get_parent().move_child(background, 0)
var title := Label.new()
title.text = title_text
title.add_theme_font_size_override("font_size", 32)
title.add_theme_color_override("font_color", Color("#ffd293"))
add_child(title)
output = Label.new()
output.custom_minimum_size.y = 151.0
output.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
output.add_theme_font_size_override("font_size", 19)
add_child(output)
func say(message: String) -> void:
output.text = message
func option(label: String, command: Callable) -> void:
var button := Button.new()
button.text = label
button.custom_minimum_size.y = 46.0
button.add_theme_font_size_override("font_size", 19)
button.pressed.connect(command)
add_child(button)
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 Lab = preload("res://release_lab.gd")Creates a constant named Lab. The expression prepares a resource at a fixed project path when the script is loaded. -
const TITLE = "Design and verify a cabin mission"Creates a constant named TITLE. The expression uses the text inside the quotes. -
var ui: VBoxContainerDeclares ui 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. -
ui = VBoxContainer.new()Stores the new value in ui. The expression creates a new VBoxContainer object. -
ui.set_script(Lab)Calls ui.set_script with the values in its parentheses. -
add_child(ui)Adds the new node to the scene tree so that Godot can run and draw it. -
ui.start(TITLE)Calls ui.start with the values in its parentheses. -
build()Calls build with the values in its parentheses. -
const FINAL_ROW := 4Creates a constant named FINAL_ROW. The expression uses the whole number 4. Godot infers the type from the first value. -
func build() -> void:Defines build, 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. -
ui.say("Find a shortest pass to 2A and 4D and return to the galley.")Calls ui.say with the values in its parentheses. -
ui.option("Solve the evening pass", run.bind(-1))Calls ui.option with the values in its parentheses. -
ui.option("Check a blocked aisle", run.bind(3))Calls ui.option with the values in its parentheses. -
func run(blocked_row: int) -> void:Defines run, 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 turns := minimum_turns(blocked_row)Creates a variable named turns. The expression calls minimum_turns with the values in its parentheses. Godot infers the type from the first value. -
ui.say("Shortest complete service: %d turns." % turns if turns >= 0 else "No complete service route.")Calls ui.say with the values in its parentheses. -
func minimum_turns(blocked_row: int) -> int:Defines minimum turns, 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 first := Vector3i(0, 0, 0)Creates a variable named first. The expression calls Vector3i with the values in its parentheses. Godot infers the type from the first value. -
var pending: Array[Vector3i] = [first]Creates a variable named pending. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold. -
var distance := {}Creates a variable named distance. The expression makes a dictionary that looks up values by key. Godot infers the type from the first value. -
distance[first] = 0Stores the new value in distance[first]. The expression uses the whole number 0. -
while not pending.is_empty():Keeps repeating while not pending.is_empty() stays true. The loop body should eventually change the result or leave with break. -
var state: Vector3i = pending.pop_front()Creates a variable named state. The expression takes the first item out of the list and returns it. The type after the colon says what kind of value it can hold. -
if state.x == 0 and state.z == 3:Checks state.x == 0 and state.z == 3. The indented instructions below run only if this test passes. -
return distance[state]Finishes the function and hands its result back to the caller. The expression looks up the item or key written between the square brackets. -
for candidate in successors(state, blocked_row):Repeats the indented instructions for each value in successors(state, blocked_row). -
if not distance.has(candidate):Checks not distance.has(candidate). The indented instructions below run only if this test passes. -
distance[candidate] = distance[state] + 1Stores the new value in distance[candidate]. The expression looks up the item or key written between the square brackets. -
pending.append(candidate)Adds an item to the end of a list. -
return -1Finishes the function and hands its result back to the caller. The expression reads -1. -
func successors(state: Vector3i, blocked_row: int) -> Array[Vector3i]:Defines successors, 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 options: Array[Vector3i] = []Creates a variable named options. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold. -
for row in [state.x - 1, state.x + 1]:Repeats the indented instructions for each value in [state.x - 1, state.x + 1]. -
if row >= 0 and row <= FINAL_ROW and row != blocked_row:Checks row >= 0 and row <= FINAL_ROW and row != blocked_row. The indented instructions below run only if this test passes. -
options.append(Vector3i(row, state.y, state.z))Adds an item to the end of a list. -
if state.x == 0:Checks state.x == 0. The indented instructions below run only if this test passes. -
for item in [1, 2]:Repeats the indented instructions for each value in [1, 2]. -
if ((state.y | state.z) & item) == 0:Checks ((state.y | state.z) & item) == 0. The indented instructions below run only if this test passes. -
options.append(Vector3i(0, state.y | item, state.z))Adds an item to the end of a list. -
if state.x == 2 and (state.y & 1) != 0:Checks state.x == 2 and (state.y & 1) != 0. The indented instructions below run only if this test passes. -
options.append(Vector3i(2, state.y & ~1, state.z | 1))Adds an item to the end of a list. -
if state.x == 4 and (state.z & 1) != 0 and (state.y & 2) != 0:Checks state.x == 4 and (state.z & 1) != 0 and (state.y & 2) != 0. The indented instructions below run only if this test passes. -
options.append(Vector3i(4, state.y & ~2, state.z | 2))Adds an item to the end of a list. -
return optionsFinishes the function and hands its result back to the caller. The expression reads options.
Read release_lab.gd line by line
-
extends VBoxContainerStarts with Godot's VBoxContainer type. This script gets the abilities of that type and adds the game's own rules. -
var output: LabelDeclares output for later use. A colon followed by a type says which kind of value belongs here. -
func start(title_text: String) -> void:Defines start, 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. -
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Positions this Control relative to the bounds of its parent. -
offset_left = 32.0Stores the new value in offset_left. The expression reads 32.0. -
offset_top = 28.0Stores the new value in offset_top. The expression reads 28.0. -
offset_right = -32.0Stores the new value in offset_right. The expression reads -32.0. -
offset_bottom = -28.0Stores the new value in offset_bottom. The expression reads -28.0. -
add_theme_constant_override("separation", 12)Calls add_theme_constant_override with the values in its parentheses. -
var background := ColorRect.new()Creates a variable named background. The expression creates a new ColorRect object. Godot infers the type from the first value. -
background.color = Color("#14283d")Stores the new value in background.color. The expression builds a colour from these values. -
background.mouse_filter = Control.MOUSE_FILTER_IGNOREUpdates background.mouse_filter, which controls how this control participates in pointer and touch input. The expression reads Control.MOUSE_FILTER_IGNORE. -
get_parent().add_child(background)Calls get_parent with the values in its parentheses. -
background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Positions this Control relative to the bounds of its parent. -
get_parent().move_child(background, 0)Calls get_parent with the values in its parentheses. -
var title := Label.new()Creates a variable named title. The expression creates a new Label object. Godot infers the type from the first value. -
title.text = title_textUpdates title.text, which controls the words a player sees. The expression reads title_text. -
title.add_theme_font_size_override("font_size", 32)Calls title.add_theme_font_size_override with the values in its parentheses. -
title.add_theme_color_override("font_color", Color("#ffd293"))Calls title.add_theme_color_override with the values in its parentheses. -
add_child(title)Adds the new node to the scene tree so that Godot can run and draw it. -
output = Label.new()Stores the new value in output. The expression creates a new Label object. -
output.custom_minimum_size.y = 151.0Stores the new value in output.custom_minimum_size.y. The expression reads 151.0. -
output.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTStores the new value in output.autowrap_mode. The expression reads TextServer.AUTOWRAP_WORD_SMART. -
output.add_theme_font_size_override("font_size", 19)Calls output.add_theme_font_size_override with the values in its parentheses. -
add_child(output)Adds the new node to the scene tree so that Godot can run and draw it. -
func say(message: String) -> void:Defines say, 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. -
output.text = messageUpdates output.text, which controls the words a player sees. The expression reads message. -
func option(label: String, command: Callable) -> void:Defines option, 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 = labelUpdates button.text, which controls the words a player sees. The expression reads label. -
button.custom_minimum_size.y = 46.0Stores the new value in button.custom_minimum_size.y. The expression reads 46.0. -
button.add_theme_font_size_override("font_size", 19)Calls button.add_theme_font_size_override with the values in its parentheses. -
button.pressed.connect(command)Links a signal to the function that should run when the event happens. -
add_child(button)Adds the new node to the scene tree so that Godot can run and draw it.
Find and fix a bug
If the queue keeps revisiting a position, inspect the distance dictionary. Record each complete state as you add it, including its cart and served fields.
Try a change
Ages 10–12. Compare playing by hand to the 12-turn solution. Ages 13–16. Add one meal and a real three-item capacity check with at most two carried at once.