Cabin Crew 13 · Load supplies only at the galley

Cabin Crew Express · Lesson 13 of 50 · Beginner to intermediate · About 28 min

Water, meals and blankets begin at the front of the cabin. Ari can stock the cart there before heading out to help a passenger.

Storage cupboards, water and folded supplies in the compact onboard galley.
What you’ll make Turn supply buttons into requests that can change the cart only at row 0.

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

Tap a supply. A bound argument names the item. → Check the loading place. Only row 0 can stock the cart. → Add and show the item. Append puts it at the end of the array.
Tap a supply → Check the loading place → Add and show the item
Godot checkpoint window for Load supplies only at the galley
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson typed Array[String], constant, != comparison, append, Callable.bind, join, inline conditional.

  1. Open the checkpoint in Godot

    Extract the folder for lesson 13. In Godot Project Manager choose Import and select project.godot. Open the main scene in the FileSystem dock and select its Control root. Double-click main.gd to enter Script. Press F5 to run. Locate the Water, Meal and Blanket buttons. The cart begins empty.

    extends Control

    Why it works Godot loads the Control scene and calls _ready after the node enters the scene tree. Press F8 to stop before making changes.

  2. Name the loading point

    Find GALLEY and read its value. Locate its guard inside _load_item.

    const GALLEY := 0

    Why it works The constant gives useful meaning to 0. Future code can say GALLEY when it means the station instead of a zero used for another purpose.

  3. Hold text labels in a cart

    Find the typed Array near the top. Add one Water using the button and compare [] in the script with the new display.

    var cart: Array[String] = []

    Why it works An empty Array starts with no values. Array[String] specifies that every item is a string such as Water or Meal.

  4. Bind a different item per button

    Read _ready and compare Water and Meal. Under each label follow the Callable after the button position.

    _load_item.bind("Water")

    Why it works The buttons run the same function with different arguments. bind stores the argument so Godot supplies it when that button is pressed.

  5. Gate the stock action

    Move once towards Cabins and press Blanket. Read the galley message. Walk back and press Blanket again.

    if row != GALLEY:

    Why it works != means different from. An away-from-galley press reaches return and cannot append anything. The second press reaches the successful branch.

  6. Read and display the cart

    Look in _refresh for the Cart line. Compare an empty cart with a cart holding Water and Blanket. Stop and explain which part adds the comma.

    note_label.text += "\nCart: " + ("empty" if cart.is_empty() else ", ".join(cart))

    Why it works join uses the separator between all array entries. The inline if chooses empty until at least one item is present.

Watch the values change

  1. Load Water at row 0. cart becomes [Water] and turns rises by 1.
    What you see. The cart line shows Water.
  2. Walk to row 1 and tap Blanket. row is different from GALLEY; stock and turn count do not change on that tap.
    What you see. The message asks Ari to return.
  3. Return to row 0 and load Blanket. cart.append places Blanket after Water.
    What you see. The cart line shows Water, Blanket.

Predict before you run Why does tapping the Meal button at row 1 leave the cart and turns unchanged?

Reveal the answer

The row check exits before both cart.append and turns += 1.

Complete checkpoint

The three buttons add their own supply name at the galley. At any passenger row, pressing a supply button displays a helpful refusal.

main.gd

extends Control

const ROW_COUNT := 6
const TOP_ROW_Y := 153
const ROW_GAP := 38
const SEAT_LETTERS: Array[String] = ["A", "B", "C", "D"]
const SEAT_X: Array[int] = [49, 126, 276, 353]
const INK := Color("#162d43")
const PANEL := Color("#1c4057")
const PAPER := Color("#f4f1e4")
const MINT := Color("#83dbba")
const GOLD := Color("#ffd986")

var row := 0
var status := "Welcome aboard. Move towards the cabins."
var location_label: Label
var note_label: Label

var turns := 0

const GALLEY := 0
var cart: Array[String] = []

