Cabin Crew 37 · Migrate an older service save

Cabin Crew Express · Lesson 37 of 50 · Intermediate to advanced · About 38 min

In version 1, Ari's file only tracks cleared flights. Version 2 adds a value for the quickest complete service.

Storage cupboards, water and folded supplies in the compact onboard galley.
What you’ll make Read two format versions, write a migrated copy and keep progress when a file is unsupported.

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.

Read version. Identify the format before reading progress. → Translate key. Read cleared from v1. → Validate. Keep unsupported values out of memory. → Write v2. Save the current representation.
Read version → Translate key → Validate → Write v2
Godot checkpoint window for Migrate an older service save
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson schema version, Variant, type check, early return, migration.

  1. Open the editable checkpoint

    Start Godot 4.7.2, choose Import in Project Manager and select the project.godot file for this lesson. Open main.gd in FileSystem, then press F5. Stop the game with F8 before editing.

    Why it works This folder is its own project with a Control scene and an attached script. You can run it without files from any other lesson.

  2. Make a sample old file

    Click Sample v1 record. Read sample and find the version and progress key it writes.

    cfg.set_value("progress", "cleared", 3)

    Why it works The number under meta describes the shape of the save. Version 1 stores the counter at cleared.

  3. Choose the matching progress key

    Click Load / upgrade. Follow the inline choice that reads cleared or completed.

    var key := "cleared" if version == 1 else "completed"

    Why it works A small migration translates an older field into the name used by the current game.

  4. Validate before changing memory

    Load a valid sample, then create and load Invalid record. Read both early exits and check the first result stays.

    if not version is int or version not in [1, VERSION]:

    Why it works A guard rejects the version or value before assigning the loaded values to flights and best_turns.

  5. Set a default for the new field

    Look at the value passed after best_turns. Use Sample v1 record again and follow the load.

    var record: Variant = cfg.get_value("progress", "best_turns", 0)

    Why it works The old file has no best-turn count. A default of 0 means no best result has been filed yet.

  6. Verify the migration really wrote

    Click Load / upgrade twice after making a v1 sample. Compare the first caption with the second.

    cfg.set_value("meta", "version", VERSION)

    Why it works A successful first migration writes VERSION and the new fields. The next load sees v2 and follows the normal branch.

Watch the values change

  1. Make v1 then load Version 1 stores cleared as 3.
    What you see. The display shows 3 flights and no best turns, then upgrades the file.
  2. Load a second time The file is now version 2.
    What you see. The same numbers arrive through the v2 key.
  3. Write v99 and load The version guard rejects it.
    What you see. The prior in-memory state stays visible.

Predict before you run Why is it safer to validate count and record before assigning either to the live variables?

Reveal the answer

Otherwise one value can partly replace valid progress before the program discovers the other value is wrong.

Complete checkpoint

A v1 file upgrades 3 flights to v2. Valid v2 progress loads 5 flights and 14 turns. An unsupported file keeps the last valid display.

main.gd

extends Control

const SAVE_PATH := "user://cabin_unit37.cfg"
const VERSION := 2
var flights := 0
var best_turns := 0
var status: Label
var detail: Label

func _ready() -> void:
	heading("Upgrade a service record without losing flights", "MIGRATION")
	status = label("", Vector2(48, 149))
	detail = label("Create a test record, then load it.", Vector2(48, 208), 18)
	button("Sample v1 record", Vector2(48, 283), sample.bind(1), 196)
	button("Sample v2 record", Vector2(256, 283), sample.bind(2), 196)
	button("Invalid record", Vector2(464, 283), sample.bind(99), 174)
	button("Load / upgrade", Vector2(650, 283), load_record, 216)
	show_record()

func sample(version: int) -> void:
	var cfg := ConfigFile.new()
	cfg.set_value("meta", "version", version)
	if version == 1:
		cfg.set_value("progress", "cleared", 3)
	elif version == 2:
		cfg.set_value("progress", "completed", 5)
		cfg.set_value("progress", "best_turns", 14)
	var result := cfg.save(SAVE_PATH)
	detail.text = "Sample v%d written." % version if result == OK else "Sample could not be saved."

func show_record() -> void:
	status.text = "Flights completed: %d     Best turns: %d" % [flights, best_turns]

