Cabin Crew 40 · Run a seven-check service regression

Cabin Crew Express · Lesson 40 of 50 · Intermediate to advanced · About 38 min

Ari's game now has art, controls and a record of progress. A short set of repeated checks helps keep the delivery rules working during later changes.

Storage cupboards, water and folded supplies in the compact onboard galley.
What you’ll make Test common and awkward turns and prove the checks find a deliberately broken delivery rule.

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

Arrange. Load a known test cart. → Act. Try a valid or invalid delivery. → Compare. Check the outcome against a true or false expectation. → Repeat. Run the same cases after a change.
Arrange → Act → Compare → Repeat
Godot checkpoint window for Run a seven-check service regression
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson Array.duplicate, nested function calls, Boolean expectation, guard clause, report.

  1. Open the editable checkpoint

    Start Godot 4.7.2, choose Import in Project Manager and select the project.godot file for this lesson. Open main.gd in FileSystem, then press F5. Stop the game with F8 before editing.

    Why it works This folder is its own project with a Control scene and an attached script. You can run it without files from any other lesson.

  2. Set up a known two-item cart

    Read the line that calls load_item twice. Click Run checks and identify the first three results.

    var cart := load_item(load_item(empty, "Water"), "Meal")

    Why it works The inner call builds a one-item cart. The outer call builds a two-item result while the original array stays empty.

  3. Choose a known correct match

    Find REQUESTS and the fourth check. Explain why slot 0 works for seat 1A.

    ["The right item matches 1A", deliver(cart, "1A", 0)],

    Why it works Array slots start at zero. That slot contains Water, which agrees with the request for 1A.

  4. Check the bad inputs

    Read the next three checks and the first condition inside deliver. Test an invalid slot by following the code instead of reading past the array.

    if not REQUESTS.has(seat) or picked < 0 or picked >= items.size():

    Why it works A guard prevents an unknown seat or slot from being used. The equality rule separately rejects an item that is wrong but otherwise valid.

  5. Watch one test catch a bug

    Click Toggle test fault and identify the failing check. Click it again to restore the rules.

    if fault:

    Why it works The fault skips the comparison, so a wrong item is falsely accepted. The test suite should report exactly that error.

  6. Read the summary

    Find the loop that builds the report. Predict how the summary changes if you add a new passing case.

    pass_count += 1

    Why it works Each true expectation increments pass_count and every row gets a result marker.

Watch the values change

  1. Run clean checks Seven expectations match.
    What you see. The header reads 7 / 7.
  2. Toggle fault on A wrong item returns true.
    What you see. The panel reads 6 / 7 and marks the wrong-item case.
  3. Toggle fault off The real equality check runs again.
    What you see. The panel returns to 7 / 7.

Predict before you run Which tests keep passing when the deliberate mismatch fault is enabled?

Reveal the answer

Only the wrong-item case fails. Unknown seats and invalid slots are still rejected before the test fault branch.

Complete checkpoint

The simulator shows 7 / 7 with correct rules. The deliberate fault changes one match and reports 6 / 7 until the player turns it off.

main.gd

extends Control

const LIMIT := 2
const REQUESTS := {"1A": "Water", "2F": "Meal"}
var fault := false
var report: Label
var status: Label

func _ready() -> void:
	heading("Prove the service rules still work", "QUALITY CHECKS")
	status = label("Run the crew checks, then compare a deliberate bug.", Vector2(48, 125))
	report = label("", Vector2(48, 192), 18)
	button("Run checks", Vector2(48, 417), checks, 159)
	button("Toggle test fault", Vector2(221, 417), toggle_fault, 195)

func load_item(items: Array[String], item: String) -> Array[String]:
	var result := items.duplicate()
	if result.size() < LIMIT:
		result.append(item)
	return result

func deliver(items: Array[String], seat: String, picked: int) -> bool:
	if not REQUESTS.has(seat) or picked < 0 or picked >= items.size():
		return false
	if fault:
		return true
	return items[picked] == REQUESTS[seat]

func toggle_fault() -> void:
	fault = not fault
	status.text = "Deliberate mismatch bug enabled." if fault else "Clean rules restored."
	checks()