func _ready() -> void:
	_text("CABIN CREW EXPRESS", Vector2(30, 21), Vector2(580, 34), 27)
	_text("Ari’s cabin service training", Vector2(31, 59), Vector2(580, 22), 15)
	_text("SINGLE AISLE · CABIN PLAN", Vector2(48, 106), Vector2(340, 24), 14)
	location_label = _text("", Vector2(469, 112), Vector2(437, 30), 20)
	_button("← Galley", Vector2(469, 147), 196, _try_move.bind(-1))
	_button("Cabins →", Vector2(687, 147), 196, _try_move.bind(1))
	note_label = _text("", Vector2(469, 444), Vector2(438, 60), 17)
	note_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	_text("GALLEY SUPPLIES · LOAD ONLY AT ROW 0", Vector2(469, 198), Vector2(435, 23), 13)
	_button("Water", Vector2(469, 225), 133, _load_item.bind("Water"))
	_button("Meal", Vector2(610, 225), 133, _load_item.bind("Meal"))
	_button("Blanket", Vector2(751, 225), 133, _load_item.bind("Blanket"))
	_refresh()

func _text(words: String, at: Vector2, box: Vector2, font_size: int) -> Label:
	var label := Label.new()
	label.text = words
	label.position = at
	label.size = box
	label.add_theme_font_size_override("font_size", font_size)
	label.add_theme_color_override("font_color", PAPER)
	add_child(label)
	return label


func _button(words: String, at: Vector2, width: int, handler: Callable) -> void:
	var button := Button.new()
	button.text = words
	button.position = at
	button.custom_minimum_size = Vector2(width, 44)
	button.size = Vector2(width, 44)
	button.pressed.connect(handler)
	add_child(button)


func _unhandled_key_input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_up") or event.is_action_pressed("ui_left"):
		_try_move(-1)
	elif event.is_action_pressed("ui_down") or event.is_action_pressed("ui_right"):
		_try_move(1)

func _try_move(direction: int) -> void:
	var candidate := row + direction
	if candidate < 0 or candidate > ROW_COUNT:
		_report("End of the aisle. Ari stays here.")
		return
	row = candidate
	turns += 1
	_report("Ari reached " + _place_name() + ".")

func _load_item(item: String) -> void:
	if row != GALLEY:
		_report("Go back to the galley to load supplies.")
		return
	cart.append(item)
	turns += 1
	_report(item + " loaded.")

func _place_name() -> String:
	if row == 0:
		return "the galley"
	return "row " + str(row)


func _report(words: String) -> void:
	status = words
	_refresh()


func _refresh() -> void:
	location_label.text = "Ari is at " + _place_name().capitalize()
	location_label.text += " · %d turns" % turns
	note_label.text = status
	note_label.text += "\nCart: " + ("empty" if cart.is_empty() else ", ".join(cart))
	queue_redraw()

