Mobile Game 04 · Decide when the gallery is ready

Museum of Lost Lights · Lesson 04 of 50 · Beginner · About 32 min

The first control panel has three states. A full lamp can light an exhibit, a low lamp needs a spare cell, and an empty lamp sends you back to the dock.

A gouache painting of an Ocean Gallery with glass jellyfish exhibits, fossils and a deep teal floor.
What you’ll make Write rules with if, elif and else. Add a visible readout that explains why the lamp is ready, low or empty.

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/03 when the lesson builds on prior work, or copy the checkpoints/04 folder to try this lesson on its own. Read the file names in the steps before editing.

Read charge. The Inspector provides an int. → Check empty. Less than 1 picks Empty. → Check low. Energies 1 and 2 pick Low. → Show ready. The final branch and mint label use the higher charges.
Read charge → Check empty → Check low → Show ready

Build it step by step

Language and tools in this lesson if, elif, else, >=, <, bool, return, Color.

  1. Open the new scene and read the panel title

    Import Lesson 04 and open main.gd. Select the root Control, set Charge to 4 in the Inspector, then run. Look at the title and the mint line below it.

    make_heading()
    	show_check()

    Why it works Godot calls _ready, which gives two jobs to two functions. make_heading builds the title. show_check works out and displays the lamp’s current status.

  2. Read the first yes-or-no check

    Find if charge < 1. Run the same scene after setting Charge to 0. Mark the first branch taken in your notes.

    if charge < 1:

    Why it works The < symbol asks whether the left number is smaller. if runs its indented instructions when that check is true.

  3. Follow the second check

    Try Charge 2. Trace the first if, the elif and the indented text instruction beneath elif.

    elif charge < 3:

    Why it works elif asks another question only if the earlier checks failed. Here charge 2 skips Empty and takes Low.

  4. Follow the final fallback

    Set Charge to 3 and run again. Read the result and explain why the first two branches were skipped.

    else:
    		status.text = "Ready · Wake an exhibit"

    Why it works else runs its instructions when each earlier check is false. Only one text branch of the chain runs.

  5. Set colour separately from words

    Read ready_to_go, then follow how the second if uses it. Rerun at 2 and 3 and check the words before looking at the colour.

    var ready_to_go: bool = charge >= 3
    	if ready_to_go:

    Why it works The >= check means greater than or equal to. bool has only true and false. The second if uses this answer to choose whether to change the theme’s default text colour. The words stay readable without recognising the colour.

Watch the values change

  1. Set and test Charge 0 0 < 1 is true
    What you see. The first branch says Empty.
  2. Set and test Charge 2 2 < 1 is false, 2 < 3 is true
    What you see. The second branch says Low.
  3. Set and test Charge 3 Both less-than checks are false. 3 >= 3 is true.
    What you see. The else says Ready and the second if tints the text.

Predict before you run At Charge 1, which branch runs and does the text get a mint tint?

Reveal the answer

It says Low. One is not less than one, but it is less than three. The tint check is false because one is below three.

Complete checkpoint

Charge 4 shows Ready · Wake an exhibit in mint. Rerun with charge 2 for Low or 0 for Empty.

main.gd

extends Control

@export var charge: int = 4

func _ready() -> void:
	make_heading()
	show_check()

func show_check() -> void:
	var status := Label.new()
	status.position = Vector2(54, 114)
	status.add_theme_font_size_override("font_size", 27)
	if charge < 1:
		status.text = "Empty · Return to the dock"
	elif charge < 3:
		status.text = "Low · Find a spare cell"
	else:
		status.text = "Ready · Wake an exhibit"
	var ready_to_go: bool = charge >= 3
	if ready_to_go:
		status.add_theme_color_override("font_color", Color("#b0f5d5"))
	add_child(status)
	print(status.text)

func make_heading() -> void:
	var heading := Label.new()
	heading.text = "Night shift · Lantern check"
	heading.position = Vector2(52, 36)
	heading.add_theme_font_size_override("font_size", 38)
	heading.add_theme_color_override("font_color", Color("#ffe1a8"))
	add_child(heading)

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 ControlThe scene root behaves like a Control, the kind of Godot node used for a game’s text, controls and other interface pieces.
  2. @export var charge: int = 4Use four battery units at startup. You can use different values in the editor to make each of the three paths run.
  3. 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.
  4. make_heading()Build the title before the room makes its other controls.
  5. show_check()Create and fill the status sign after the heading has been made.
  6. func show_check() -> void:Group all the code that chooses the lamp’s status and shows it to the player.
  7. var status := Label.new()Make an empty Label. The next section gives it one of three messages.
  8. status.position = Vector2(54, 114)Use two numbers to give this control its position, measured right and down from the root.
  9. status.add_theme_font_size_override("font_size", 27)Set a larger or smaller font here without changing the typography on the other controls.
  10. if charge < 1:Check for an empty battery before considering the low-charge case. A charge of zero belongs here.
  11. status.text = "Empty · Return to the dock"Show the empty-battery instruction and send the player back to the dock.
  12. elif charge < 3:The empty check has already failed. Now ask if the battery has one or two units left.
  13. status.text = "Low · Find a spare cell"Give the player the low-charge prompt when the second test passes.
  14. else:Reach this branch only when both earlier comparisons failed. The battery has three units or more.
  15. status.text = "Ready · Wake an exhibit"Tell the player the lamp now has enough charge to wake an exhibit.
  16. var ready_to_go: bool = charge >= 3Store the answer to a new yes-or-no comparison. At three or higher the Boolean value becomes true.
  17. if ready_to_go:Use that Boolean to decide whether to style the sign as ready.
  18. status.add_theme_color_override("font_color", Color("#b0f5d5"))Give the ready Label a mint tint. The words still tell the visitor what to do if they cannot distinguish the colour.
  19. add_child(status)Put the finished status display onto the game screen.
  20. print(status.text)Log the status text so a developer can compare each battery-input experiment.
  21. func make_heading() -> void:Group the instructions that make the panel title. The room calls this once at startup.
  22. var heading := Label.new()Create a Label node and keep a reference under the name heading so the next lines all customise the same sign.
  23. heading.text = "Night shift · Lantern check"Show the name of this museum task above its controls.
  24. heading.position = Vector2(52, 36)Put the title 52 pixels to the right and 36 pixels down from the root’s upper-left corner.
  25. heading.add_theme_font_size_override("font_size", 38)Set this heading’s font size to 38. The other labels keep their own sizes.
  26. heading.add_theme_color_override("font_color", Color("#ffe1a8"))Give only this label a warm gold text colour. The six characters after # describe red, green and blue light.
  27. add_child(heading)Connect the title to the scene tree so Godot can display it.

Find and fix a bug

If every value below 3 says Low, check the order of your branches. The empty check must run first because 0 also meets charge < 3.

Try a change

Ages 10–12: Make a table of the results at 0, 1, 2 and 3. Ages 13–16: Add a Fully charged branch for charge == 6 and keep the other three outcomes working.