Museum of Lost Lights · Lesson 31 of 50 · Intermediate to advanced · About 36 min
The museum now has three wings. Your first job is giving the lantern a clear way to choose where to go.

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/30 when the lesson builds on prior work, or copy the checkpoints/31 folder to try this lesson on its own. Read the file names in the steps before editing. Keep the checkpoint ZIP intact and make edits in an extracted working copy.

Build it step by step
Language and tools in this lesson Array of dictionaries, String, for, Button, Callable.bind, Label.text.
-
Open your checkpoint
In Godot's Project Manager, click Import, select this lesson's project.godot and click Import & Edit. Open main.gd in FileSystem, then press F6 with the main scene open or F5 to run the project.
Why it works The main scene is the stage. The script attached to its Control node builds the menu when it enters the scene tree.
-
Write one card's information
Find GALLERIES. Compare the three records and underline the keys that repeat. Change the Ocean exhibit from Jellyfish to Squid, then run again.
{"name": "Ocean", "exhibit": "Jellyfish", "room": 1}Why it works Square brackets contain an ordered array. Braces contain named keys and values. Every card has the same keys, so one set of code can read every card.
-
Let one loop make three buttons
Locate this loop in _ready. Follow it once for Ocean and again for Fossil. Keep the second line one indentation level deeper.
for gallery in GALLERIES: make_button(panel, gallery["name"], select_gallery.bind(gallery))Why it works The loop gives gallery a new dictionary for each trip. bind saves that trip's dictionary for later, when the button is pressed.
-
Connect the signal once
Scroll to make_button. Look for pressed.connect. Click Ocean after running the scene.
button.pressed.connect(handle_press)Why it works pressed is a Godot signal. It calls the saved function in response to a tap, click or keyboard activation. The same connection pattern serves all three buttons.
-
Read the selected card
Follow select_gallery. Match the first %s, %d and last %s to each of the three values in the array below the text.
status.text = "%s · Room %d · %s exhibit"Why it works %s prints a text value, %d prints a whole number and % fills the slots in order. Setting text on the status label makes the new information appear.
-
Test all three cards
Run and try Fossil, Ocean and Space in that order. Then press Tab to move focus and Space to activate the focused button.
Why it works Testing a different order shows every card carries its own data. Testing with the keyboard reveals whether controls can work without a mouse.
Watch the values change
-
Run Loop visits Ocean, Fossil and Space.
What you see. Three buttons appear and no gallery is selected. -
Press Fossil The callable stores the Fossil record with room 5.
What you see. Status shows Fossil · Room 5 · Ammonite exhibit. -
Press Space The Space record arrives as the new argument.
What you see. Status shows Space · Room 9 · Saturn exhibit.
Predict before you run If you swap the Ocean and Fossil dictionaries without changing the loop, which button appears first and which room does it select?
Reveal the answer
Fossil becomes the first button and it still selects Room 5. Position comes from array order, but the room comes from the dictionary.
Complete checkpoint
Three buttons announce Ocean Room 1, Fossil Room 5 and Space Room 9. The keyboard can activate each focused button.
main.gd
extends Control
const GALLERIES := [
{"name": "Ocean", "exhibit": "Jellyfish", "room": 1},
{"name": "Fossil", "exhibit": "Ammonite", "room": 5},
{"name": "Space", "exhibit": "Saturn", "room": 9}
]
var status: Label
func _ready() -> void:
var panel := make_panel("31 · Choose a museum gallery")
make_label(panel, "Choose the wing you would like to explore.")
for gallery in GALLERIES:
make_button(panel, gallery["name"], select_gallery.bind(gallery))
status = make_label(panel, "No gallery selected.")
func select_gallery(gallery: Dictionary) -> void:
status.text = "%s · Room %d · %s exhibit" % [
gallery["name"], gallery["room"], gallery["exhibit"]
]
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. -
const GALLERIES := [Start a constant array. It stores one record per wing in this course's practice museum. -
{"name": "Ocean", "exhibit": "Jellyfish", "room": 1},Make a dictionary for Ocean. Its three keys identify what should appear on the card and the result readout. -
{"name": "Fossil", "exhibit": "Ammonite", "room": 5},Define the Fossil card using the same keys. The different values are what make this card choose room 5. -
{"name": "Space", "exhibit": "Saturn", "room": 9}Define the Space card with room 9 and Saturn as its exhibit. -
]Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
var status: LabelDeclares status 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("31 · Choose a museum gallery")Creates a variable named panel. The expression calls make_panel with the values in its parentheses. Godot infers the type from the first value. -
make_label(panel, "Choose the wing you would like to explore.")Calls make_label with the values in its parentheses. -
for gallery in GALLERIES:Take the next gallery dictionary from the array on each trip through this loop. -
make_button(panel, gallery["name"], select_gallery.bind(gallery))Make this card's button. Use the dictionary's name for its caption and save the whole dictionary as the later selection function's argument. -
status = make_label(panel, "No gallery selected.")Stores the new value in status. The expression calls make_label with the values in its parentheses. -
func select_gallery(gallery: Dictionary) -> void:Defines select gallery, 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 = "%s · Room %d · %s exhibit" % [Build the status label by placing text, then a whole number, then more text in the three placeholders. The next line supplies the values. -
gallery["name"], gallery["room"], gallery["exhibit"]Read the three values from the chosen gallery dictionary in the exact order used by the formatted status string. -
]Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
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 every button displays the same museum wing, check that bind(gallery) uses the loop variable on each turn. Passing a single fixed dictionary would give them the same details.
Try a change
Ages 10–12 can add a fourth garden wing using the same three keys. Ages 13–16 can add a locked Boolean and make a card visibly unavailable until a previous gallery is complete.