Tiny Shelf 36 · Keep a scrapbook stamp

Tiny Shelf Studio · Lesson 36 of 50 · Intermediate to advanced · About 35 min

A completed display earns a scrapbook stamp. It should be waiting when the player comes back.

Miniature packaging workshop with coloured paper models, tools and a teal cutting mat.
What you’ll make Save an earned stamp using ConfigFile and restore it after closing the game.

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

Name the stamp. Use a stable save key. → Write once. Save the earned Boolean. → Check the error. Report only real success. → Read back. Restore the badge state.
Name the stamp → Write once → Check the error → Read back
Godot checkpoint window for Keep a scrapbook stamp
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson ConfigFile.new, set_value, get_value, Error, OK, user://.

  1. Open the design bench

    Import this checkpoint's project.godot into Godot 4.7.2. Open main.gd from FileSystem. Press F5 for the starting view and F8 to stop it. Keep a copy to undo experiments.

    Why it works The checkpoint scene attaches main.gd to a Control. Each test starts from that known version, so compare one change with the original.

  2. Choose a path for personal progress

    Find SAVE_PATH and follow the path into save and load. Godot creates it in this checkpoint’s user data directory.

    const SAVE_PATH := "user://tiny_studio_unit_36.cfg"

    Why it works user:// is the writable place for the player’s files. The checkpoint has its own custom user data folder.

  3. Store a value with two names

    Press Reset practice and then Finish display. Follow set_value and work out which section and key identify the stamp.

    book.set_value("scrapbook", STAMP, true)

    Why it works The section is scrapbook and the stamp ID is first_display. The value is the Boolean true.

  4. Check whether saving succeeded

    Find the value returned by save. Follow its success branch before changing the stamp status.

    var result := book.save(SAVE_PATH)

    Why it works OK means the file was written. A failed write gets an error message instead of a false promise that the badge is stored.

  5. Read with a safe default

    Click Read badge or close and reopen the checkpoint. Compare the Boolean read with the default used for a missing key.

    earned = book.get_value("scrapbook", STAMP, false) == true

    Why it works The third get_value argument is used when the key does not exist. Equality to true keeps only the exact earned value.

  6. Try the first-run flow

    Click Reset practice. Look for STAMP WAITING. Click Finish display twice and read the same stamp again.

    elif result == ERR_FILE_NOT_FOUND:

    Why it works The missing file is a normal first run. Updating the same key avoids stamping it twice.

Watch the values change

  1. Start with a fresh checkpoint load reports ERR_FILE_NOT_FOUND.
    What you see. The badge section says STAMP WAITING.
  2. Finish a display save succeeds and earned becomes true.
    What you see. The gold outline appears and the message confirms the save.
  3. Reopen load reads the saved true value.
    What you see. STAMP EARNED stays visible.

Predict before you run If the file does not have the first_display key, which badge state is shown?

Reveal the answer

The default is false, so the badge displays STAMP WAITING.

Complete checkpoint

A one-stamp scrapbook that retains an earned badge after quitting and that can be reset in the practice app.

main.gd

extends Control

const SAVE_PATH := "user://tiny_studio_unit_36.cfg"
const STAMP := "first_display"
var earned := false
var status: Label
var notice: Label

func _ready() -> void:
	title_page("SCRAPBOOK", "Keep a scrapbook stamp", "Finish a practice board, then read the saved badge back.")
	status = label("", Vector2(56, 218), 31, GOLD)
	notice = label("", Vector2(57, 273), 18)
	place_button("Finish display", Vector2(56, 409), award)
	place_button("Read badge", Vector2(242, 409), read_scrapbook)
	place_button("Reset practice", Vector2(428, 409), reset_practice)
	read_scrapbook()

func award() -> void:
	var book := ConfigFile.new()
	book.set_value("scrapbook", "format", 1)
	book.set_value("scrapbook", STAMP, true)
	var result := book.save(SAVE_PATH)
	if result == OK:
		earned = true
		update_status("Badge saved on this device.")
	else:
		update_status("Could not save. Code %d." % result)