func _draw() -> void:
	var font := ThemeDB.fallback_font
	draw_rect(Rect2(0, 0, 960, 540), INK)
	draw_rect(Rect2(29, 92, 388, 414), PANEL)
	draw_rect(Rect2(438, 92, 492, 414), PANEL)
	draw_rect(Rect2(193, 126, 54, 256), Color("#132b3d"))
	draw_string(font, Vector2(139, 137), "GALLEY", HORIZONTAL_ALIGNMENT_LEFT, -1, 13, GOLD)
	for y in range(1, ROW_COUNT + 1):
		var seat_y := TOP_ROW_Y + (y - 1) * ROW_GAP
		for index in range(SEAT_LETTERS.size()):
			var x := SEAT_X[index]
			var letter := SEAT_LETTERS[index]
			var seat := str(y) + letter
			var seat_colour := Color("#315b70")
			draw_rect(Rect2(x, seat_y, 49, 31), seat_colour)
			draw_string(font, Vector2(x + 10, seat_y + 21), seat, HORIZONTAL_ALIGNMENT_LEFT, -1, 15, PAPER)
	var ari_y := 140 if row == 0 else TOP_ROW_Y + 15 + (row - 1) * ROW_GAP
	draw_circle(Vector2(220, ari_y), 11, MINT)
	draw_string(font, Vector2(210, ari_y + 3), "ARI", HORIZONTAL_ALIGNMENT_LEFT, -1, 10, INK)

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 ControlThe lesson’s root is a Godot Control. This base supplies screen drawing and lets the script add labels and interactive buttons to the same scene.
  2. const ROW_COUNT := 6Name the highest permitted aisle row. Passenger rows run from 1 to 6. The galley at 0 is a separate legal place.
  3. const TOP_ROW_Y := 153Begin the first row of seats at y = 153 pixels in the teaching viewport.
  4. const ROW_GAP := 38Place each next passenger row 38 pixels below the preceding one.
  5. const SEAT_LETTERS: Array[String] = ["A", "B", "C", "D"]Keep A and B on the left side and C and D on the right. The seat-letter loop uses this order.
  6. const SEAT_X: Array[int] = [49, 126, 276, 353]Store one x pixel position for each seat letter in the same index order. The wide gap between the second and third values forms the aisle.
  7. const INK := Color("#162d43")Creates a constant named INK. The expression builds a colour from these values. Godot infers the type from the first value.
  8. const PANEL := Color("#1c4057")Creates a constant named PANEL. The expression builds a colour from these values. Godot infers the type from the first value.
  9. const PAPER := Color("#f4f1e4")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value.
  10. const MINT := Color("#83dbba")Creates a constant named MINT. The expression builds a colour from these values. Godot infers the type from the first value.
  11. const GOLD := Color("#ffd986")Creates a constant named GOLD. The expression builds a colour from these values. Godot infers the type from the first value.
  12. var row := 0Start Ari in the galley. The row is a game position, independent of the number of pixels used to draw it.
  13. var status := "Welcome aboard. Move towards the cabins."Keep the latest player message in a value that other functions can update.
  14. var location_label: LabelReserve a typed reference for the Label that will show Ari’s row and accepted turn count.
  15. var note_label: LabelReserve a Label for action feedback and the live cart inventory.
  16. var turns := 0Start with no accepted actions. Walking, loading and serving will add to this counter only after their checks pass.
  17. const GALLEY := 0Give the special position 0 a familiar name. The stock and finish rules use the same named location.
  18. var cart: Array[String] = []Create the list of supplies currently loaded. Each stored string occupies one position in the cart.
  19. func _ready() -> void:Godot calls this once as the Control becomes ready. The function places the controls and starts the first display refresh.
  20. _text("CABIN CREW EXPRESS", Vector2(30, 21), Vector2(580, 34), 27)Calls _text with the values in its parentheses.
  21. _text("Ari’s cabin service training", Vector2(31, 59), Vector2(580, 22), 15)Calls _text with the values in its parentheses.
  22. _text("SINGLE AISLE · CABIN PLAN", Vector2(48, 106), Vector2(340, 24), 14)Calls _text with the values in its parentheses.
  23. location_label = _text("", Vector2(469, 112), Vector2(437, 30), 20)Create the main status header first and retain it for row and turn updates.
  24. _button("← Galley", Vector2(469, 147), 196, _try_move.bind(-1))The named Galley button sends -1 to the movement rule when pressed. Its screen arrow and placement point the player back.
  25. _button("Cabins →", Vector2(687, 147), 196, _try_move.bind(1))The Cabins button sends +1 towards the higher-numbered passenger rows.
  26. note_label = _text("", Vector2(469, 444), Vector2(438, 60), 17)Create the multiline feedback area below the service controls and keep a reference to it.
  27. note_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTAllow long reports to fit the right panel by breaking at word boundaries.
  28. _text("GALLEY SUPPLIES · LOAD ONLY AT ROW 0", Vector2(469, 198), Vector2(435, 23), 13)Calls _text with the values in its parentheses.
  29. _button("Water", Vector2(469, 225), 133, _load_item.bind("Water"))Make the Water button request its own supply with the shared galley rule.
  30. _button("Meal", Vector2(610, 225), 133, _load_item.bind("Meal"))Make this button send Meal to exactly the same loading function.
  31. _button("Blanket", Vector2(751, 225), 133, _load_item.bind("Blanket"))Connect the Blanket choice to the same location and cart checks as the other items.
  32. _refresh()Calls _refresh with the values in its parentheses.
  33. func _text(words: String, at: Vector2, box: Vector2, font_size: int) -> Label:Collect the label setup into one helper. The caller supplies the displayed words, a position, a size and the desired font size.
  34. var label := Label.new()Create a Godot Label at runtime for the text UI.
  35. label.text = wordsGive the new label the words passed by its caller.
  36. label.position = atSet its top-left pixel location on the Control screen.
  37. label.size = boxSet the requested bounding rectangle so the text can wrap within the feedback region.
  38. label.add_theme_font_size_override("font_size", font_size)Apply this helper’s requested size to the Label without changing the global project theme.
  39. label.add_theme_color_override("font_color", PAPER)Use the shared warm-white text colour for readable interface labels.
  40. add_child(label)Attach the Label to the active scene tree so Godot will draw it.
  41. return labelProvide the newly created node to its caller so dynamic text can later be edited.
  42. func _button(words: String, at: Vector2, width: int, handler: Callable) -> void:Reuse one constructor for all touch-friendly buttons. A Callable carries the function to connect.
  43. var button := Button.new()Make a standard Godot Button that supports pointer input and keyboard focus.
  44. button.text = wordsUpdates button.text, which controls the words a player sees. The expression reads words.
  45. button.position = atUpdates button.position, which controls the node's local x and y location. The expression reads at.
  46. button.custom_minimum_size = Vector2(width, 44)Reserve at least 44 drawing pixels vertically for this checkpoint’s buttons.
  47. button.size = Vector2(width, 44)Updates button.size, which controls the control's width and height. The expression packs x and y into one value.
  48. button.pressed.connect(handler)Connect the press signal to the caller’s behaviour. Bound values such as a direction or stock item are kept on that callable.
  49. add_child(button)Put the Button in the running scene so it can appear and receive interaction.
  50. func _unhandled_key_input(event: InputEvent) -> void:Handle recognised arrow events after ordinary GUI controls have had their chance to respond.
  51. if event.is_action_pressed("ui_up") or event.is_action_pressed("ui_left"):Recognise either Up or Left as a move back towards row 0.
  52. _try_move(-1)Calls _try_move with the values in its parentheses.
  53. elif event.is_action_pressed("ui_down") or event.is_action_pressed("ui_right"):If the previous test was false, recognise Down or Right as a move towards the passenger rows.
  54. _try_move(1)Calls _try_move with the values in its parentheses.
  55. func _try_move(direction: int) -> void:All movement inputs ask this one function to propose and validate a step.
  56. var candidate := row + directionAdd the move offset to the old row without altering Ari’s current position yet.
  57. if candidate < 0 or candidate > ROW_COUNT:Reject an invalid row if either comparison is true. Legal endpoints 0 and 6 both pass.
  58. _report("End of the aisle. Ari stays here.")Calls _report with the values in its parentheses.
  59. returnFinishes the function right here. Any later instructions in this function are skipped.
  60. row = candidateCommit the new row only after both ends of the aisle have been checked.
  61. turns += 1Spend exactly one turn for this accepted action, after all refusal branches have returned.
  62. _report("Ari reached " + _place_name() + ".")Calls _report with the values in its parentheses.
  63. func _load_item(item: String) -> void:Use the string from the chosen stock button to ask for one supply.
  64. if row != GALLEY:Refuse stock when Ari stands outside the galley. The location check runs before the capacity check.
  65. _report("Go back to the galley to load supplies.")Calls _report with the values in its parentheses.
  66. returnFinishes the function right here. Any later instructions in this function are skipped.
  67. cart.append(item)Put one newly loaded supply at the end of the cart, after the location and capacity checks.
  68. turns += 1Spend exactly one turn for this accepted action, after all refusal branches have returned.
  69. _report(item + " loaded.")Calls _report with the values in its parentheses.
  70. func _place_name() -> String:Translate the row number into the location words shown to the player.
  71. if row == 0:Use a place name for the galley rather than presenting it as passenger row zero.
  72. return "the galley"Return an understandable location label for row 0.
  73. return "row " + str(row)Turn a passenger row into a single piece of display text.
  74. func _report(words: String) -> void:Pass any updated action feedback through one place before refreshing.
  75. status = wordsSave the latest message before asking the screen to show it.
  76. _refresh()Calls _refresh with the values in its parentheses.
  77. func _refresh() -> void:Update the labels from the current saved game state whenever an action reports something.
  78. location_label.text = "Ari is at " + _place_name().capitalize()Render the current row or galley name in the location header.
  79. location_label.text += " · %d turns" % turnsAppend the accepted action count using whole-number formatting.
  80. note_label.text = statusPut the latest action report at the top of the feedback box.
  81. note_label.text += "\nCart: " + ("empty" if cart.is_empty() else ", ".join(cart))Add inventory on the next line. Show empty for an unstocked cart and join item names with commas otherwise.
  82. queue_redraw()Ask Godot to repaint the cabin on the next frame using the updated row, selected seat and deliveries.
  83. func _draw() -> void:Godot calls this method to display the cabin layout and visible game state.
  84. var font := ThemeDB.fallback_fontUse Godot’s available fallback font for custom drawing on the cabin plan.
  85. draw_rect(Rect2(0, 0, 960, 540), INK)Paints a rectangle at the given position and size.
  86. draw_rect(Rect2(29, 92, 388, 414), PANEL)Paints a rectangle at the given position and size.
  87. draw_rect(Rect2(438, 92, 492, 414), PANEL)Paints a rectangle at the given position and size.
  88. draw_rect(Rect2(193, 126, 54, 256), Color("#132b3d"))Paints a rectangle at the given position and size.
  89. draw_string(font, Vector2(139, 137), "GALLEY", HORIZONTAL_ALIGNMENT_LEFT, -1, 13, GOLD)Paints a piece of text with a font at a position measured from the node's origin.
  90. for y in range(1, ROW_COUNT + 1):Draw passenger rows 1 through 6. The upper bound of range is excluded, so add 1 to include the last row.
  91. var seat_y := TOP_ROW_Y + (y - 1) * ROW_GAPConvert the logical row number to vertical screen pixels. Subtract one so the first passenger row starts at the configured origin.
  92. for index in range(SEAT_LETTERS.size()):Run the body once for each letter index. The current function uses that index to create or draw every seat in order.
  93. var x := SEAT_X[index]Use the stored pixel coordinate for the current seat letter to leave the central gap undisturbed.
  94. var letter := SEAT_LETTERS[index]Read the seat letter from its original position in the four-element list.
  95. var seat := str(y) + letterBuild the visual seat ID from the row being drawn rather than Ari’s current row.
  96. var seat_colour := Color("#315b70")Creates a variable named seat_colour. The expression builds a colour from these values. Godot infers the type from the first value.
  97. draw_rect(Rect2(x, seat_y, 49, 31), seat_colour)Paints a rectangle at the given position and size.
  98. draw_string(font, Vector2(x + 10, seat_y + 21), seat, HORIZONTAL_ALIGNMENT_LEFT, -1, 15, PAPER)Paints a piece of text with a font at a position measured from the node's origin.
  99. var ari_y := 140 if row == 0 else TOP_ROW_Y + 15 + (row - 1) * ROW_GAPPlace Ari at the galley when row is zero. Otherwise convert the row to its passenger-seat centre.
  100. draw_circle(Vector2(220, ari_y), 11, MINT)Show a fixed x coordinate in the middle aisle and a y coordinate that follows Ari’s position.
  101. draw_string(font, Vector2(210, ari_y + 3), "ARI", HORIZONTAL_ALIGNMENT_LEFT, -1, 10, INK)Paints a piece of text with a font at a position measured from the node's origin.

Find and fix a bug

If every button loads Water, inspect each .bind argument. The label and the bound item should identify the same supply.

Try a change

Ages 10–12: load all three supplies in a chosen order and read them back. Ages 13–16: explain why a cart item should be a plain string here and how a dictionary could later store quantities.