Cabin Crew Express · Lesson 36 of 50 · Intermediate to advanced · About 38 min
Ari's service record should survive closing the app. Keep this lab's tiny test save on the player's own device.

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, set_value, save, load, user://.
-
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.
-
Find the writable folder
Read SAVE_PATH. Open Project > Open User Data Folder after saving if you want to see the actual file.
const SAVE_PATH := "user://cabin_unit36.cfg"Why it works user:// points to the app's local writable directory, while res:// belongs to the packaged project.
-
Prepare a section and key
Click Complete a flight twice, then Save progress. Find the three arguments in set_value.
cfg.set_value("progress", "completed", completed)Why it works progress is a section, completed is a key and the final completed is the current count in memory.
-
Check the save result
Read the status after saving and find how result is compared with OK.
var result := cfg.save(SAVE_PATH)Why it works File operations return a result code. The label can report failure instead of promising the save worked.
-
Clear the screen then recover the file
Click Clear screen count and Load progress. Watch the number return.
var count: int = int(cfg.get_value("progress", "completed", 0))Why it works The clear button changes memory. The load button reads the separate file and redraws the status with the saved count.
-
Limit the loaded value
Find clampi. Read the two endpoint values. Try lowering the upper value in read_save to 1, then reload a stored count of 2 and restore the code.
completed = clampi(count, 0, 9)Why it works Clamping keeps an unexpected count inside the limits of the mission list.
Watch the values change
-
Complete twice Memory holds 2 while the file keeps its earlier value.
What you see. The count reads 2 / 9. -
Save, then clear the screen The file holds 2 while memory holds 0.
What you see. The count becomes 0 / 9. -
Load progress The config restores 2 and the bounds check accepts it.
What you see. The count is 2 / 9 again.
Predict before you run You save three finished flights and then press Clear screen count. What will Load progress show?
Reveal the answer
It shows three. The clear button changes the running variable only.
Complete checkpoint
Saving a count, clearing the screen and loading restores the saved number. Without a readable save, the current count stays in place.
main.gd
extends Control
const SAVE_PATH := "user://cabin_unit36.cfg"
var completed := 0
var status: Label
var detail: Label
func _ready() -> void:
heading("Keep a record of completed flights", "LOCAL SAVE")
status = label("", Vector2(48, 149))
detail = label("Mark one flight complete, then save and reload it.", Vector2(48, 207), 17)
button("Complete a flight", Vector2(48, 284), complete, 190)
button("Save progress", Vector2(254, 284), write_save, 171)
button("Load progress", Vector2(441, 284), read_save, 171)
button("Clear screen count", Vector2(628, 284), clear_screen, 197)
redraw_status()
func redraw_status() -> void:
status.text = "%d / 9 flights completed in memory" % completed
func complete() -> void:
completed = mini(completed + 1, 9)
detail.text = "Progress is in memory. Press Save progress to keep it."
redraw_status()
func write_save() -> void:
var cfg := ConfigFile.new()
cfg.set_value("progress", "completed", completed)
var result := cfg.save(SAVE_PATH)
detail.text = "Saved locally." if result == OK else "Could not write save file (%d)." % result
func read_save() -> void:
var cfg := ConfigFile.new()
var result := cfg.load(SAVE_PATH)
if result != OK:
detail.text = "No readable save • the screen has not changed."
return
var count: int = int(cfg.get_value("progress", "completed", 0))
completed = clampi(count, 0, 9)
detail.text = "Loaded the saved progress."
redraw_status()
func clear_screen() -> void:
completed = 0
detail.text = "Screen cleared • the stored file is still there."
redraw_status()
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
-
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://cabin_unit36.cfg"Use a writable per-user file. This checkpoint does not use Ari’s final game save. -
var completed := 0Creates a variable named completed. The expression uses the whole number 0. 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 detail: LabelDeclares detail 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 record of completed flights", "LOCAL SAVE")Calls heading with the values in its parentheses. -
status = label("", Vector2(48, 149))Stores the new value in status. The expression calls label with the values in its parentheses. -
detail = label("Mark one flight complete, then save and reload it.", Vector2(48, 207), 17)Stores the new value in detail. The expression calls label with the values in its parentheses. -
button("Complete a flight", Vector2(48, 284), complete, 190)Calls button with the values in its parentheses. -
button("Save progress", Vector2(254, 284), write_save, 171)Calls button with the values in its parentheses. -
button("Load progress", Vector2(441, 284), read_save, 171)Calls button with the values in its parentheses. -
button("Clear screen count", Vector2(628, 284), clear_screen, 197)Calls button with the values in its parentheses. -
redraw_status()Calls redraw_status with the values in its parentheses. -
func redraw_status() -> void:Defines redraw 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 = "%d / 9 flights completed in memory" % completedUpdates status.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func complete() -> void:Defines complete, 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. -
completed = mini(completed + 1, 9)Add one finished flight but keep the local practice counter at most nine. -
detail.text = "Progress is in memory. Press Save progress to keep it."Updates detail.text, which controls the words a player sees. The expression uses the text inside the quotes. -
redraw_status()Calls redraw_status with the values in its parentheses. -
func write_save() -> void:Defines write 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 cfg := ConfigFile.new()Prepare a section-and-key document for storing and retrieving progress. -
cfg.set_value("progress", "completed", completed)Put the live count under a named section and a single predictable key. -
var result := cfg.save(SAVE_PATH)Write to the device, then read the returned error code before claiming success. -
detail.text = "Saved locally." if result == OK else "Could not write save file (%d)." % resultUpdates detail.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
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 cfg := ConfigFile.new()Creates a variable named cfg. The expression creates a new ConfigFile object. Godot infers the type from the first value. -
var result := cfg.load(SAVE_PATH)Try to read a separate local file. Missing and unreadable saves appear as safe outcomes. -
if result != OK:Prevent an invalid file from replacing the value already visible on screen. -
detail.text = "No readable save • the screen has not changed."Updates detail.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 count: int = int(cfg.get_value("progress", "completed", 0))Read the saved count with a zero default, then convert it before checking its range. -
completed = clampi(count, 0, 9)Restrict file data to the number of available flights. -
detail.text = "Loaded the saved progress."Updates detail.text, which controls the words a player sees. The expression uses the text inside the quotes. -
redraw_status()Calls redraw_status with the values in its parentheses. -
func clear_screen() -> void:Defines clear screen, 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. -
completed = 0Stores the new value in completed. The expression uses the whole number 0. -
detail.text = "Screen cleared • the stored file is still there."Changing the screen value alone leaves the stored progress intact. -
redraw_status()Calls redraw_status with the values in its parentheses. -
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("CABIN CREW EXPRESS / " + topic, Vector2(48, 41), 15, Color("#ffca7b"))Calls label with the values in its parentheses. -
label(caption, Vector2(48, 73), 27)Calls label with the values in its parentheses. -
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. -
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 = textUpdates item.text, which controls the words a player sees. The expression reads text. -
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(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. -
var item := Button.new()Creates a variable named item. The expression creates a new Button object. Godot infers the type from the first value. -
item.text = textUpdates item.text, which controls the words a player sees. The expression reads text. -
item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at. -
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. -
item.pressed.connect(action)Links a signal to the function that should run when the event happens. -
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 _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state. -
draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))Paints a rectangle at the given position and size. -
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 Load says there is no readable save, complete one flight and save it first. Each checkpoint has its own local filename.
Try a change
Ages 10–12 can add a second counter away from the saved value. Ages 13–16 can add a saved audio preference in a settings section and test that both values load.