Museum of Lost Lights · Lesson 34 of 50 · Intermediate to advanced · About 36 min
The lantern needs to know whether a room is waiting, being explored or ready for the next gallery. Give every screen one clear job.

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/33 when the lesson builds on prior work, or copy the checkpoints/34 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 enum, match, return, disabled, state, guard clause.
-
Open the gallery flow lab
Import the checkpoint and press F5. Try to press Light the practice exhibit before entering the gallery. Notice which button is available first.
Why it works The layout teaches the player what action makes sense right now. The practice button stands in for the puzzle's eventual lamp-lit event.
-
Name the three phases
Open main.gd and read the enum at the top. Say when the room would be INTRO, PLAY or CLEARED.
enum Phase { INTRO, PLAY, CLEARED }Why it works An enum makes named choices. One room can store one of them as its current phase, so two contradictory phases cannot be active at the same time.
-
Change phase only after a valid action
Read finish_room from its heading to render_flow. Find the line that prevents an early win, and then the line that changes the phase.
if phase != Phase.PLAY: returnWhy it works != asks whether the values differ. An early return stops the function, so an attempt to finish when you are outside PLAY cannot change the state.
-
Make the buttons match the phase
In render_flow, read the three lines setting disabled. Enter and clear Ocean, then move through Fossil to Space and compare which actions light up.
next_button.disabled = phase != Phase.CLEARED or current_gallery == ROOMS.size() - 1Why it works disabled turns an action off. The or rule disables Next gallery when either the room is uncleared or the player has reached the last array item.
-
Give each phase a useful message
Find match phase. Choose an entirely different message for INTRO and rerun. Leave the indented branch labels in place.
match phase:Why it works match selects a branch by the current value. The three branches keep the intro, active and cleared messages next to one another.
-
Test the final gallery and restart
Clear Space. Verify Next gallery stays disabled, then hit Restart current gallery. Check that you are still in Space and have to enter again.
Why it works A final-gallery test catches going beyond the array. Resetting the phase separately from the gallery keeps Restart on the same room.
Watch the values change
-
Start Ocean current_gallery = 0, phase = INTRO.
What you see. Enter gallery is available. -
Enter then light PLAY becomes CLEARED after the light action.
What you see. The article reports Ocean · Exhibit awake! and enables Next gallery. -
Press Next gallery Gallery index becomes 1 and phase returns to INTRO.
What you see. Fossil waits for you to enter.
Predict before you run At Space, can you both clear the exhibit and have Next gallery disabled? Why?
Reveal the answer
Yes. The room reaches CLEARED, but Space has the last index. The or condition keeps Next gallery disabled.
Complete checkpoint
Each gallery can be entered, cleared and restarted. Next starts the following gallery and stays disabled after Space finishes.
main.gd
extends Control
enum Phase { INTRO, PLAY, CLEARED }
const ROOMS := ["Ocean", "Fossil", "Space"]
var current_gallery := 0
var phase := Phase.INTRO
var progress_label: Label
var begin_button: Button
var light_button: Button
var next_button: Button
func _ready() -> void:
var panel := make_panel("34 · Polish the gallery flow")
progress_label = make_label(panel, "", 25)
begin_button = make_button(panel, "Enter gallery", begin_room)
light_button = make_button(panel, "Light the practice exhibit", finish_room)
next_button = make_button(panel, "Next gallery", advance_room)
make_button(panel, "Restart current gallery", reset_room)
render_flow()
func begin_room() -> void:
if phase == Phase.INTRO:
phase = Phase.PLAY
render_flow()
func finish_room() -> void:
if phase != Phase.PLAY:
return
phase = Phase.CLEARED
render_flow()
func advance_room() -> void:
if phase != Phase.CLEARED or current_gallery == ROOMS.size() - 1:
return
current_gallery += 1
reset_room()
func reset_room() -> void:
phase = Phase.INTRO
render_flow()
func render_flow() -> void:
begin_button.disabled = phase != Phase.INTRO
light_button.disabled = phase != Phase.PLAY
next_button.disabled = phase != Phase.CLEARED or current_gallery == ROOMS.size() - 1
var room_name: String = ROOMS[current_gallery]
match phase:
Phase.INTRO:
progress_label.text = room_name + " · Ready to enter"
Phase.PLAY:
progress_label.text = room_name + " · Find the exhibit"
Phase.CLEARED:
progress_label.text = room_name + " · Exhibit awake!"
func make_panel(heading: String, width: float = 580.0) -> VBoxContainer:
var margin := MarginContainer.new()
margin.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
for side in ["left", "right", "top", "bottom"]:
margin.add_theme_constant_override("margin_" + side, 24)
add_child(margin)
var centring := CenterContainer.new()
margin.add_child(centring)
var panel := VBoxContainer.new()
panel.custom_minimum_size.x = width
panel.add_theme_constant_override("separation", 12)
centring.add_child(panel)
make_label(panel, heading, 28)
return panel
func make_label(parent: Control, message: String, size: int = 18) -> Label:
var label := Label.new()
label.text = message
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
label.add_theme_font_size_override("font_size", size)
parent.add_child(label)
return label
func make_button(parent: Control, caption: String, handle_press: Callable) -> Button:
var button := Button.new()
button.text = caption
button.custom_minimum_size.y = 52
button.pressed.connect(handle_press)
parent.add_child(button)
return button
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 ControlGive this script the layout and input abilities of Godot's Control node. The code below turns the root Control into a particular museum practice scene. -
enum Phase { INTRO, PLAY, CLEARED }Give the gallery three named phases. Code can read or change one current phase without comparing long strings. -
const ROOMS := ["Ocean", "Fossil", "Space"]Creates a constant named ROOMS. The expression makes a list whose items keep their order. Godot infers the type from the first value. -
var current_gallery := 0Creates a variable named current_gallery. The expression uses the whole number 0. Godot infers the type from the first value. -
var phase := Phase.INTROCreates a variable named phase. The expression reads Phase.INTRO. Godot infers the type from the first value. -
var progress_label: LabelDeclares progress_label for later use. A colon followed by a type says which kind of value belongs here. -
var begin_button: ButtonDeclares begin_button for later use. A colon followed by a type says which kind of value belongs here. -
var light_button: ButtonDeclares light_button for later use. A colon followed by a type says which kind of value belongs here. -
var next_button: ButtonDeclares next_button for later use. A colon followed by a type says which kind of value belongs here. -
func _ready() -> void:Godot calls this function once when the node is ready. The indented setup creates the interface and gives its controls their starting values. -
var panel := make_panel("34 · Polish the gallery flow")Creates a variable named panel. The expression calls make_panel with the values in its parentheses. Godot infers the type from the first value. -
progress_label = make_label(panel, "", 25)Stores the new value in progress_label. The expression calls make_label with the values in its parentheses. -
begin_button = make_button(panel, "Enter gallery", begin_room)Stores the new value in begin_button. The expression calls make_button with the values in its parentheses. -
light_button = make_button(panel, "Light the practice exhibit", finish_room)Stores the new value in light_button. The expression calls make_button with the values in its parentheses. -
next_button = make_button(panel, "Next gallery", advance_room)Stores the new value in next_button. The expression calls make_button with the values in its parentheses. -
make_button(panel, "Restart current gallery", reset_room)Calls make_button with the values in its parentheses. -
render_flow()Calls render_flow with the values in its parentheses. -
func begin_room() -> void:Defines begin room, 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. -
if phase == Phase.INTRO:Only the intro screen is allowed to enter active play. -
phase = Phase.PLAYSwitch to active play, after which rendering enables the puzzle action. -
render_flow()Calls render_flow with the values in its parentheses. -
func finish_room() -> void:Defines finish room, 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. -
if phase != Phase.PLAY:Check for a phase outside active play. The early return protects against claiming a win before entering. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
phase = Phase.CLEAREDRecord that the active room's exhibit has been woken. -
render_flow()Calls render_flow with the values in its parentheses. -
func advance_room() -> void:Defines advance room, 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. -
if phase != Phase.CLEARED or current_gallery == ROOMS.size() - 1:Stop moving forward if the room has not been cleared or the gallery already has the last usable index. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
current_gallery += 1Advance the index one place along the ordered gallery names. -
reset_room()Calls reset_room with the values in its parentheses. -
func reset_room() -> void:Defines reset room, 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. -
phase = Phase.INTROReset this room to its starting phase. The currently selected gallery stays the same. -
render_flow()Calls render_flow with the values in its parentheses. -
func render_flow() -> void:Defines render flow, 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. -
begin_button.disabled = phase != Phase.INTROGray out Enter unless the room is waiting at its introduction. -
light_button.disabled = phase != Phase.PLAYGray out the puzzle action unless the active phase is PLAY. -
next_button.disabled = phase != Phase.CLEARED or current_gallery == ROOMS.size() - 1Make Next available only when the current room is cleared and another gallery remains. -
var room_name: String = ROOMS[current_gallery]Look up the current gallery's name with its zero-based index and store the returned text. -
match phase:Read the active phase once and select its matching message branch. -
Phase.INTRO:Use the indented message only when the room is at its introduction. -
progress_label.text = room_name + " · Ready to enter"Updates progress_label.text, which controls the words a player sees. The expression calculates a new value from the expressions on either side of the operator. -
Phase.PLAY:Use the indented message only while the player is exploring. -
progress_label.text = room_name + " · Find the exhibit"Updates progress_label.text, which controls the words a player sees. The expression calculates a new value from the expressions on either side of the operator. -
Phase.CLEARED:Use the indented message only when the current exhibit is awake. -
progress_label.text = room_name + " · Exhibit awake!"Updates progress_label.text, which controls the words a player sees. The expression calculates a new value from the expressions on either side of the operator. -
func make_panel(heading: String, width: float = 580.0) -> VBoxContainer:Make a helper that takes the heading text and the panel's minimum width. If the caller omits the width, use 580 game pixels. Return the laid-out panel so the caller can add more controls. -
var margin := MarginContainer.new()Create a container that can reserve clear space between the game controls and the window edges. -
margin.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Choose the preset that aligns the margin's edges with all four edges of the parent Control. -
for side in ["left", "right", "top", "bottom"]:Visit the four names in order so the following indented line can set one margin on each trip through the loop. -
margin.add_theme_constant_override("margin_" + side, 24)Join margin_ to the current side's name. Set that edge to 24 game pixels, and repeat for the other sides. -
add_child(margin)Connect the completed margin container to the lesson's scene root so it can lay out its children. -
var centring := CenterContainer.new()Make a container that will centre the controls within the available space. -
margin.add_child(centring)Give the centring container the space inside the extra border, then let it place the panel within that space. -
var panel := VBoxContainer.new()Create a container that arranges its child controls in one vertical column. -
panel.custom_minimum_size.x = widthAsk the layout for at least the selected width, where x is the horizontal part of the size. -
panel.add_theme_constant_override("separation", 12)Leave 12 game pixels of space between each pair of controls in the vertical panel. -
centring.add_child(panel)Attach the vertical column to the centring container. Godot can now place the whole column together. -
make_label(panel, heading, 28)Put the supplied heading at the top of the column and ask for a larger 28-pixel font. -
return panelHand the panel reference back to the caller so it can add labels or buttons under the heading. -
func make_label(parent: Control, message: String, size: int = 18) -> Label:Accept the parent control, text to show and optional font size. Return the new Label so the caller can update it later. -
var label := Label.new()Make a new empty Label, which is Godot's text display node. -
label.text = messageCopy the incoming message into the label's visible words. -
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTLet the label wrap lines at sensible places so a long message can fit when the layout is narrow. -
label.add_theme_font_size_override("font_size", size)Set the chosen size on this one label so other text can keep different sizes. -
parent.add_child(label)Add the label to the supplied parent. It becomes part of that panel's layout and can appear on screen. -
return labelReturn the exact label you created so the caller can change its text without building a second label. -
func make_button(parent: Control, caption: String, handle_press: Callable) -> Button:Build and return one Button. The arguments identify its parent, the words on it and the action to run after pressing it. -
var button := Button.new()Create a Godot Button that supports pointer, touch and keyboard activation. -
button.text = captionPut the supplied caption on the visible control. -
button.custom_minimum_size.y = 52Reserve at least 52 game pixels vertically so the control remains a comfortable target. -
button.pressed.connect(handle_press)Connect the pressed signal to the supplied callable. Godot waits for activation before running the action. -
parent.add_child(button)Attach the Button to its parent and let that container position it. -
return buttonGive the finished Button to the caller, which can keep a reference to set focus or change the caption later.
Find and fix a bug
If Next gallery crashes at Space, verify that current_gallery is compared to ROOMS.size() - 1 before you add 1. A list with three items ends at index 2.
Try a change
Ages 10–12 can add a fourth room to ROOMS and test the finish again. Ages 13–16 can insert another phase after PLAY for a player-controlled victory animation.