func checks() -> void:
	var empty: Array[String] = []
	var cart := load_item(load_item(empty, "Water"), "Meal")
	var tests := [
		["Original cart stays empty", empty.is_empty()],
		["A cart can carry two items", cart.size() == 2],
		["A third item cannot be loaded", load_item(cart, "Blanket").size() == 2],
		["The right item matches 1A", deliver(cart, "1A", 0)],
		["The wrong item is refused by 1A", not deliver(cart, "1A", 1)],
		["An unknown seat is refused", not deliver(cart, "5B", 0)],
		["An invalid slot is refused", not deliver(cart, "1A", 9)],
	]
	var pass_count := 0
	var results: Array[String] = []
	for test in tests:
		var passed: bool = test[1]
		if passed:
			pass_count += 1
		results.append(("%s  " % ("✓" if passed else "✗")) + str(test[0]))
	report.text = "\n".join(results)
	status.text = "%d / %d checks passed." % [pass_count, tests.size()]

func heading(caption: String, topic: String) -> void:
	label("CABIN CREW EXPRESS     /     " + topic, Vector2(48, 41), 15, Color("#ffca7b"))
	label(caption, Vector2(48, 73), 27)

func label(text: String, at: Vector2, pixels: int = 20, ink: Color = Color("#f7f4e9")) -> Label:
	var item := Label.new()
	item.text = text
	item.position = at
	item.add_theme_font_size_override("font_size", pixels)
	item.add_theme_color_override("font_color", ink)
	add_child(item)
	return item

func button(text: String, at: Vector2, action: Callable, width: float = 155.0) -> Button:
	var item := Button.new()
	item.text = text
	item.position = at
	item.custom_minimum_size = Vector2(width, 47)
	item.pressed.connect(action)
	add_child(item)
	return item

