Market Day London · Lesson 36 of 50 · Intermediate to advanced · About 35 min
Nia should remember her fastest shift after a player closes 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.


Build it step by step
Language and tools in this lesson ConfigFile, get_value, set_value, has_section_key, user://.
-
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.
-
Choose a best result
Press Clear lesson save, then record 16, 13 and 16. Compare the fastest result after all three.
best = turns if best < 0 else mini(best, turns)Why it works mini keeps the lower of two valid turn counts. A negative value stands for no saved record.
-
Write a simple record
Find the ConfigFile in finish, then compare its named group and key with the read function.
file.set_value("shift", "best", best)Why it works The file stores a readable shift section and a best field. The write and read code agree on both names.
-
Use the writable folder
Read SAVE_PATH and compare its prefix with res://. In Godot, open Project > Open User Data Folder and find the lesson file after a recorded score.
const SAVE_PATH := "user://market_lesson36_badge.cfg"Why it works A game may be installed in a read-only folder. user:// points to a location Godot provides for this player's local data.
-
Handle a missing or invalid file
Clear the lesson save, then press Load saved badge. Follow the missing-file branch.
if file.load(SAVE_PATH) != OK or not file.has_section_key("shift", "best"):Why it works A fresh install has no data yet. The test works whether the file is absent or the expected field is absent.
-
Test a full restart
Record 13, stop with F8 and run again. Check that Loaded still shows 13 turns and Gold.
best = int(value) if value is int and int(value) > 0 else -1Why it works Variables reset when the scene closes. ConfigFile restores the saved value when a new scene starts.
Watch the values change
-
Clear No valid best is saved.
What you see. The status shows No badge yet. -
Record 16 then 13 13 replaces 16 and is within target.
What you see. Gold is shown at 13 turns. -
Close and reopen ConfigFile reads 13 from user data.
What you see. Gold returns without replaying the shift.
Predict before you run Will finishing later in 16 turns wipe a saved score of 13?
Reveal the answer
No. The comparison keeps the smaller value.
Complete checkpoint
The fastest saved result survives closing and reopening. Clear lesson save resets this practice file only.
main.gd
extends Control
const SAVE_PATH := "user://market_lesson36_badge.cfg"
const TARGET := 14
var best := -1
var status: Label
func _ready() -> void:
heading("Keep a gold badge between sessions", "LOCAL SAVE")
label("Shift target: 14 turns", Vector2(44, 128), 19, MINT)
status = label("", Vector2(44, 376), 25)
button("Record 16 turns", Vector2(44, 205), finish.bind(16), 188)
button("Record 13 turns", Vector2(246, 205), finish.bind(13), 188)
button("Load saved badge", Vector2(44, 270), read_save, 188)
button("Clear lesson save", Vector2(246, 270), reset_lab, 188)
read_save()
func finish(turns: int) -> void:
best = turns if best < 0 else mini(best, turns)
var file := ConfigFile.new()
file.set_value("shift", "best", best)
var code := file.save(SAVE_PATH)
show_result("Saved" if code == OK else "Could not save file")
func read_save() -> void:
var file := ConfigFile.new()
if file.load(SAVE_PATH) != OK or not file.has_section_key("shift", "best"):
best = -1
else:
var value: Variant = file.get_value("shift", "best")
best = int(value) if value is int and int(value) > 0 else -1
show_result("Loaded")
func show_result(action: String) -> void:
var value := "No badge yet" if best < 0 else "%d turns • %s" % [best, "Gold" if best <= TARGET else "Finished"]
status.text = action + " — " + value
func reset_lab() -> void:
if FileAccess.file_exists(SAVE_PATH):
var code := DirAccess.remove_absolute(SAVE_PATH)
if code != OK:
show_result("Could not clear save")
return
read_save()
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_lesson36_badge.cfg"user:// is Godot’s writable storage for the app. Use the same filename when saving and loading this lesson. -
const TARGET := 14Creates a constant named TARGET. The expression uses the whole number 14. Godot infers the type from the first value. -
var best := -1Use -1 to mean that no positive turn count has been recorded. -
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 a gold badge between sessions", "LOCAL SAVE")Calls heading with the values in its parentheses. -
label("Shift target: 14 turns", Vector2(44, 128), 19, MINT)Calls label with the values in its parentheses. -
status = label("", Vector2(44, 376), 25)Stores the new value in status. The expression calls label with the values in its parentheses. -
button("Record 16 turns", Vector2(44, 205), finish.bind(16), 188)Calls button with the values in its parentheses. -
button("Record 13 turns", Vector2(246, 205), finish.bind(13), 188)Calls button with the values in its parentheses. -
button("Load saved badge", Vector2(44, 270), read_save, 188)Calls button with the values in its parentheses. -
button("Clear lesson save", Vector2(246, 270), reset_lab, 188)Calls button with the values in its parentheses. -
read_save()Calls read_save with the values in its parentheses. -
func finish(turns: int) -> void:Defines finish, 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. -
best = turns if best < 0 else mini(best, turns)The first result becomes best. On later runs mini() preserves the smaller turn count. -
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", "best", best)Store the score under the best key in the shift section of the ConfigFile. -
var code := file.save(SAVE_PATH)Capture Godot’s return code so the label can report whether writing succeeded. -
show_result("Saved" if code == OK else "Could not save file")Calls show_result with the values in its parentheses. -
func read_save() -> void:Defines read save, 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 or not file.has_section_key("shift", "best"):Check both that the file loaded and that the named entry exists. A first run can have an empty storage folder. -
best = -1Stores the new value in best. The expression reads -1. -
else:Runs the indented fallback instructions if none of the earlier tests passed. -
var value: Variant = file.get_value("shift", "best")A file entry can be a different type from the type the project wrote. Variant lets the next line inspect it safely. -
best = int(value) if value is int and int(value) > 0 else -1A usable best result is an integer greater than zero. Other saved values return to the empty status. -
show_result("Loaded")Calls show_result with the values in its parentheses. -
func show_result(action: String) -> void:Defines show result, 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 value := "No badge yet" if best < 0 else "%d turns • %s" % [best, "Gold" if best <= TARGET else "Finished"]The saved result earns Gold when its turn count is at or below the target. The exact target counts. -
status.text = action + " — " + valueUpdates status.text, which controls the words a player sees. The expression calculates a new value from the expressions on either side of the operator. -
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. -
if FileAccess.file_exists(SAVE_PATH):Look up this exact practice file before removing it. The reset can run twice without a missing-file error. -
var code := DirAccess.remove_absolute(SAVE_PATH)Creates a variable named code. The expression calls DirAccess.remove_absolute with the values in its parentheses. Godot infers the type from the first value. -
if code != OK:Checks code != OK. The indented instructions below run only if this test passes. -
show_result("Could not clear save")Calls show_result with the values in its parentheses. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
read_save()Read the result again after clearing. It displays the current state of the storage. -
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 restart displays No badge yet, check that writes and reads use exactly the same path, section and key.
Try a change
Ages 10–12 can change the target and test a boundary. Ages 13–16 can keep separate keys for two shifts and test both after a restart.