func read_scrapbook() -> void:
	var book := ConfigFile.new()
	var result := book.load(SAVE_PATH)
	if result == OK:
		earned = book.get_value("scrapbook", STAMP, false) == true
		update_status("Read the badge from the scrapbook file.")
	elif result == ERR_FILE_NOT_FOUND:
		earned = false
		update_status("There is no scrapbook file yet.")
	else:
		update_status("Could not read the scrapbook file.")

func reset_practice() -> void:
	if FileAccess.file_exists(SAVE_PATH):
		DirAccess.remove_absolute(SAVE_PATH)
	read_scrapbook()

func update_status(note: String) -> void:
	status.text = "STAMP EARNED" if earned else "STAMP WAITING"
	notice.text = note
	queue_redraw()

func _draw() -> void:
	backdrop()
	var outline := GOLD if earned else EDGE
	panel(Rect2(43, 168, 860, 193), Color("#244457"), outline)

const PAPER := Color("#fff4de")
const GOLD := Color("#f5bd79")
const SEA := Color("#9ddcd0")
const INK := Color("#172c3c")
const EDGE := Color("#54817f")

func title_page(unit: String, heading: String, subtitle: String) -> void:
	label("TINY SHELF STUDIO  /  " + unit, Vector2(44, 36), 14, GOLD)
	label(heading, Vector2(44, 65), 33)
	label(subtitle, Vector2(46, 119), 16, SEA)

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 place_button(words: String, at: Vector2, action: Callable, width: float = 170.0) -> Button:
	var control := Button.new()
	control.text = words
	control.position = at
	control.custom_minimum_size = Vector2(width, 48)
	control.add_theme_font_size_override("font_size", 17)
	control.add_theme_color_override("font_color", PAPER)
	control.add_theme_stylebox_override("normal", style(Color("#244557"), EDGE))
	control.add_theme_stylebox_override("hover", style(Color("#365a62"), SEA))
	control.add_theme_stylebox_override("pressed", style(Color("#315b56"), SEA))
	control.add_theme_stylebox_override("focus", style(Color.TRANSPARENT, GOLD, 3))
	control.pressed.connect(action)
	add_child(control)
	return control

func style(fill: Color, edge: Color, line: int = 1) -> StyleBoxFlat:
	var block := StyleBoxFlat.new()
	block.bg_color = fill
	block.border_color = edge
	block.set_border_width_all(line)
	block.set_corner_radius_all(8)
	return block

func panel(rect: Rect2, fill: Color, edge: Color = EDGE) -> void:
	draw_style_box(style(fill, edge), rect)

func backdrop() -> void:
	draw_rect(Rect2(Vector2.ZERO, size), Color("#102333"))
	panel(Rect2(22, 19, size.x - 44, size.y - 38), Color("#183344"), Color("#325665"))

