Museum of Lost Lights · Lesson 11 of 50 · Beginner to intermediate · About 32 min
The mirror from lesson 10 fits on a much larger museum floor. Label the squares so the lamp, mirror and exhibit can all point to an exact location.

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/10 when the lesson builds on prior work, or copy the checkpoints/11 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 const, Vector2i, return, range, Array[Button], pressed.connect, Callable.bind, scene tree.
-
Mark the floor on paper
Open this lesson’s starter in Godot’s Project Manager. In the Scene panel, select the root Control. On paper, draw five columns and five rows. Label the top row 0–4, the second row 5–9 and the third 10–14. Circle the addresses 10, 12 and 2. Open main.gd and compare those numbers with the project constants.
const WIDTH := 5Why it works A constant declared with const gives a lasting name to a fixed value. WIDTH controls how many squares fit before the next row. The numbers name tiles, so a mirror stays tile 12 even when it changes its slant.
-
Give a paper coordinate a tile number
Look at _to_index. Put the cursor on cell and read its type. Trace column 2 and row 2 with a finger. Multiply 2 rows by 5, then add 2 across. Check that this lands on tile 12.
return cell.y * WIDTH + cell.xWhy it works Vector2i stores two whole numbers. Its x is the column and its y is the row. The arrow announces the returned type. return hands the calculated address back to the caller.
-
Create a button for every numbered square
Read _make_tiles from its loop down to grid.add_child(tile). The first trip has index 0, the last index 24. Press F6 to run the open scene. Count the tiles in the first row before looking at the whole grid.
for index in range(WIDTH * HEIGHT):Why it works range starts at zero and stops just before its argument. Button.new() creates a Button node. tiles.append(tile) keeps a reference so _render can update it. grid.add_child(tile) puts it in the on-screen GridContainer.
-
Tell each Button which square it belongs to
Find the call to connect and compare the order of .pressed, .connect and .bind. Run the project and tap the mirror twice. Try one plain numbered tile afterwards.
tile.pressed.connect(_tap_tile.bind(index))Why it works Each button emits pressed. connect registers a function for later. bind adds that button’s index to the call. Thus every tile can use the same _tap_tile function with a different index.
-
Draw the route to the first mirror
Read _trace_beam, then _render. Work out the three addresses it appends for columns 0, 1 and 2 of row 2. Compare them with the highlighted tiles. Turn the mirror and check that these first three addresses stay the same.
travelled.append(_to_index(Vector2i(column, 2)))Why it works The typed Array[int] keeps a list of tile addresses. The current lesson highlights only the section that leads to the mirror. The route and mirror’s picture are two values drawn by the same render function.
Watch the values change
-
Place the lamp Column 0, row 2
What you see. 2 × 5 + 0 gives tile 10. -
Pass the beam along the row Columns 0, 1 and 2
What you see. The list holds 10, 11 and 12. -
Tap tile 12 The mirror starts at slant 1
What you see. The slant changes to 0 and the symbol becomes /.
Predict before you run What address does column 4, row 1 have? When you press it, how many mirrors turn?
Reveal the answer
Its address is 9 because 1 × 5 + 4 equals 9. The mirror check accepts only tile 12, so zero mirrors turn.
Complete checkpoint
The window contains 25 tiles in 5 rows. Lamp 10 and mirror 12 are part of a highlighted 10 → 11 → 12 path. Only tile 12 toggles its slash picture.
main.gd
extends Control
const WIDTH := 5
const HEIGHT := 5
const SOURCE := 10
const TARGET := 2
const MIRROR := 12
const EAST := Vector2i(1, 0)
const SOUTH := Vector2i(0, 1)
const NORTH := Vector2i(0, -1)
var grid: GridContainer
var status: Label
var tiles: Array[Button] = []
var active_path: Array[int] = []
var orientation := 1
func _ready() -> void:
var background := ColorRect.new()
background.color = Color("#102a3b")
background.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(background)
background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
var centre := CenterContainer.new()
add_child(centre)
centre.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
var page := VBoxContainer.new()
page.add_theme_constant_override("separation", 8)
centre.add_child(page)
var heading := Label.new()
heading.text = "Museum of Lost Lights"
heading.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
heading.add_theme_font_size_override("font_size", 26)
page.add_child(heading)
var subheading := Label.new()
subheading.text = "Ocean Gallery · tap the mirror to turn it"
subheading.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
page.add_child(subheading)
grid = GridContainer.new()
grid.columns = WIDTH
grid.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
page.add_child(grid)
status = Label.new()
status.custom_minimum_size.x = 460
status.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
page.add_child(status)
_make_tiles()
_render()
func _make_tiles() -> void:
for index in range(WIDTH * HEIGHT):
var tile := Button.new()
tile.custom_minimum_size = Vector2(60, 48)
tile.focus_mode = Control.FOCUS_ALL
tile.pressed.connect(_tap_tile.bind(index))
tiles.append(tile)
grid.add_child(tile)
func _tap_tile(index: int) -> void:
if index == MIRROR:
orientation = 1 - orientation
_render()
func _to_index(cell: Vector2i) -> int:
return cell.y * WIDTH + cell.x
func _trace_beam() -> Array[int]:
var travelled: Array[int] = []
for column in range(3):
travelled.append(_to_index(Vector2i(column, 2)))
return travelled
func _render() -> void:
active_path = _trace_beam()
for index in range(tiles.size()):
var tile := tiles[index]
tile.text = _tile_label(index)
tile.modulate = Color("#9fece7") if active_path.has(index) else Color("#e8dcb9")
status.text = "Board addresses run from 0 to 24. Try turning tile 12."
func _tile_label(index: int) -> String:
if index == SOURCE:
return "Lamp"
if index == TARGET:
return "Goal"
if index == MIRROR:
return "/" if orientation == 0 else "\\"
if active_path.has(index):
return "Beam"
return str(index)
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 WIDTH := 5Creates a constant named WIDTH. The expression uses the whole number 5. Godot infers the type from the first value. -
const HEIGHT := 5Creates a constant named HEIGHT. The expression uses the whole number 5. Godot infers the type from the first value. -
const SOURCE := 10Creates a constant named SOURCE. The expression uses the whole number 10. Godot infers the type from the first value. -
const TARGET := 2Creates a constant named TARGET. The expression uses the whole number 2. Godot infers the type from the first value. -
const MIRROR := 12Creates a constant named MIRROR. The expression uses the whole number 12. Godot infers the type from the first value. -
const EAST := Vector2i(1, 0)Creates a constant named EAST. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value. -
const SOUTH := Vector2i(0, 1)Creates a constant named SOUTH. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value. -
const NORTH := Vector2i(0, -1)Creates a constant named NORTH. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value. -
var grid: GridContainerDeclares grid for later use. A colon followed by a type says which kind of value belongs here. -
var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here. -
var tiles: Array[Button] = []Creates a variable named tiles. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold. -
var active_path: Array[int] = []Creates a variable named active_path. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold. -
var orientation := 1Creates a variable named orientation. The expression uses the whole number 1. Godot infers the type from the first value. -
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. -
var background := ColorRect.new()Creates a variable named background. The expression creates a new ColorRect object. Godot infers the type from the first value. -
background.color = Color("#102a3b")Stores the new value in background.color. The expression builds a colour from these values. -
background.mouse_filter = Control.MOUSE_FILTER_IGNOREUpdates background.mouse_filter, which controls how this control participates in pointer and touch input. The expression reads Control.MOUSE_FILTER_IGNORE. -
add_child(background)Adds the new node to the scene tree so that Godot can run and draw it. -
background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Positions this Control relative to the bounds of its parent. -
var centre := CenterContainer.new()Creates a variable named centre. The expression creates a new CenterContainer object. Godot infers the type from the first value. -
add_child(centre)Adds the new node to the scene tree so that Godot can run and draw it. -
centre.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Positions this Control relative to the bounds of its parent. -
var page := VBoxContainer.new()Creates a variable named page. The expression creates a new VBoxContainer object. Godot infers the type from the first value. -
page.add_theme_constant_override("separation", 8)Calls page.add_theme_constant_override with the values in its parentheses. -
centre.add_child(page)Adds the new node to the scene tree so that Godot can run and draw it. -
var heading := Label.new()Creates a variable named heading. The expression creates a new Label object. Godot infers the type from the first value. -
heading.text = "Museum of Lost Lights"Updates heading.text, which controls the words a player sees. The expression uses the text inside the quotes. -
heading.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTERStores the new value in heading.horizontal_alignment. The expression reads HORIZONTAL_ALIGNMENT_CENTER. -
heading.add_theme_font_size_override("font_size", 26)Calls heading.add_theme_font_size_override with the values in its parentheses. -
page.add_child(heading)Adds the new node to the scene tree so that Godot can run and draw it. -
var subheading := Label.new()Creates a variable named subheading. The expression creates a new Label object. Godot infers the type from the first value. -
subheading.text = "Ocean Gallery · tap the mirror to turn it"Updates subheading.text, which controls the words a player sees. The expression uses the text inside the quotes. -
subheading.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTERStores the new value in subheading.horizontal_alignment. The expression reads HORIZONTAL_ALIGNMENT_CENTER. -
page.add_child(subheading)Adds the new node to the scene tree so that Godot can run and draw it. -
grid = GridContainer.new()Stores the new value in grid. The expression creates a new GridContainer object. -
grid.columns = WIDTHStores the new value in grid.columns. The expression reads WIDTH. -
grid.size_flags_horizontal = Control.SIZE_SHRINK_CENTERStores the new value in grid.size_flags_horizontal. The expression reads Control.SIZE_SHRINK_CENTER. -
page.add_child(grid)Adds the new node to the scene tree so that Godot can run and draw it. -
status = Label.new()Stores the new value in status. The expression creates a new Label object. -
status.custom_minimum_size.x = 460Stores the new value in status.custom_minimum_size.x. The expression uses the whole number 460. -
status.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTERStores the new value in status.horizontal_alignment. The expression reads HORIZONTAL_ALIGNMENT_CENTER. -
page.add_child(status)Adds the new node to the scene tree so that Godot can run and draw it. -
_make_tiles()Calls _make_tiles with the values in its parentheses. -
_render()Calls _render with the values in its parentheses. -
func _make_tiles() -> void:Defines make tiles, 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. -
for index in range(WIDTH * HEIGHT):Repeats the indented instructions for each value in range(WIDTH * HEIGHT). -
var tile := Button.new()Creates a variable named tile. The expression creates a new Button object. Godot infers the type from the first value. -
tile.custom_minimum_size = Vector2(60, 48)Updates tile.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value. -
tile.focus_mode = Control.FOCUS_ALLStores the new value in tile.focus_mode. The expression reads Control.FOCUS_ALL. -
tile.pressed.connect(_tap_tile.bind(index))Links a signal to the function that should run when the event happens. -
tiles.append(tile)Adds an item to the end of a list. -
grid.add_child(tile)Adds the new node to the scene tree so that Godot can run and draw it. -
func _tap_tile(index: int) -> void:Defines tap tile, 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 index == MIRROR:Checks index == MIRROR. The indented instructions below run only if this test passes. -
orientation = 1 - orientationStores the new value in orientation. The expression calculates a new value from the expressions on either side of the operator. -
_render()Calls _render with the values in its parentheses. -
func _to_index(cell: Vector2i) -> int:Defines to index, 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. -
return cell.y * WIDTH + cell.xFinishes the function and hands its result back to the caller. The expression calculates a new value from the expressions on either side of the operator. -
func _trace_beam() -> Array[int]:Defines trace beam, 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 travelled: Array[int] = []Creates a variable named travelled. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold. -
for column in range(3):Repeats the indented instructions for each value in range(3). -
travelled.append(_to_index(Vector2i(column, 2)))Adds an item to the end of a list. -
return travelledFinishes the function and hands its result back to the caller. The expression reads travelled. -
func _render() -> void:Defines render, 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. -
active_path = _trace_beam()Stores the new value in active_path. The expression calls _trace_beam with the values in its parentheses. -
for index in range(tiles.size()):Repeats the indented instructions for each value in range(tiles.size()). -
var tile := tiles[index]Creates a variable named tile. The expression looks up the item or key written between the square brackets. Godot infers the type from the first value. -
tile.text = _tile_label(index)Updates tile.text, which controls the words a player sees. The expression calls _tile_label with the values in its parentheses. -
tile.modulate = Color("#9fece7") if active_path.has(index) else Color("#e8dcb9")Updates tile.modulate, which controls the colour and transparency used to tint this node and its children. The expression builds a colour from these values. -
status.text = "Board addresses run from 0 to 24. Try turning tile 12."Updates status.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func _tile_label(index: int) -> String:Defines tile 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. -
if index == SOURCE:Checks index == SOURCE. The indented instructions below run only if this test passes. -
return "Lamp"Finishes the function and hands its result back to the caller. The expression uses the text inside the quotes. -
if index == TARGET:Checks index == TARGET. The indented instructions below run only if this test passes. -
return "Goal"Finishes the function and hands its result back to the caller. The expression uses the text inside the quotes. -
if index == MIRROR:Checks index == MIRROR. The indented instructions below run only if this test passes. -
return "/" if orientation == 0 else "\\"Finishes the function and hands its result back to the caller. The expression chooses one of two values by checking the condition in the middle. -
if active_path.has(index):Checks active_path.has(index). The indented instructions below run only if this test passes. -
return "Beam"Finishes the function and hands its result back to the caller. The expression uses the text inside the quotes. -
return str(index)Finishes the function and hands its result back to the caller. The expression turns a value into text for a player message.
Find and fix a bug
If the lights appear in a single column, check that grid.columns is WIDTH. If all squares turn the mirror, check that the if test compares index with MIRROR and contains the change inside its indented body.
Try a change
On paper, draw a three-column, four-row board and label all addresses. For a stretch, fork this project into a six-by-five board and work out new lamp, mirror and goal addresses before entering them.