func load_record() -> void:
	var cfg := ConfigFile.new()
	if cfg.load(SAVE_PATH) != OK:
		detail.text = "No readable file. Previous record kept."
		return
	var version: Variant = cfg.get_value("meta", "version", 0)
	if not version is int or version not in [1, VERSION]:
		detail.text = "Unsupported version. Previous record kept."
		return
	var key := "cleared" if version == 1 else "completed"
	var count: Variant = cfg.get_value("progress", key, -1)
	var record: Variant = cfg.get_value("progress", "best_turns", 0)
	if not count is int or count < 0 or count > 9 or not record is int or record < 0:
		detail.text = "Invalid progress values. Previous record kept."
		return
	flights = count
	best_turns = record
	show_record()
	if version == 1:
		cfg.set_value("meta", "version", VERSION)
		cfg.set_value("progress", "completed", flights)
		cfg.set_value("progress", "best_turns", best_turns)
		detail.text = "Old record upgraded to v2." if cfg.save(SAVE_PATH) == OK else "Old record loaded. Upgrade could not be written."
	else:
		detail.text = "Loaded the v2 record."

func heading(caption: String, topic: String) -> void:
	label("CABIN CREW EXPRESS     /     " + topic, Vector2(48, 41), 15, Color("#ffca7b"))
	label(caption, Vector2(48, 73), 27)

func label(text: String, at: Vector2, pixels: int = 20, ink: Color = Color("#f7f4e9")) -> Label:
	var item := Label.new()
	item.text = text
	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(text: String, at: Vector2, action: Callable, width: float = 155.0) -> Button:
	var item := Button.new()
	item.text = text
	item.position = at
	item.custom_minimum_size = Vector2(width, 47)
	item.pressed.connect(action)
	add_child(item)
	return item

