Cabin Crew 11 · Keep Ari in the aisle

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

At the front of the plane, the galley sits at row 0. The passenger cabin has six rows. Write a rule so Ari can reach every row while staying in the central aisle.

Storage cupboards, water and folded supplies in the compact onboard galley.
What you’ll make Check proposed moves against both ends of the aircraft cabin.

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/10 when the lesson builds on prior work, or copy the checkpoints/11 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.

Pick a direction. Add -1 toward the galley or 1 toward the cabins. → Check both endpoints. Reject the candidate if it lies outside rows 0 to 6. → Store and repaint. Update Ari’s row only after the checks pass.
Pick a direction → Check both endpoints → Store and repaint
Godot checkpoint window for Keep Ari in the aisle
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson int, 0-based position, function parameter, or, comparisons, early return, _unhandled_key_input.

  1. Open the checkpoint in Godot

    Extract the folder for lesson 11. 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. Find the galley, the green ARI marker and the six cabin rows.

    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. Find the bounds

    Read ROW_COUNT and row in main.gd. Walk one step towards the cabins. Compare the caption with the seat labels nearby.

    const ROW_COUNT := 6

    Why it works The maximum is 6. The initial row is 0, so each step through the aisle can be represented with a single whole number.

  3. Propose a move

    Find _try_move and use the parameter as an offset. Calculate 0 + 1 before pressing Cabins. Calculate 1 - 1 before pressing Galley.

    var candidate := row + direction

    Why it works The sign of direction says which way to go. candidate is temporary, so changing it leaves the visible courier at the old row.

  4. Reject both ends

    At row 0, press Galley. Then walk to row 6 and try Cabins once more. Read the same boundary message.

    if candidate < 0 or candidate > ROW_COUNT:

    Why it works or is true if either test is true. The check rejects -1 and 7 while accepting every row from 0 through 6.

  5. Commit the accepted row

    Locate row = candidate below the return. Stop, put a breakpoint by clicking the gutter at that line, run and make one blocked tap then one accepted one. Remove the breakpoint afterward.

    row = candidate

    Why it works A blocked press exits early. An accepted press reaches the assignment and keeps the new row for later movement.

  6. Compare touch and keys

    Run again. Try the left and right buttons, then Up and Down with the keyboard. Verify that the caption and ARI marker move together.

    		_try_move(1)

    Why it works Buttons and keyboard call _try_move. _report sends a single update through _refresh, so both input methods paint the same position.

Watch the values change

  1. Try Galley at row 0. The candidate is -1, outside the allowed aisle.
    What you see. Ari stays at row 0.
  2. Move to Cabins from row 0. The candidate is 1 and passes both edge tests.
    What you see. The marker and caption go to row 1.
  3. Try Cabins at row 6. The candidate is 7, beyond ROW_COUNT.
    What you see. Ari stays at row 6 with a boundary message.

Predict before you run What would happen if the second test used candidate >= ROW_COUNT?

Reveal the answer

It would block row 6 along with row 7. The last guest row is legal, so the script uses >.

Complete checkpoint