func paint_words(words: String, at: Vector2, pixels: int = 16, ink: Color = PAPER) -> void:
	draw_string(ThemeDB.fallback_font, at, words, HORIZONTAL_ALIGNMENT_LEFT, -1, pixels, 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 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://tiny_studio_unit_36.cfg"This user directory belongs to the lesson. Keep the same path for writes, reads and the practice reset.
  3. const STAMP := "first_display"A stable key stands for the badge even if the display title changes.
  4. var earned := falseCreates a variable named earned. The expression uses the yes-or-no value false. Godot infers the type from the first value.
  5. var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here.
  6. var notice: LabelDeclares notice 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. title_page("SCRAPBOOK", "Keep a scrapbook stamp", "Finish a practice board, then read the saved badge back.")Calls title_page with the values in its parentheses.
  9. status = label("", Vector2(56, 218), 31, GOLD)Stores the new value in status. The expression calls label with the values in its parentheses.
  10. notice = label("", Vector2(57, 273), 18)Stores the new value in notice. The expression calls label with the values in its parentheses.
  11. place_button("Finish display", Vector2(56, 409), award)Calls place_button with the values in its parentheses.
  12. place_button("Read badge", Vector2(242, 409), read_scrapbook)Calls place_button with the values in its parentheses.
  13. place_button("Reset practice", Vector2(428, 409), reset_practice)Calls place_button with the values in its parentheses.
  14. read_scrapbook()Calls read_scrapbook with the values in its parentheses.
  15. func award() -> void:Defines award, 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.
  16. var book := ConfigFile.new()Creates a variable named book. The expression creates a new ConfigFile object. Godot infers the type from the first value.
  17. book.set_value("scrapbook", "format", 1)Keep a version number next to the data. Lesson 37 practices upgrading that kind of record.
  18. book.set_value("scrapbook", STAMP, true)Set the earned flag under exactly one ID. A repeated completion overwrites that same flag.
  19. var result := book.save(SAVE_PATH)Write the candidate and inspect the result before confirming success.
  20. if result == OK:Checks result == OK. The indented instructions below run only if this test passes.
  21. earned = trueOnly commit the newly earned stamp in this version when the write succeeds.
  22. update_status("Badge saved on this device.")Calls update_status with the values in its parentheses.
  23. else:Runs the indented fallback instructions if none of the earlier tests passed.
  24. update_status("Could not save. Code %d." % result)Calls update_status with the values in its parentheses.
  25. func read_scrapbook() -> void:Defines read scrapbook, 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.
  26. var book := ConfigFile.new()Creates a variable named book. The expression creates a new ConfigFile object. Godot infers the type from the first value.
  27. var result := book.load(SAVE_PATH)Creates a variable named result. The expression reads a resource from the project when this instruction runs. Godot infers the type from the first value.
  28. if result == OK:Checks result == OK. The indented instructions below run only if this test passes.
  29. earned = book.get_value("scrapbook", STAMP, false) == trueA missing badge is false, and only an actual Boolean true counts as an earned badge.
  30. update_status("Read the badge from the scrapbook file.")Calls update_status with the values in its parentheses.
  31. elif result == ERR_FILE_NOT_FOUND:Treat a first run normally. The app has no saved badge until the first completion.
  32. earned = falseStores the new value in earned. The expression uses the yes-or-no value false.
  33. update_status("There is no scrapbook file yet.")Calls update_status with the values in its parentheses.
  34. else:Runs the indented fallback instructions if none of the earlier tests passed.
  35. update_status("Could not read the scrapbook file.")Calls update_status with the values in its parentheses.
  36. func reset_practice() -> void:Defines reset practice, 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.
  37. if FileAccess.file_exists(SAVE_PATH):Checks FileAccess.file_exists(SAVE_PATH). The indented instructions below run only if this test passes.
  38. DirAccess.remove_absolute(SAVE_PATH)Remove only this known practice save, after checking that it exists.
  39. read_scrapbook()Calls read_scrapbook with the values in its parentheses.
  40. func update_status(note: String) -> void:Defines update status, 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.
  41. status.text = "STAMP EARNED" if earned else "STAMP WAITING"Updates status.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle.
  42. notice.text = noteUpdates notice.text, which controls the words a player sees. The expression reads note.
  43. queue_redraw()Asks Godot to call _draw again on a later frame so the screen matches the new state.
  44. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  45. backdrop()Calls backdrop with the values in its parentheses.
  46. var outline := GOLD if earned else EDGEShow saved progress through colour and a separate visible text label.
  47. panel(Rect2(43, 168, 860, 193), Color("#244457"), outline)Calls panel with the values in its parentheses.
  48. const PAPER := Color("#fff4de")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value.
  49. const GOLD := Color("#f5bd79")Creates a constant named GOLD. The expression builds a colour from these values. Godot infers the type from the first value.
  50. const SEA := Color("#9ddcd0")Creates a constant named SEA. The expression builds a colour from these values. Godot infers the type from the first value.
  51. const INK := Color("#172c3c")Creates a constant named INK. The expression builds a colour from these values. Godot infers the type from the first value.
  52. const EDGE := Color("#54817f")Creates a constant named EDGE. The expression builds a colour from these values. Godot infers the type from the first value.
  53. func title_page(unit: String, heading: String, subtitle: String) -> void:Defines title page, 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.
  54. label("TINY SHELF STUDIO / " + unit, Vector2(44, 36), 14, GOLD)Make a small line above the main heading so you can tell which checkpoint is open.
  55. label(heading, Vector2(44, 65), 33)Calls label with the values in its parentheses.
  56. label(subtitle, Vector2(46, 119), 16, SEA)Calls label with the values in its parentheses.
  57. 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.
  58. var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value.
  59. item.text = wordsUpdates item.text, which controls the words a player sees. The expression reads words.
  60. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  61. item.add_theme_font_size_override("font_size", pixels)This Label has its own readable type size. Changing that value leaves the other labels alone.
  62. item.add_theme_color_override("font_color", ink)This label keeps the chosen text ink even when the default theme changes.
  63. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  64. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  65. func place_button(words: String, at: Vector2, action: Callable, width: float = 170.0) -> Button:Defines place 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.
  66. var control := Button.new()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value.
  67. control.text = wordsUpdates control.text, which controls the words a player sees. The expression reads words.
  68. control.position = atUpdates control.position, which controls the node's local x and y location. The expression reads at.
  69. control.custom_minimum_size = Vector2(width, 48)Give the button room for its label and an easy target for taps. Containers and layouts can allocate more space.
  70. control.add_theme_font_size_override("font_size", 17)Calls control.add_theme_font_size_override with the values in its parentheses.
  71. control.add_theme_color_override("font_color", PAPER)Calls control.add_theme_color_override with the values in its parentheses.
  72. control.add_theme_stylebox_override("normal", style(Color("#244557"), EDGE))Calls control.add_theme_stylebox_override with the values in its parentheses.
  73. control.add_theme_stylebox_override("hover", style(Color("#365a62"), SEA))Calls control.add_theme_stylebox_override with the values in its parentheses.
  74. control.add_theme_stylebox_override("pressed", style(Color("#315b56"), SEA))Calls control.add_theme_stylebox_override with the values in its parentheses.
  75. control.add_theme_stylebox_override("focus", style(Color.TRANSPARENT, GOLD, 3))Draw a gold outline around keyboard focus. Other button styles can still indicate hover and press independently.
  76. control.pressed.connect(action)pressed fires on a successful control activation. Godot calls the function passed in without running it at setup time.
  77. add_child(control)Adds the new node to the scene tree so that Godot can run and draw it.
  78. return controlFinishes the function and hands its result back to the caller. The expression reads control.
  79. func style(fill: Color, edge: Color, line: int = 1) -> StyleBoxFlat:Defines style, 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.
  80. var block := StyleBoxFlat.new()Creates a variable named block. The expression creates a new StyleBoxFlat object. Godot infers the type from the first value.
  81. block.bg_color = fillStores the new value in block.bg_color. The expression reads fill.
  82. block.border_color = edgeStores the new value in block.border_color. The expression reads edge.
  83. block.set_border_width_all(line)Apply one border line width to all four edges of the card.
  84. block.set_corner_radius_all(8)Round each corner of this reusable style by eight pixels.
  85. return blockFinishes the function and hands its result back to the caller. The expression reads block.
  86. func panel(rect: Rect2, fill: Color, edge: Color = EDGE) -> void:Defines panel, 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.
  87. draw_style_box(style(fill, edge), rect)Calls draw_style_box with the values in its parentheses.
  88. func backdrop() -> void:Defines backdrop, 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.
  89. draw_rect(Rect2(Vector2.ZERO, size), Color("#102333"))Paints a rectangle at the given position and size.
  90. panel(Rect2(22, 19, size.x - 44, size.y - 38), Color("#183344"), Color("#325665"))Subtract space on both sides of the canvas. As the window changes, the outer frame fits within it.
  91. func paint_words(words: String, at: Vector2, pixels: int = 16, ink: Color = PAPER) -> void:Defines paint words, 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.
  92. draw_string(ThemeDB.fallback_font, at, words, HORIZONTAL_ALIGNMENT_LEFT, -1, pixels, ink)Godot draws the words from the baseline at the supplied pixel position using its fallback typeface.

Find and fix a bug

If a badge resets after reopening, check that save and load use the same path, section and key. Print the error code before assuming the file exists.

Try a change

Create a second stamp with another stable key. Read both and show the number collected.