Market Day London · Lesson 38 of 50 · Intermediate to advanced · About 35 min
Someone calls while a customer is waiting. Nia's current task should be ready when the game returns.

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, NOTIFICATION_APPLICATION_PAUSED, ConfigFile, String, bool.
-
Import the editable checkpoint
In the Godot 4.7.2 Project Manager, select Import and open the project.godot in this lesson folder. Open main.gd in the FileSystem dock. Press F5 to run the market and F8 to stop it before editing.
Why it works This checkpoint already has a Control root with its script attached, so you can compare your edits to a working market scene.
-
Block moves while paused
Press Pause shift, then press Pick up bread and Hand over bread. Read the early guards in both functions.
if paused or held != "Empty":Why it works The guard exits before either action changes the order. A pause cannot count as a move.
-
Save enough to continue
Resume, pick up bread and pause again. Follow the two set_value calls and explain why both fields are saved.
file.set_value("shift", "held", held)Why it works A player needs their current item and delivery count. A score alone cannot restore a partly completed order.
-
Check a loaded value
Choose Resume shift and look at the equality checks before held is assigned. Try any other string in a duplicate saved file.
if saved_item == "Empty" or saved_item == "Bread":Why it works This small practice only knows Bread and Empty. Keeping a known value avoids displaying an item the rules cannot use.
-
Use lifecycle notifications
Read _notification. Match the manual Pause and Resume buttons to the functions in the application notification branches.
if what == NOTIFICATION_APPLICATION_PAUSED:Why it works The same implementation works for a visible control and for this pair of lifecycle signals. The manual buttons make the behaviour testable in the editor.
-
Test on a phone
On a device build, pick up bread, briefly switch apps, then reopen. Log the visible basket and hand it over once. In the editor you can repeat with the manual buttons.
elif what == NOTIFICATION_APPLICATION_RESUMED:Why it works A phone test checks the platform lifecycle as well as the shared rule. Leave your test notes with the exact app and device version.
Watch the values change
-
Pick up bread held becomes Bread.
What you see. The basket shows Bread. -
Pause and try handover paused is true so no delivery changes.
What you see. The basket stays full. -
Resume and deliver The record restores Bread and clears paused.
What you see. Delivered reads 1.
Predict before you run Can a customer collect twice by pressing the handover button during a pause?
Reveal the answer
No. It returns before changing either the count or the item.
Complete checkpoint
The practice shift preserves basket and delivery count during a pause and restores them on resume.
main.gd
extends Control
const SAVE_PATH := "user://market_lesson38_pause.cfg"
var deliveries := 0
var held := "Empty"
var paused := false
var status: Label
func _ready() -> void:
heading("Pause a shift and bring it back", "APP LIFECYCLE")
label("Simulate leaving and reopening one stage of a market order.", Vector2(44, 130), 18, MINT)
status = label("", Vector2(44, 385), 22)
button("Pick up bread", Vector2(44, 215), pick_up, 176)
button("Hand over bread", Vector2(233, 215), hand_over, 176)
button("Pause shift", Vector2(44, 280), pause_shift, 176)
button("Resume shift", Vector2(233, 280), resume_shift, 176)
button("Reset practice", Vector2(422, 280), reset_lab, 176)
show_state()
func pick_up() -> void:
if paused or held != "Empty":
return
held = "Bread"
show_state()
func hand_over() -> void:
if paused or held != "Bread":
return
held = "Empty"
deliveries += 1
show_state()
func pause_shift() -> void:
paused = true
var file := ConfigFile.new()
file.set_value("shift", "deliveries", deliveries)
file.set_value("shift", "held", held)
var error := file.save(SAVE_PATH)
show_state(" • Save failed" if error != OK else "")
func resume_shift() -> void:
var file := ConfigFile.new()
if file.load(SAVE_PATH) == OK:
var saved_item: Variant = file.get_value("shift", "held", "Empty")
if saved_item == "Empty" or saved_item == "Bread":
held = str(saved_item)
var saved_count: Variant = file.get_value("shift", "deliveries", 0)
if saved_count is int:
deliveries = maxi(0, int(saved_count))
paused = false
show_state()
func reset_lab() -> void:
deliveries = 0
held = "Empty"
pause_shift()
resume_shift()
func show_state(note: String = "") -> void:
status.text = "%s • Basket %s • Delivered %d%s" % ["Paused" if paused else "Active", held, deliveries, note]
func _notification(what: int) -> void:
if not is_node_ready():
return
if what == NOTIFICATION_APPLICATION_PAUSED:
pause_shift()
elif what == NOTIFICATION_APPLICATION_RESUMED:
resume_shift()
func _draw() -> void:
base()
const INK := Color("#173353")
const PAPER := Color("#fff4db")
const MINT := Color("#a8e0c1")
const GOLD := Color("#ffd291")
func heading(caption: String, topic: String) -> void:
label("MARKET DAY LONDON / " + topic, Vector2(44, 36), 15, GOLD)
label(caption, Vector2(44, 66), 31)
func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> Label:
var item := Label.new()
item.text = words
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(words: String, at: Vector2, action: Callable, width: float = 164.0) -> Button:
var control := Button.new()
control.text = words
control.position = at
control.custom_minimum_size = Vector2(width, 43)
control.pressed.connect(action)
add_child(control)
return control
func base(colour: Color = Color("#193652")) -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))
draw_rect(Rect2(Vector2(22, 20), size - Vector2(44, 40)), colour)
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 SAVE_PATH := "user://market_lesson38_pause.cfg"Creates a constant named SAVE_PATH. The expression uses the text inside the quotes. Godot infers the type from the first value. -
var deliveries := 0Creates a variable named deliveries. The expression uses the whole number 0. Godot infers the type from the first value. -
var held := "Empty"This small practice basket is represented by one word, either Empty or Bread. -
var paused := falseCreates a variable named paused. The expression uses the yes-or-no value false. Godot infers the type from the first value. -
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("Pause a shift and bring it back", "APP LIFECYCLE")Calls heading with the values in its parentheses. -
label("Simulate leaving and reopening one stage of a market order.", Vector2(44, 130), 18, MINT)Calls label with the values in its parentheses. -
status = label("", Vector2(44, 385), 22)Stores the new value in status. The expression calls label with the values in its parentheses. -
button("Pick up bread", Vector2(44, 215), pick_up, 176)Calls button with the values in its parentheses. -
button("Hand over bread", Vector2(233, 215), hand_over, 176)Calls button with the values in its parentheses. -
button("Pause shift", Vector2(44, 280), pause_shift, 176)Calls button with the values in its parentheses. -
button("Resume shift", Vector2(233, 280), resume_shift, 176)Calls button with the values in its parentheses. -
button("Reset practice", Vector2(422, 280), reset_lab, 176)Calls button with the values in its parentheses. -
show_state()Calls show_state with the values in its parentheses. -
func pick_up() -> void:Defines pick up, 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 or held != "Empty":Accept bread only while playing with an empty basket. Returning here leaves the saved delivery unchanged. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
held = "Bread"Stores the new value in held. The expression uses the text inside the quotes. -
show_state()Calls show_state with the values in its parentheses. -
func hand_over() -> void:Defines hand over, 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 or held != "Bread":The handover needs an active shift and bread in the basket. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
held = "Empty"Stores the new value in held. The expression uses the text inside the quotes. -
deliveries += 1Increment the delivery count only after the valid handover empties the basket. -
show_state()Calls show_state with the values in its parentheses. -
func pause_shift() -> void:Defines pause shift, 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 = trueSet paused first so another action cannot change the state being saved. -
var file := ConfigFile.new()Creates a variable named file. The expression creates a new ConfigFile object. Godot infers the type from the first value. -
file.set_value("shift", "deliveries", deliveries)Calls file.set_value with the values in its parentheses. -
file.set_value("shift", "held", held)Save what Nia is carrying along with the delivery count so resume can reconstruct both values. -
var error := file.save(SAVE_PATH)Creates a variable named error. The expression calls file.save with the values in its parentheses. Godot infers the type from the first value. -
show_state(" • Save failed" if error != OK else "")Report a save failure alongside the status, where the player can read it immediately. -
func resume_shift() -> void:Defines resume shift, 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 file := ConfigFile.new()Creates a variable named file. The expression creates a new ConfigFile object. Godot infers the type from the first value. -
if file.load(SAVE_PATH) == OK:Apply saved values only after Godot loads the practice file successfully. -
var saved_item: Variant = file.get_value("shift", "held", "Empty")Creates a variable named saved_item. The expression calls file.get_value with the values in its parentheses. The type after the colon says what kind of value it can hold. -
if saved_item == "Empty" or saved_item == "Bread":Allow the two values this basket understands. Unexpected entries are skipped. -
held = str(saved_item)Stores the new value in held. The expression turns a value into text for a player message. -
var saved_count: Variant = file.get_value("shift", "deliveries", 0)Creates a variable named saved_count. The expression calls file.get_value with the values in its parentheses. The type after the colon says what kind of value it can hold. -
if saved_count is int:Checks saved_count is int. The indented instructions below run only if this test passes. -
deliveries = maxi(0, int(saved_count))Use zero as the minimum number of completed deliveries. -
paused = falseUpdates paused, which controls whether the scene tree is paused. The expression uses the yes-or-no value false. -
show_state()Calls show_state with the values in its parentheses. -
func reset_lab() -> void:Defines reset lab, 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. -
deliveries = 0Stores the new value in deliveries. The expression uses the whole number 0. -
held = "Empty"Stores the new value in held. The expression uses the text inside the quotes. -
pause_shift()Pausing writes the cleared practice state and resuming reopens the just-saved values. -
resume_shift()Calls resume_shift with the values in its parentheses. -
func show_state(note: String = "") -> void:Defines show state, 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 = "%s • Basket %s • Delivered %d%s" % ["Paused" if paused else "Active", held, deliveries, note]Updates status.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
func _notification(what: int) -> void:Godot calls this for engine notices, such as the app going into the background. -
if not is_node_ready():Some engine notifications can arrive during setup. Wait until the node is ready before changing the status label. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
if what == NOTIFICATION_APPLICATION_PAUSED:When the system signals that the app moved to the background, call the same save function used by the pause button. -
pause_shift()Calls pause_shift with the values in its parentheses. -
elif what == NOTIFICATION_APPLICATION_RESUMED:When the app becomes active again, call the same load function used by the resume button. -
resume_shift()Calls resume_shift with the values in its parentheses. -
func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state. -
base()Calls base with the values in its parentheses. -
const INK := Color("#173353")Creates a constant named INK. The expression builds a colour from these values. Godot infers the type from the first value. -
const PAPER := Color("#fff4db")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value. -
const MINT := Color("#a8e0c1")Creates a constant named MINT. The expression builds a colour from these values. Godot infers the type from the first value. -
const GOLD := Color("#ffd291")Creates a constant named GOLD. The expression builds a colour from these values. Godot infers the type from the first value. -
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("MARKET DAY LONDON / " + topic, Vector2(44, 36), 15, GOLD)Calls label with the values in its parentheses. -
label(caption, Vector2(44, 66), 31)Calls label with the values in its parentheses. -
func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> 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 = wordsUpdates item.text, which controls the words a player sees. The expression reads words. -
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(words: String, at: Vector2, action: Callable, width: float = 164.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 control := Button.new()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value. -
control.text = wordsUpdates control.text, which controls the words a player sees. The expression reads words. -
control.position = atUpdates control.position, which controls the node's local x and y location. The expression reads at. -
control.custom_minimum_size = Vector2(width, 43)Updates control.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value. -
control.pressed.connect(action)Links a signal to the function that should run when the event happens. -
add_child(control)Adds the new node to the scene tree so that Godot can run and draw it. -
return controlFinishes the function and hands its result back to the caller. The expression reads control. -
func base(colour: Color = Color("#193652")) -> void:Defines base, 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. -
draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))Paints a rectangle at the given position and size. -
draw_rect(Rect2(Vector2(22, 20), size - Vector2(44, 40)), colour)Paints a rectangle at the given position and size.
Find and fix a bug
If a paused app responds to a tap, check that both practice actions test paused before changing state.
Try a change
Ages 10–12 can add an on-screen resume reminder. Ages 13–16 can test a partially fulfilled two-item order across an interrupted app session.