Cabin Crew Express · Lesson 38 of 50 · Intermediate to advanced · About 38 min
Someone might answer a call during the flight game. Ari should be left at exactly the same row when the player comes back.

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/37 when the lesson builds on prior work, or copy the checkpoints/38 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 _notification, Boolean state, is_instance_valid, disabled, early return.
-
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.
-
Pause a running service
Press Advance one row, then Pause. Read the toggle and refresh functions.
paused = not pausedWhy it works paused records a real mode. Changing the button's printed caption is a separate job.
-
Disable the visible move control
Observe the Advance button while paused. Compare its appearance after pressing Resume.
step_button.disabled = pausedWhy it works Button.disabled makes the touch target inactive and gives a visual clue about the current mode.
-
Block a turn behind the buttons too
Read advance from top to bottom. Test Simulate app switching and check the row after resuming.
if paused:Why it works The early return protects the game if another part of the project calls advance directly while service is paused.
-
Listen for app lifecycle notifications
Read the condition inside _notification. Compare switching away on desktop with tapping the simulator button.
if what == NOTIFICATION_APPLICATION_PAUSED or what == NOTIFICATION_APPLICATION_FOCUS_OUT:Why it works Godot informs scripts about lifecycle changes. The is_instance_valid check protects UI references before _ready has created them.
-
Resume deliberately
Press Resume and then advance again. Observe whether you move one or two rows.
row = mini(row + 1, 6)Why it works The new turn runs only when paused is false. Resuming does not replay any attempted movement.
Watch the values change
-
Advance then pause row is 1 and paused becomes true.
What you see. The row stays visible and the move button disables. -
Simulate app switch background_app sets paused to true.
What you see. There is still no movement. -
Resume and advance The mode reopens before the next turn.
What you see. The display now reads Row 2.
Predict before you run Does pressing Resume count as a turn?
Reveal the answer
No. It changes the mode and refreshes the controls. Only Advance changes row.
Complete checkpoint
Pause and simulated app switching hold the current row. Resume returns control to the player, ready for exactly one new move.
main.gd
extends Control
var paused := false
var row := 0
var step_button: Button
var toggle_button: Button
var status: Label
func _ready() -> void:
heading("Keep Ari in place while the game pauses", "LIFECYCLE")
status = label("", Vector2(48, 148))
step_button = button("Advance one row", Vector2(48, 263), advance, 202)
toggle_button = button("Pause", Vector2(265, 263), toggle_pause, 164)
button("Simulate app switching", Vector2(445, 263), background_app, 244)
refresh()
func _notification(what: int) -> void:
if what == NOTIFICATION_APPLICATION_PAUSED or what == NOTIFICATION_APPLICATION_FOCUS_OUT:
if is_instance_valid(status):
background_app()
func background_app() -> void:
paused = true
refresh()
func toggle_pause() -> void:
paused = not paused
refresh()
func advance() -> void:
if paused:
return
row = mini(row + 1, 6)
refresh()
func refresh() -> void:
status.text = "Row %d • %s" % [row, "Paused" if paused else "Service running"]
step_button.disabled = paused
toggle_button.text = "Resume" if paused else "Pause"
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
-
extends ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules. -
var paused := falseStore whether the logical service loop is accepting actions. -
var row := 0Creates a variable named row. The expression uses the whole number 0. Godot infers the type from the first value. -
var step_button: ButtonDeclares step_button for later use. A colon followed by a type says which kind of value belongs here. -
var toggle_button: ButtonDeclares toggle_button 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. -
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. -
heading("Keep Ari in place while the game pauses", "LIFECYCLE")Calls heading with the values in its parentheses. -
status = label("", Vector2(48, 148))Stores the new value in status. The expression calls label with the values in its parentheses. -
step_button = button("Advance one row", Vector2(48, 263), advance, 202)Stores the new value in step_button. The expression calls button with the values in its parentheses. -
toggle_button = button("Pause", Vector2(265, 263), toggle_pause, 164)Stores the new value in toggle_button. The expression calls button with the values in its parentheses. -
button("Simulate app switching", Vector2(445, 263), background_app, 244)Calls button with the values in its parentheses. -
refresh()Calls refresh with the values in its parentheses. -
func _notification(what: int) -> void:Godot calls this for engine notices, such as the app going into the background. -
if what == NOTIFICATION_APPLICATION_PAUSED or what == NOTIFICATION_APPLICATION_FOCUS_OUT:React both to mobile backgrounding and to a window that has lost focus. -
if is_instance_valid(status):A lifecycle event can arrive before _ready has created the display. Check the reference first. -
background_app()Calls background_app with the values in its parentheses. -
func background_app() -> void:Use the same pause state for a real interruption and the manual simulator button. -
paused = trueRequest a particular mode directly. Repeating an interruption should not resume the game. -
refresh()Calls refresh with the values in its parentheses. -
func toggle_pause() -> void:Defines toggle pause, 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. -
paused = not pausedThe explicit pause/resume button switches between the two modes. -
refresh()Calls refresh with the values in its parentheses. -
func advance() -> 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 paused:Protect the logic even if a different scene requests movement without clicking its disabled button. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
row = mini(row + 1, 6)Advance only allowed turns and retain the upper row limit. -
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. -
status.text = "Row %d • %s" % [row, "Paused" if paused else "Service running"]Updates status.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
step_button.disabled = pausedMake the touch target and its appearance agree with the current state. -
toggle_button.text = "Resume" if paused else "Pause"Offer the next available action in words so the player can deliberately resume. -
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. -
label("CABIN CREW EXPRESS / " + topic, Vector2(48, 41), 15, Color("#ffca7b"))Calls label with the values in its parentheses. -
label(caption, Vector2(48, 73), 27)Calls label with the values in its parentheses. -
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. -
var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value. -
item.text = textUpdates item.text, which controls the words a player sees. The expression reads text. -
item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at. -
item.add_theme_font_size_override("font_size", pixels)Calls item.add_theme_font_size_override with the values in its parentheses. -
item.add_theme_color_override("font_color", ink)Calls item.add_theme_color_override with the values in its parentheses. -
add_child(item)Adds the new node to the scene tree so that Godot can run and draw it. -
return itemFinishes the function and hands its result back to the caller. The expression reads item. -
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. -
var item := Button.new()Creates a variable named item. The expression creates a new Button object. Godot infers the type from the first value. -
item.text = textUpdates item.text, which controls the words a player sees. The expression reads text. -
item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at. -
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. -
item.pressed.connect(action)Links a signal to the function that should run when the event happens. -
add_child(item)Adds the new node to the scene tree so that Godot can run and draw it. -
return itemFinishes the function and hands its result back to the caller. The expression reads item. -
func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state. -
draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))Paints a rectangle at the given position and size. -
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 leaving the app crashes before the first screen appears, check that the notification validates status before touching the button or label.
Try a change
Ages 10–12 can add a different paused caption. Ages 13–16 can connect background_app to a validated save routine and test pause when a move animation is underway.