Midnight Deck Dash · Lesson 36 of 50 · Intermediate to advanced · About 36 min
A passenger can close the game before dinner and continue their delivery route afterwards.

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 FileAccess, user://, JSON.stringify, JSON.parse, Variant, Dictionary.get.
-
Open the editable checkpoint
In Godot Project Manager, choose Import, select this lesson's project.godot and click Import & Edit. Open main.gd from FileSystem and press F5 to see the finished checkpoint. Press F8 before trying a change.
Why it works The lesson scene already has a Control root with this script attached.
-
Choose the device save location
Read SAVE. In Godot use Project > Open User Data Folder to see where this lesson stores files. Avoid editing the main game's save.
const SAVE := "user://midnight_deck_lesson36.json"Why it works user:// points to a project writable folder. This checkpoint uses its own practice filename.
-
Choose the saved values
Press Complete a job and Toggle mute. In save_progress find the Dictionary with two named keys.
file.store_string(JSON.stringify({"best_job": best_job, "muted": muted}))Why it works The file only needs values that matter across sessions. A Dictionary connects each key to its current value.
-
Open the file for writing
Press Save and look for Saved. Read the guard after FileAccess.open.
var file := FileAccess.open(SAVE, FileAccess.WRITE)Why it works FileAccess.WRITE opens a file for replacement. If there is no FileAccess object, the function returns before writing.
-
Read and check the structure
Change the values again and press Load. Follow parser.parse and the type test before assigning player data.
if parser.parse(file.get_as_text()) != OK or not parser.data is Dictionary:Why it works JSON starts as text. Parsing turns it into a value. Checking its shape keeps damaged files from changing the live game.
-
Restore the validated progress
Compare the restored number and mute status. Restart using F8 and F5, then press Load again.
best_job = int(job)Why it works int converts a validated whole-number value to the type used by best_job. Assignments update the live state only after every guard passed.
Watch the values change
-
Save best job 2 and muted true JSON records both values in the practice file.
What you see. Saved appears. -
Change to best job 3 and muted false The in-memory values change. The file stays as it was.
What you see. In memory appears. -
Load Valid JSON supplies the earlier values.
What you see. The screen returns to job 2 and muted true.
Predict before you run If you toggle mute after saving but before closing, which value loads on a fresh run?
Reveal the answer
The saved setting loads. Toggling a value changes memory until the program writes it to a file.
Complete checkpoint
The standalone practice checkpoint saves, validates and restores job progress and the mute setting.
main.gd
extends Control
const SAVE := "user://midnight_deck_lesson36.json"
var best_job := 1
var muted := false
var status: Label
func _ready() -> void:
label("Midnight Deck Dash / Save progress", 28)
status = label("", 88)
button("Complete a job", 53, 168, complete_one)
button("Toggle mute", 282, 168, toggle_mute)
button("Save", 53, 244, save_progress)
button("Load", 282, 244, load_progress)
show_status("In memory")
func complete_one() -> void:
best_job = mini(best_job + 1, 9)
show_status("In memory")
func toggle_mute() -> void:
muted = not muted
show_status("In memory")
func show_status(action: String) -> void:
status.text = "%s • Best job %d • Muted: %s" % [action, best_job, str(muted)]
func save_progress() -> void:
var file := FileAccess.open(SAVE, FileAccess.WRITE)
if file == null:
show_status("Could not save")
return
file.store_string(JSON.stringify({"best_job": best_job, "muted": muted}))
file.close()
show_status("Saved")
func load_progress() -> void:
var file := FileAccess.open(SAVE, FileAccess.READ)
if file == null:
show_status("No practice save")
return
var parser := JSON.new()
if parser.parse(file.get_as_text()) != OK or not parser.data is Dictionary:
show_status("Unreadable practice save")
return
var data: Dictionary = parser.data
var job: Variant = data.get("best_job", 1)
if not (job is float or job is int) or not data.get("muted", false) is bool:
show_status("Invalid data")
return
if float(job) != floorf(float(job)) or job < 1 or job > 9:
show_status("Invalid job")
return
best_job = int(job)
muted = data.get("muted", false)
show_status("Loaded")
func label(text: String, y: float, font_size: int = 25) -> Label:
var new_label := Label.new()
new_label.text = text
new_label.position = Vector2(52, y)
new_label.add_theme_font_size_override("font_size", font_size)
add_child(new_label)
return new_label
func button(text: String, x: float, y: float, action: Callable, width: float = 214.0) -> Button:
var control := Button.new()
control.text = text
control.position = Vector2(x, y)
control.custom_minimum_size = Vector2(width, 48)
add_child(control)
control.pressed.connect(action)
return control
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#112632"))
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 ControlAttach this script to a Control so the checkpoint can draw, size itself, place buttons and respond to input. -
const SAVE := "user://midnight_deck_lesson36.json"Creates a constant named SAVE. The expression uses the text inside the quotes. Godot infers the type from the first value. -
var best_job := 1Creates a variable named best_job. The expression uses the whole number 1. Godot infers the type from the first value. -
var muted := falseCreates a variable named muted. 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 setup function when the scene enters the running tree. void means the function gives the caller no value back. -
label("Midnight Deck Dash / Save progress", 28)Calls label with the values in its parentheses. -
status = label("", 88)Stores the new value in status. The expression calls label with the values in its parentheses. -
button("Complete a job", 53, 168, complete_one)Calls button with the values in its parentheses. -
button("Toggle mute", 282, 168, toggle_mute)Calls button with the values in its parentheses. -
button("Save", 53, 244, save_progress)Calls button with the values in its parentheses. -
button("Load", 282, 244, load_progress)Calls button with the values in its parentheses. -
show_status("In memory")Calls show_status with the values in its parentheses. -
func complete_one() -> void:Defines complete one, 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_job = mini(best_job + 1, 9)Stores the new value in best_job. The expression calls mini with the values in its parentheses. -
show_status("In memory")Calls show_status with the values in its parentheses. -
func toggle_mute() -> void:Defines toggle mute, 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. -
muted = not mutedStores the new value in muted. The expression reads not muted. -
show_status("In memory")Calls show_status with the values in its parentheses. -
func show_status(action: String) -> void:Defines show 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. -
status.text = "%s • Best job %d • Muted: %s" % [action, best_job, str(muted)]Updates status.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func save_progress() -> void:Defines save progress, 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 := FileAccess.open(SAVE, FileAccess.WRITE)Creates a variable named file. The expression tries to open a file and returns a handle, or null if opening fails. Godot infers the type from the first value. -
if file == null:Checks file == null. The indented instructions below run only if this test passes. -
show_status("Could not save")Calls show_status with the values in its parentheses. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
file.store_string(JSON.stringify({"best_job": best_job, "muted": muted}))Give the two real state values readable key names, convert the Dictionary to JSON text and write that text into the open practice file. -
file.close()Release the file after writing. The next load can open it separately for reading. -
show_status("Saved")Calls show_status with the values in its parentheses. -
func load_progress() -> void:Defines load progress, 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 := FileAccess.open(SAVE, FileAccess.READ)Creates a variable named file. The expression tries to open a file and returns a handle, or null if opening fails. Godot infers the type from the first value. -
if file == null:Checks file == null. The indented instructions below run only if this test passes. -
show_status("No practice save")Calls show_status with the values in its parentheses. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
var parser := JSON.new()Create a parser that can report whether the JSON text is valid before giving its parsed data. -
if parser.parse(file.get_as_text()) != OK or not parser.data is Dictionary:Checks parser.parse(file.get_as_text()) != OK or not parser.data is Dictionary. The indented instructions below run only if this test passes. -
show_status("Unreadable practice save")Calls show_status with the values in its parentheses. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
var data: Dictionary = parser.dataAfter the guard accepts the top-level Dictionary, use it as a state record with named keys. -
var job: Variant = data.get("best_job", 1)Look for the saved progress key and read 1 if it is absent. Variant lets the guard check the type of data loaded from disk. -
if not (job is float or job is int) or not data.get("muted", false) is bool:Checks not (job is float or job is int) or not data.get("muted", false) is bool. The indented instructions below run only if this test passes. -
show_status("Invalid data")Calls show_status with the values in its parentheses. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
if float(job) != floorf(float(job)) or job < 1 or job > 9:Reject decimal and out-of-range values before the game uses them to unlock a job. -
show_status("Invalid job")Calls show_status with the values in its parentheses. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
best_job = int(job)Convert the checked stored number to the whole-number type the live game uses. -
muted = data.get("muted", false)Stores the new value in muted. The expression looks up a named or indexed value, using a fallback when the call gives one. -
show_status("Loaded")Calls show_status with the values in its parentheses. -
func label(text: String, y: float, font_size: int = 25) -> 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 new_label := Label.new()Create a Godot node for visible text. The colon and equals together ask GDScript to infer the type from Label.new(). -
new_label.text = textCopy the supplied string into the actual label so the learner can read it. -
new_label.position = Vector2(52, y)Use a fixed 52-pixel left margin and the caller's chosen height in local design pixels. -
new_label.add_theme_font_size_override("font_size", font_size)Give this label the caller's text size. Other labels can keep their own sizes. -
add_child(new_label)Attach the Label to the scene root so Godot can draw and manage it. -
return new_labelGive the caller this particular label so a later delivery or move can change its words. -
func button(text: String, x: float, y: float, action: Callable, width: float = 214.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()Create a GUI button. It can respond to pointer taps and to the keyboard when focused. -
control.text = textShow the caller's chosen action name on the new button. -
control.position = Vector2(x, y)Place the action at the coordinates passed by the caller. -
control.custom_minimum_size = Vector2(width, 48)Ask the Control layout for at least the supplied width and 48 design pixels of height. -
add_child(control)Connect the new button to the visible scene root. -
control.pressed.connect(action)Connect the Godot pressed signal to the Callable received as action. The action runs on a press later. -
return controlReturn this button so the caller can customise it or keep its reference. -
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("#112632"))Draw the full dark blue practice backdrop from local (0, 0) to the current size. Shapes drawn later can appear above it.
Find and fix a bug
If loading displays an unexpected value, check the last Saved status and the exact key spelling. best_job and muted must match on both sides.
Try a change
Ages 10–12 can save and restore three different pairs of values. Ages 13–16 can store a palette setting and check that an older save without that key gets the intended default.