func _draw() -> void:
	draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))
	draw_rect(Rect2(Vector2(28, 25), Vector2(904, 483)), Color("#18364b"))

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 ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules.
  2. const LIMIT := 2The regression test pins a cart’s maximum item count at two.
  3. const REQUESTS := {"1A": "Water", "2F": "Meal"}Known seat requests make it possible to test both matching and mismatched items.
  4. var fault := falseCreates a variable named fault. The expression uses the yes-or-no value false. Godot infers the type from the first value.
  5. var report: LabelDeclares report for later use. A colon followed by a type says which kind of value belongs here.
  6. var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here.
  7. 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.
  8. heading("Prove the service rules still work", "QUALITY CHECKS")Calls heading with the values in its parentheses.
  9. status = label("Run the crew checks, then compare a deliberate bug.", Vector2(48, 125))Stores the new value in status. The expression calls label with the values in its parentheses.
  10. report = label("", Vector2(48, 192), 18)Stores the new value in report. The expression calls label with the values in its parentheses.
  11. button("Run checks", Vector2(48, 417), checks, 159)Calls button with the values in its parentheses.
  12. button("Toggle test fault", Vector2(221, 417), toggle_fault, 195)Calls button with the values in its parentheses.
  13. func load_item(items: Array[String], item: String) -> Array[String]:Defines load item, 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.
  14. var result := items.duplicate()Use a fresh array so adding a load does not edit an earlier test state.
  15. if result.size() < LIMIT:Check the free capacity of the candidate cart before appending.
  16. result.append(item)Adds an item to the end of a list.
  17. return resultFinishes the function and hands its result back to the caller. The expression reads result.
  18. func deliver(items: Array[String], seat: String, picked: int) -> bool:Defines deliver, 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.
  19. if not REQUESTS.has(seat) or picked < 0 or picked >= items.size():Guard both the seat lookup and array access before using these addresses.
  20. return falseFinishes the function and hands its result back to the caller. The expression uses the yes-or-no value false.
  21. if fault:Deliberately bypass the matching decision after basic checks. This proves the test detects the right problem.
  22. return trueFinishes the function and hands its result back to the caller. The expression uses the yes-or-no value true.
  23. return items[picked] == REQUESTS[seat]The actual delivery rule compares the selected item with this seat’s request.
  24. func toggle_fault() -> void:Defines toggle fault, 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.
  25. fault = not faultStores the new value in fault. The expression reads not fault.
  26. status.text = "Deliberate mismatch bug enabled." if fault else "Clean rules restored."Updates status.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle.
  27. checks()Calls checks with the values in its parentheses.
  28. func checks() -> void:Defines checks, 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.
  29. var empty: Array[String] = []Arrange a typed empty cart as a known initial test input.
  30. var cart := load_item(load_item(empty, "Water"), "Meal")Compose two loads. The first result becomes the input to the second.
  31. var tests := [Creates a variable named tests. The expression makes a list whose items keep their order. Godot infers the type from the first value.
  32. ["Original cart stays empty", empty.is_empty()],Starts a list or dictionary used as part of the instruction above.
  33. ["A cart can carry two items", cart.size() == 2],Starts a list or dictionary used as part of the instruction above.
  34. ["A third item cannot be loaded", load_item(cart, "Blanket").size() == 2],Starts a list or dictionary used as part of the instruction above.
  35. ["The right item matches 1A", deliver(cart, "1A", 0)],Starts a list or dictionary used as part of the instruction above.
  36. ["The wrong item is refused by 1A", not deliver(cart, "1A", 1)],The result should be false for Meal at 1A. Prepending not turns that expectation into a passing true.
  37. ["An unknown seat is refused", not deliver(cart, "5B", 0)],Starts a list or dictionary used as part of the instruction above.
  38. ["An invalid slot is refused", not deliver(cart, "1A", 9)],The deliberate mismatch should never allow a slot outside the valid array.
  39. ]Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next.
  40. var pass_count := 0Start every run fresh so prior results cannot accumulate.
  41. var results: Array[String] = []Creates a variable named results. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  42. for test in tests:Repeats the indented instructions for each value in tests.
  43. var passed: bool = test[1]The first value in each case holds its name, and the second contains the Boolean result.
  44. if passed:Checks passed. The indented instructions below run only if this test passes.
  45. pass_count += 1Reads pass_count, adds to its old value, then stores the result back in the same place.
  46. results.append(("%s " % ("✓" if passed else "✗")) + str(test[0]))Adds an item to the end of a list.
  47. report.text = "\n".join(results)Join the labelled outcomes with line breaks so the player can identify one failing rule.
  48. status.text = "%d / %d checks passed." % [pass_count, tests.size()]Summarise passing cases over total cases, independent of how many tests the learner adds.
  49. func heading(caption: String, topic: String) -> void:Defines heading, 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.
  50. label("CABIN CREW EXPRESS / " + topic, Vector2(48, 41), 15, Color("#ffca7b"))Calls label with the values in its parentheses.
  51. label(caption, Vector2(48, 73), 27)Calls label with the values in its parentheses.
  52. func label(text: String, at: Vector2, pixels: int = 20, ink: Color = Color("#f7f4e9")) -> Label:Defines 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.
  53. var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value.
  54. item.text = textUpdates item.text, which controls the words a player sees. The expression reads text.
  55. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  56. item.add_theme_font_size_override("font_size", pixels)Calls item.add_theme_font_size_override with the values in its parentheses.
  57. item.add_theme_color_override("font_color", ink)Calls item.add_theme_color_override with the values in its parentheses.
  58. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  59. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  60. func button(text: String, at: Vector2, action: Callable, width: float = 155.0) -> Button:Defines 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.
  61. var item := Button.new()Creates a variable named item. The expression creates a new Button object. Godot infers the type from the first value.
  62. item.text = textUpdates item.text, which controls the words a player sees. The expression reads text.
  63. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  64. item.custom_minimum_size = Vector2(width, 47)Updates item.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value.
  65. item.pressed.connect(action)Links a signal to the function that should run when the event happens.
  66. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  67. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  68. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  69. draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))Paints a rectangle at the given position and size.
  70. draw_rect(Rect2(Vector2(28, 25), Vector2(904, 483)), Color("#18364b"))Paints a rectangle at the given position and size.

Find and fix a bug

If the wrong-item test still passes with the fault on, confirm the check uses not deliver for a known seat and an in-range slot containing a different item.

Try a change

Ages 10–12 can add a check for delivering a Meal to 2F. Ages 13–16 can also check an empty cart and a negative slot, then explain what each one protects.