func _draw() -> void:
	draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))
	draw_rect(Rect2(Vector2(28, 25), Vector2(904, 483)), Color("#18364b"))

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://cabin_unit37.cfg"Creates a constant named SAVE_PATH. The expression uses the text inside the quotes. Godot infers the type from the first value.
  3. const VERSION := 2Fix the format this scene currently writes so future loaders can recognise it.
  4. var flights := 0Creates a variable named flights. The expression uses the whole number 0. Godot infers the type from the first value.
  5. var best_turns := 0Creates a variable named best_turns. The expression uses the whole number 0. 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. var detail: LabelDeclares detail for later use. A colon followed by a type says which kind of value belongs here.
  8. 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.
  9. heading("Upgrade a service record without losing flights", "MIGRATION")Calls heading with the values in its parentheses.
  10. status = label("", Vector2(48, 149))Stores the new value in status. The expression calls label with the values in its parentheses.
  11. detail = label("Create a test record, then load it.", Vector2(48, 208), 18)Stores the new value in detail. The expression calls label with the values in its parentheses.
  12. button("Sample v1 record", Vector2(48, 283), sample.bind(1), 196)Calls button with the values in its parentheses.
  13. button("Sample v2 record", Vector2(256, 283), sample.bind(2), 196)Calls button with the values in its parentheses.
  14. button("Invalid record", Vector2(464, 283), sample.bind(99), 174)Calls button with the values in its parentheses.
  15. button("Load / upgrade", Vector2(650, 283), load_record, 216)Calls button with the values in its parentheses.
  16. show_record()Calls show_record with the values in its parentheses.
  17. func sample(version: int) -> void:Defines sample, 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. var cfg := ConfigFile.new()Creates a variable named cfg. The expression creates a new ConfigFile object. Godot infers the type from the first value.
  19. cfg.set_value("meta", "version", version)Label the generated fixture before choosing its older or current progress fields.
  20. if version == 1:Use the older branch only when reading or creating a genuinely older record.
  21. cfg.set_value("progress", "cleared", 3)Version one stored the flown count under a different field name.
  22. elif version == 2:If the earlier branches did not run, checks version == 2 next.
  23. cfg.set_value("progress", "completed", 5)Calls cfg.set_value with the values in its parentheses.
  24. cfg.set_value("progress", "best_turns", 14)Calls cfg.set_value with the values in its parentheses.
  25. var result := cfg.save(SAVE_PATH)Creates a variable named result. The expression calls cfg.save with the values in its parentheses. Godot infers the type from the first value.
  26. detail.text = "Sample v%d written." % version if result == OK else "Sample could not be saved."Updates detail.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle.
  27. func show_record() -> void:Defines show record, 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.
  28. status.text = "Flights completed: %d Best turns: %d" % [flights, best_turns]Updates status.text, which controls the words a player sees. The expression uses the text inside the quotes.
  29. func load_record() -> void:Defines load record, 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.
  30. var cfg := ConfigFile.new()Creates a variable named cfg. The expression creates a new ConfigFile object. Godot infers the type from the first value.
  31. if cfg.load(SAVE_PATH) != OK:Keep live data if the file cannot even be parsed.
  32. detail.text = "No readable file. Previous record kept."Updates detail.text, which controls the words a player sees. The expression uses the text inside the quotes.
  33. returnFinishes the function right here. Any later instructions in this function are skipped.
  34. var version: Variant = cfg.get_value("meta", "version", 0)Treat a file field as untrusted data until the type guard proves it is a usable integer.
  35. if not version is int or version not in [1, VERSION]:Reject unknown versions rather than guessing the layout of the saved progress.
  36. detail.text = "Unsupported version. Previous record kept."Updates detail.text, which controls the words a player sees. The expression uses the text inside the quotes.
  37. returnFinishes the function right here. Any later instructions in this function are skipped.
  38. var key := "cleared" if version == 1 else "completed"Choose the correct count key for the given format before reading it.
  39. var count: Variant = cfg.get_value("progress", key, -1)Use a sentinel value for a required count that is missing.
  40. var record: Variant = cfg.get_value("progress", "best_turns", 0)Older records never tracked a best turn count. Zero stands for no record.
  41. if not count is int or count < 0 or count > 9 or not record is int or record < 0:Confirm both values are valid before affecting the scene at all.
  42. detail.text = "Invalid progress values. Previous record kept."Updates detail.text, which controls the words a player sees. The expression uses the text inside the quotes.
  43. returnFinishes the function right here. Any later instructions in this function are skipped.
  44. flights = countCommit the validated count only after the entire record has passed its checks.
  45. best_turns = recordStores the new value in best_turns. The expression reads record.
  46. show_record()Calls show_record with the values in its parentheses.
  47. if version == 1:Checks version == 1. The indented instructions below run only if this test passes.
  48. cfg.set_value("meta", "version", VERSION)Set the current version before writing the migrated progress so it will load cleanly next time.
  49. cfg.set_value("progress", "completed", flights)Calls cfg.set_value with the values in its parentheses.
  50. cfg.set_value("progress", "best_turns", best_turns)Calls cfg.set_value with the values in its parentheses.
  51. detail.text = "Old record upgraded to v2." if cfg.save(SAVE_PATH) == OK else "Old record loaded. Upgrade could not be written."Distinguish successfully reading an old file from successfully writing its upgrade.
  52. else:Runs the indented fallback instructions if none of the earlier tests passed.
  53. detail.text = "Loaded the v2 record."Updates detail.text, which controls the words a player sees. The expression uses the text inside the quotes.
  54. 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.
  55. label("CABIN CREW EXPRESS / " + topic, Vector2(48, 41), 15, Color("#ffca7b"))Calls label with the values in its parentheses.
  56. label(caption, Vector2(48, 73), 27)Calls label with the values in its parentheses.
  57. func label(text: String, at: Vector2, pixels: int = 20, ink: Color = Color("#f7f4e9")) -> 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 = textUpdates item.text, which controls the words a player sees. The expression reads text.
  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)Calls item.add_theme_font_size_override with the values in its parentheses.
  62. item.add_theme_color_override("font_color", ink)Calls item.add_theme_color_override with the values in its parentheses.
  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 button(text: String, at: Vector2, action: Callable, width: float = 155.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.
  66. var item := Button.new()Creates a variable named item. The expression creates a new Button object. Godot infers the type from the first value.
  67. item.text = textUpdates item.text, which controls the words a player sees. The expression reads text.
  68. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  69. item.custom_minimum_size = Vector2(width, 47)Updates item.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value.
  70. item.pressed.connect(action)Links a signal to the function that should run when the event happens.
  71. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  72. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  73. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  74. draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))Paints a rectangle at the given position and size.
  75. draw_rect(Rect2(Vector2(28, 25), Vector2(904, 483)), Color("#18364b"))Paints a rectangle at the given position and size.

Find and fix a bug

If the second load still upgrades v1, check the cfg.save call after changing the version. Updating the ConfigFile object alone does not update the file.

Try a change

Ages 10–12 can compare two real sample save files in the editor. Ages 13–16 can add a valid v3 field with its own default and tests.