London Market 38 · Pause a shift and bring it back

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.

Watercolour of a dry London market lane lined with bread, produce and flower stalls.
What you’ll make Save a small practice state and use the same pause logic for a button or lifecycle events.

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.

Pause. Guard order actions. → Save. Keep basket and delivered count. → Resume. Check and restore saved data. → Continue. Accept order actions again.
Pause → Save → Resume → Continue
Godot checkpoint window for Pause a shift and bring it back
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson _notification, NOTIFICATION_APPLICATION_PAUSED, ConfigFile, String, bool.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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

  1. Pick up bread held becomes Bread.
    What you see. The basket shows Bread.
  2. Pause and try handover paused is true so no delivery changes.
    What you see. The basket stays full.
  3. 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
  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 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.
  3. var deliveries := 0Creates a variable named deliveries. The expression uses the whole number 0. Godot infers the type from the first value.
  4. var held := "Empty"This small practice basket is represented by one word, either Empty or Bread.
  5. var paused := falseCreates a variable named paused. The expression uses the yes-or-no value false. Godot infers the type from the first value.
  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("Pause a shift and bring it back", "APP LIFECYCLE")Calls heading with the values in its parentheses.
  9. label("Simulate leaving and reopening one stage of a market order.", Vector2(44, 130), 18, MINT)Calls label with the values in its parentheses.
  10. status = label("", Vector2(44, 385), 22)Stores the new value in status. The expression calls label with the values in its parentheses.
  11. button("Pick up bread", Vector2(44, 215), pick_up, 176)Calls button with the values in its parentheses.
  12. button("Hand over bread", Vector2(233, 215), hand_over, 176)Calls button with the values in its parentheses.
  13. button("Pause shift", Vector2(44, 280), pause_shift, 176)Calls button with the values in its parentheses.
  14. button("Resume shift", Vector2(233, 280), resume_shift, 176)Calls button with the values in its parentheses.
  15. button("Reset practice", Vector2(422, 280), reset_lab, 176)Calls button with the values in its parentheses.
  16. show_state()Calls show_state with the values in its parentheses.
  17. 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.
  18. if paused or held != "Empty":Accept bread only while playing with an empty basket. Returning here leaves the saved delivery unchanged.
  19. returnFinishes the function right here. Any later instructions in this function are skipped.
  20. held = "Bread"Stores the new value in held. The expression uses the text inside the quotes.
  21. show_state()Calls show_state with the values in its parentheses.
  22. 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.
  23. if paused or held != "Bread":The handover needs an active shift and bread in the basket.
  24. returnFinishes the function right here. Any later instructions in this function are skipped.
  25. held = "Empty"Stores the new value in held. The expression uses the text inside the quotes.
  26. deliveries += 1Increment the delivery count only after the valid handover empties the basket.
  27. show_state()Calls show_state with the values in its parentheses.
  28. 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.
  29. paused = trueSet paused first so another action cannot change the state being saved.
  30. var file := ConfigFile.new()Creates a variable named file. The expression creates a new ConfigFile object. Godot infers the type from the first value.
  31. file.set_value("shift", "deliveries", deliveries)Calls file.set_value with the values in its parentheses.
  32. file.set_value("shift", "held", held)Save what Nia is carrying along with the delivery count so resume can reconstruct both values.
  33. 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.
  34. show_state(" • Save failed" if error != OK else "")Report a save failure alongside the status, where the player can read it immediately.
  35. 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.
  36. var file := ConfigFile.new()Creates a variable named file. The expression creates a new ConfigFile object. Godot infers the type from the first value.
  37. if file.load(SAVE_PATH) == OK:Apply saved values only after Godot loads the practice file successfully.
  38. 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.
  39. if saved_item == "Empty" or saved_item == "Bread":Allow the two values this basket understands. Unexpected entries are skipped.
  40. held = str(saved_item)Stores the new value in held. The expression turns a value into text for a player message.
  41. 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.
  42. if saved_count is int:Checks saved_count is int. The indented instructions below run only if this test passes.
  43. deliveries = maxi(0, int(saved_count))Use zero as the minimum number of completed deliveries.
  44. paused = falseUpdates paused, which controls whether the scene tree is paused. The expression uses the yes-or-no value false.
  45. show_state()Calls show_state with the values in its parentheses.
  46. 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.
  47. deliveries = 0Stores the new value in deliveries. The expression uses the whole number 0.
  48. held = "Empty"Stores the new value in held. The expression uses the text inside the quotes.
  49. pause_shift()Pausing writes the cleared practice state and resuming reopens the just-saved values.
  50. resume_shift()Calls resume_shift with the values in its parentheses.
  51. 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.
  52. 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.
  53. func _notification(what: int) -> void:Godot calls this for engine notices, such as the app going into the background.
  54. if not is_node_ready():Some engine notifications can arrive during setup. Wait until the node is ready before changing the status label.
  55. returnFinishes the function right here. Any later instructions in this function are skipped.
  56. 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.
  57. pause_shift()Calls pause_shift with the values in its parentheses.
  58. elif what == NOTIFICATION_APPLICATION_RESUMED:When the app becomes active again, call the same load function used by the resume button.
  59. resume_shift()Calls resume_shift with the values in its parentheses.
  60. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  61. base()Calls base with the values in its parentheses.
  62. 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.
  63. 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.
  64. 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.
  65. 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.
  66. 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.
  67. label("MARKET DAY LONDON / " + topic, Vector2(44, 36), 15, GOLD)Calls label with the values in its parentheses.
  68. label(caption, Vector2(44, 66), 31)Calls label with the values in its parentheses.
  69. 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.
  70. var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value.
  71. item.text = wordsUpdates item.text, which controls the words a player sees. The expression reads words.
  72. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  73. item.add_theme_font_size_override("font_size", pixels)Calls item.add_theme_font_size_override with the values in its parentheses.
  74. item.add_theme_color_override("font_color", ink)Calls item.add_theme_color_override with the values in its parentheses.
  75. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  76. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  77. 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.
  78. var control := Button.new()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value.
  79. control.text = wordsUpdates control.text, which controls the words a player sees. The expression reads words.
  80. control.position = atUpdates control.position, which controls the node's local x and y location. The expression reads at.
  81. 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.
  82. control.pressed.connect(action)Links a signal to the function that should run when the event happens.
  83. add_child(control)Adds the new node to the scene tree so that Godot can run and draw it.
  84. return controlFinishes the function and hands its result back to the caller. The expression reads control.
  85. 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.
  86. draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))Paints a rectangle at the given position and size.
  87. 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.