Ari starts at the galley and visits every row from 1 to 6. A move outside that range leaves the position intact.

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

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
	_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
	_report("Ari reached " + _place_name() + ".")

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()
	note_label.text = status
	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. func _ready() -> void:Godot calls this once as the Control becomes ready. The function places the controls and starts the first display refresh.
  17. _text("CABIN CREW EXPRESS", Vector2(30, 21), Vector2(580, 34), 27)Calls _text with the values in its parentheses.
  18. _text("Ari’s cabin service training", Vector2(31, 59), Vector2(580, 22), 15)Calls _text with the values in its parentheses.
  19. _text("SINGLE AISLE · CABIN PLAN", Vector2(48, 106), Vector2(340, 24), 14)Calls _text with the values in its parentheses.
  20. location_label = _text("", Vector2(469, 112), Vector2(437, 30), 20)Create the main status header first and retain it for row and turn updates.
  21. _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.
  22. _button("Cabins →", Vector2(687, 147), 196, _try_move.bind(1))The Cabins button sends +1 towards the higher-numbered passenger rows.
  23. 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.
  24. note_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTAllow long reports to fit the right panel by breaking at word boundaries.
  25. _refresh()Calls _refresh with the values in its parentheses.
  26. 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.
  27. var label := Label.new()Create a Godot Label at runtime for the text UI.
  28. label.text = wordsGive the new label the words passed by its caller.
  29. label.position = atSet its top-left pixel location on the Control screen.
  30. label.size = boxSet the requested bounding rectangle so the text can wrap within the feedback region.
  31. 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.
  32. label.add_theme_color_override("font_color", PAPER)Use the shared warm-white text colour for readable interface labels.
  33. add_child(label)Attach the Label to the active scene tree so Godot will draw it.
  34. return labelProvide the newly created node to its caller so dynamic text can later be edited.
  35. 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.
  36. var button := Button.new()Make a standard Godot Button that supports pointer input and keyboard focus.
  37. button.text = wordsUpdates button.text, which controls the words a player sees. The expression reads words.
  38. button.position = atUpdates button.position, which controls the node's local x and y location. The expression reads at.
  39. button.custom_minimum_size = Vector2(width, 44)Reserve at least 44 drawing pixels vertically for this checkpoint’s buttons.
  40. 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.
  41. 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.
  42. add_child(button)Put the Button in the running scene so it can appear and receive interaction.
  43. func _unhandled_key_input(event: InputEvent) -> void:Handle recognised arrow events after ordinary GUI controls have had their chance to respond.
  44. 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.
  45. _try_move(-1)Calls _try_move with the values in its parentheses.
  46. 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.
  47. _try_move(1)Calls _try_move with the values in its parentheses.
  48. func _try_move(direction: int) -> void:All movement inputs ask this one function to propose and validate a step.
  49. var candidate := row + directionAdd the move offset to the old row without altering Ari’s current position yet.
  50. if candidate < 0 or candidate > ROW_COUNT:Reject an invalid row if either comparison is true. Legal endpoints 0 and 6 both pass.
  51. _report("End of the aisle. Ari stays here.")Calls _report with the values in its parentheses.
  52. returnFinishes the function right here. Any later instructions in this function are skipped.
  53. row = candidateCommit the new row only after both ends of the aisle have been checked.
  54. _report("Ari reached " + _place_name() + ".")Calls _report with the values in its parentheses.
  55. func _place_name() -> String:Translate the row number into the location words shown to the player.
  56. if row == 0:Use a place name for the galley rather than presenting it as passenger row zero.
  57. return "the galley"Return an understandable location label for row 0.
  58. return "row " + str(row)Turn a passenger row into a single piece of display text.
  59. func _report(words: String) -> void:Pass any updated action feedback through one place before refreshing.
  60. status = wordsSave the latest message before asking the screen to show it.
  61. _refresh()Calls _refresh with the values in its parentheses.
  62. func _refresh() -> void:Update the labels from the current saved game state whenever an action reports something.
  63. location_label.text = "Ari is at " + _place_name().capitalize()Render the current row or galley name in the location header.
  64. note_label.text = statusPut the latest action report at the top of the feedback box.
  65. queue_redraw()Ask Godot to repaint the cabin on the next frame using the updated row, selected seat and deliveries.
  66. func _draw() -> void:Godot calls this method to display the cabin layout and visible game state.
  67. var font := ThemeDB.fallback_fontUse Godot’s available fallback font for custom drawing on the cabin plan.
  68. draw_rect(Rect2(0, 0, 960, 540), INK)Paints a rectangle at the given position and size.
  69. draw_rect(Rect2(29, 92, 388, 414), PANEL)Paints a rectangle at the given position and size.
  70. draw_rect(Rect2(438, 92, 492, 414), PANEL)Paints a rectangle at the given position and size.
  71. draw_rect(Rect2(193, 126, 54, 256), Color("#132b3d"))Paints a rectangle at the given position and size.
  72. 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.
  73. 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.
  74. 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.
  75. 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.
  76. var x := SEAT_X[index]Use the stored pixel coordinate for the current seat letter to leave the central gap undisturbed.
  77. var letter := SEAT_LETTERS[index]Read the seat letter from its original position in the four-element list.
  78. var seat := str(y) + letterBuild the visual seat ID from the row being drawn rather than Ari’s current row.
  79. 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.
  80. draw_rect(Rect2(x, seat_y, 49, 31), seat_colour)Paints a rectangle at the given position and size.
  81. 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.
  82. 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.
  83. 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.
  84. 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 the mark leaves the cabin, make sure the assignment sits after the guard and that return belongs inside the rejected branch.

Try a change

Ages 10–12: walk from row 0 to row 6 using one input method and return using another. Ages 13–16: adapt the diagram for a smaller four-row training plane and verify both endpoints.