Midnight Deck Dash · Lesson 37 of 50 · Intermediate to advanced · About 36 min
An updated course game introduces sound preferences. Players need a way to keep progress from the earlier version.

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/36 when the lesson builds on prior work, or copy the checkpoints/37 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 version field, JSON.parse, match, Dictionary.get, atomic state assignment.
-
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.
-
Test a known older save
Run and press Load old format. Compare its JSON string in _ready with the screen after loading.
button("Load old format", 52, 149, load_text.bind('{"version":1,"job":4}'), 165)Why it works The old record uses job rather than highest_job. Keeping the version beside the progress tells the loader which key to read.
-
Choose the correct conversion
Find match and the 1.0 branch. Press Load new format and trace the 2.0 branch.
match data.get("version"):Why it works match selects the matching schema. Godot's JSON number is read as a float, so these branches match 1.0 and 2.0.
-
Set a safe old-version default
Read the quiet value in the 1.0 branch. Reload the old format after the new format and check Quiet.
migrated = {"version": VERSION, "highest_job": data.get("job"), "quiet": false}Why it works The old schema had no quiet setting. The migration deliberately supplies false so every version has the new key.
-
Keep checks above state changes
Press Try damaged save. Then press Try future format. In the script follow each return and confirm it happens before current changes.
result.text = "Unknown version. Keeping current game."Why it works A malformed or unknown format could be incomplete. Early returns preserve the current playable state.
-
Assign only the finished state
Compare current to migrated. Check that the validated job is converted to an integer before assigning current.
current = {"version": VERSION, "highest_job": int(job), "quiet": quiet}Why it works One Dictionary assignment replaces the state after all validation. A partial bad record cannot overwrite an individual field.
Watch the values change
-
Load older format The job key becomes highest_job and quiet defaults to false.
What you see. Job 4 appears. -
Load current format The version 2 keys are validated.
What you see. Job 6 and quiet true appear. -
Load damaged or future format The loader returns before assigning current.
What you see. Job 6 remains playable.
Predict before you run What remains on screen if you load the new format, then try the damaged sample?
Reveal the answer
Job 6 remains with Quiet true. The message reports the damaged save.
Complete checkpoint
The old and new formats load successfully. Malformed and unknown formats display a message and preserve active data.
main.gd
extends Control
const VERSION := 2
var current := {"version": VERSION, "highest_job": 1, "quiet": false}
var status: Label
var result: Label
func _ready() -> void:
label("Midnight Deck Dash / Save recovery", 29)
status = label("", 82)
result = label("The game is ready.", 230, 20)
button("Load old format", 52, 149, load_text.bind('{"version":1,"job":4}'), 165)
button("Load new format", 230, 149, load_text.bind('{"version":2,"highest_job":6,"quiet":true}'), 165)
button("Try damaged save", 410, 149, load_text.bind('{"version":'), 180)
button("Try future format", 605, 149, load_text.bind('{"version":9,"job":8}'), 190)
render()
func load_text(raw: String) -> void:
var parsed := JSON.new()
if parsed.parse(raw) != OK or not parsed.data is Dictionary:
result.text = "Damaged save. Keeping current game."
return
var data: Dictionary = parsed.data
var migrated: Dictionary = {}
match data.get("version"):
1.0:
migrated = {"version": VERSION, "highest_job": data.get("job"), "quiet": false}
2.0:
migrated = data
_:
result.text = "Unknown version. Keeping current game."
return
var job: Variant = migrated.get("highest_job")
var quiet: Variant = migrated.get("quiet")
if not (job is float or job is int) or not quiet is bool:
result.text = "Unexpected type. Keeping current game."
return
if float(job) != floorf(float(job)) or job < 1 or job > 9:
result.text = "Invalid job. Keeping current game."
return
current = {"version": VERSION, "highest_job": int(job), "quiet": quiet}
result.text = "Loaded and validated as version 2."
render()
func render() -> void:
status.text = "Highest job: %d • Quiet: %s" % [current["highest_job"], str(current["quiet"])]
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 VERSION := 2Creates a constant named VERSION. The expression uses the whole number 2. Godot infers the type from the first value. -
var current := {"version": VERSION, "highest_job": 1, "quiet": false}Creates a variable named current. The expression makes a dictionary that looks up values by key. 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. -
var result: LabelDeclares result 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 recovery", 29)Calls label with the values in its parentheses. -
status = label("", 82)Stores the new value in status. The expression calls label with the values in its parentheses. -
result = label("The game is ready.", 230, 20)Stores the new value in result. The expression calls label with the values in its parentheses. -
button("Load old format", 52, 149, load_text.bind('{"version":1,"job":4}'), 165)Calls button with the values in its parentheses. -
button("Load new format", 230, 149, load_text.bind('{"version":2,"highest_job":6,"quiet":true}'), 165)Calls button with the values in its parentheses. -
button("Try damaged save", 410, 149, load_text.bind('{"version":'), 180)Calls button with the values in its parentheses. -
button("Try future format", 605, 149, load_text.bind('{"version":9,"job":8}'), 190)Calls button with the values in its parentheses. -
render()Calls render with the values in its parentheses. -
func load_text(raw: String) -> void:Defines load text, 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 parsed := JSON.new()Creates a variable named parsed. The expression creates a new JSON object. Godot infers the type from the first value. -
if parsed.parse(raw) != OK or not parsed.data is Dictionary:Checks parsed.parse(raw) != OK or not parsed.data is Dictionary. The indented instructions below run only if this test passes. -
result.text = "Damaged save. Keeping current game."Updates result.text, which controls the words a player sees. The expression uses the text inside the quotes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
var data: Dictionary = parsed.dataCreates a variable named data. The expression reads parsed.data. The type after the colon says what kind of value it can hold. -
var migrated: Dictionary = {}Creates a variable named migrated. The expression makes a dictionary that looks up values by key. The type after the colon says what kind of value it can hold. -
match data.get("version"):Choose a migration branch from the parsed number. Missing and unsupported versions reach the underscore branch. -
1.0:Starts the instructions for this branch. The indented lines belong together. -
migrated = {"version": VERSION, "highest_job": data.get("job"), "quiet": false}Translate a version 1 record's job key to the modern name and add a sound default. -
2.0:Starts the instructions for this branch. The indented lines belong together. -
migrated = dataUse the version 2 fields as provided. The shared checks still have to accept them before the live game changes. -
_:Handles every match value that the branches above did not handle. -
result.text = "Unknown version. Keeping current game."Updates result.text, which controls the words a player sees. The expression uses the text inside the quotes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
var job: Variant = migrated.get("highest_job")Creates a variable named job. The expression looks up a named or indexed value, using a fallback when the call gives one. The type after the colon says what kind of value it can hold. -
var quiet: Variant = migrated.get("quiet")Creates a variable named quiet. The expression looks up a named or indexed value, using a fallback when the call gives one. The type after the colon says what kind of value it can hold. -
if not (job is float or job is int) or not quiet is bool:Checks not (job is float or job is int) or not quiet is bool. The indented instructions below run only if this test passes. -
result.text = "Unexpected type. Keeping current game."Updates result.text, which controls the words a player sees. The expression uses the text inside the quotes. -
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. -
result.text = "Invalid job. Keeping current game."Updates result.text, which controls the words a player sees. The expression uses the text inside the quotes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
current = {"version": VERSION, "highest_job": int(job), "quiet": quiet}Assign a whole validated record at once. Earlier errors leave every old field untouched. -
result.text = "Loaded and validated as version 2."Updates result.text, which controls the words a player sees. The expression uses the text inside the quotes. -
render()Calls render with the values in its parentheses. -
func render() -> void:Defines render, 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 = "Highest job: %d • Quiet: %s" % [current["highest_job"], str(current["quiet"])]Updates status.text, which controls the words a player sees. The expression uses the text inside the quotes. -
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 a future save overwrites progress, make sure the underscore match branch returns before the assignment to current.
Try a change
Ages 10–12 can list the result of each sample in a notebook. Ages 13–16 can add a version 3 palette field and build fixtures for versions 1, 2, 3 and an unsupported version.