Museum of Lost Lights · Lesson 03 of 50 · Beginner · About 30 min
Lumi needs a spare light cell after waking an exhibit. Work out how much energy is left before the night shift starts.

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/02 when the lesson builds on prior work, or copy the checkpoints/03 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 parameter, argument, int, return, str, maxi, subtraction, function call.
-
Open the checkpoint and find its two inputs
Import Lesson 03. Select the root Control and find Starting Charge and Spent Charge in the Inspector. Leave the defaults 6 and 2 for the first run.
@export var starting_charge: int = 6 @export var spent_charge: int = 2Why it works Both variables have type int, which means a whole number. These are the inputs we hand to the calculation. Keep both values zero or above for this experiment.
-
Read the function names from left to right
Look at charge_after_spend at the bottom of the script. Underline initial and cost and notice the comma. Find where it is called near the start of _ready.
func charge_after_spend(initial: int, cost: int) -> int:Why it works initial and cost are parameters. They are local names inside the function. The actual values supplied in each call are arguments. The order of the arguments determines which parameter gets each value.
-
Subtract before sending the answer
Trace the line using the minus sign, then read the return line. Work out the remaining value for 6 and 2. Try 6 and 8 using the same steps.
var remaining := initial - cost return maxi(remaining, 0)Why it works The minus sign calculates the difference. maxi picks the larger of the two ints it receives. Choosing 0 when the difference is negative stops the displayed charge from going below zero. return gives that int back to the caller.
-
Collect and display the returned value
Find where left is first assigned. Find the line where it is changed into text with str and joined to the words Charge left.
sign.text = "Charge left: " + str(left)Why it works The calling line waits for the function’s answer. str converts the number to text so + can join it to another String.
-
Test a result at the boundary
Run with the defaults, record the result, then stop and rerun with Starting Charge set to 6 and Spent Charge set to 8. Use the Output panel to check the printed result as well as the sign.
Why it works A boundary test uses a value at or past an important limit. Here it proves the function gives zero when the raw result would be below zero.
Watch the values change
-
Call with 6 and 2 initial = 6 and cost = 2
What you see. The subtraction gives 4. -
Finish the first call maxi compares 4 and 0
What you see. The function returns 4. -
Call with 6 and 8 The difference is -2
What you see. The function returns 0 instead. -
Convert the result str turns the number 0 into the text 0
What you see. The sign can display it.
Predict before you run What appears when the two exported inputs are both 5?
Reveal the answer
Charge left: 0. Subtracting equal numbers gives 0 and maxi(0, 0) returns 0.
Complete checkpoint
The default inputs show Charge left: 4. Set Spent Charge to 8 and rerun. The sign reads Charge left: 0.
main.gd
extends Control
@export var starting_charge: int = 6
@export var spent_charge: int = 2
func _ready() -> void:
var left := charge_after_spend(starting_charge, spent_charge)
var sign := Label.new()
sign.position = Vector2(52, 54)
sign.add_theme_font_size_override("font_size", 36)
sign.text = "Charge left: " + str(left)
add_child(sign)
print(sign.text)
func charge_after_spend(initial: int, cost: int) -> int:
var remaining := initial - cost
return maxi(remaining, 0)
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 ControlThe scene root behaves like a Control, the kind of Godot node used for a game’s text, controls and other interface pieces. -
@export var starting_charge: int = 6Start the experiment with a battery of six charge units. The Inspector can give the game a different whole number. -
@export var spent_charge: int = 2The proposed use costs two units by default. The test can try a cost greater than the starting charge. -
func _ready() -> void:Godot automatically calls this setup function after bringing this node and its children into the scene. The code below it prepares the first frame. -
var left := charge_after_spend(starting_charge, spent_charge)Pass the two Inspector values into the calculation. Save the whole-number answer under the name left. -
var sign := Label.new()Make the Label that the game will use to report the charge result. -
sign.position = Vector2(52, 54)Use two numbers to give this control its position, measured right and down from the root. -
sign.add_theme_font_size_override("font_size", 36)Set a larger or smaller font here without changing the typography on the other controls. -
sign.text = "Charge left: " + str(left)Convert left to text with str so you can join it to the label’s fixed wording. -
add_child(sign)Adds the new node to the scene tree so that Godot can run and draw it. -
print(sign.text)Log the sign’s final wording so the Output panel can confirm whether the calculation changed. -
func charge_after_spend(initial: int, cost: int) -> int:Accept the starting amount and the cost as two ints. The caller expects one int returned when the calculation finishes. -
var remaining := initial - costSubtract cost from initial. This intermediate answer can be negative if the lamp spends more energy than its battery has. -
return maxi(remaining, 0)Choose the larger number out of remaining and zero. Send that value back so the sign never displays negative charge.
Find and fix a bug
If you get an invalid operands error on the sign line, make sure str(left) wraps the number. A String can join to a String with +, but left is an int.
Try a change
Ages 10–12: Test 0 and 0, then 2 and 1. Ages 13–16: Add an optional upper limit to your function using clampi and test numbers above and below both boundaries.