Museum of Lost Lights · Lesson 14 of 50 · Beginner to intermediate · About 35 min
It’s time to wake the first museum exhibit. Let the completed beam path decide when the exhibit opens.

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/13 when the lesson builds on prior work, or copy the checkpoints/14 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 bool, Array.has, guard clause, break, game state, presentation.
-
Name the target that counts
Find TARGET near the top of the script and the target node label in _tile_label. Mark tile 2 on your paper grid and trace which direction would reach it from tile 12.
const TARGET := 2Why it works Putting one constant in both the rule and the drawing means they always refer to the same tile. This target is the top-row exhibit.
-
Let light stop at an awakened exhibit
Read _trace_beam in the order it runs. Find the append before the target test. Touch the paper target and keep it on your visited list even though the ray now stops there.
if spot == TARGET:Why it works The check ends the loop only after the target’s address has been recorded. Moving the test before append would hide the hit from the rule that checks for a win.
-
Ask the complete path whether it hit the target
Open _render and read the first two lines together. Test on paper with [10, 11, 12, 17, 22] and again with [10, 11, 12, 7, 2]. Then run the scene.
won = active_path.has(TARGET)Why it works Array.has returns a Boolean. won is false while the target is missing and true once the path includes it. _render makes a fresh calculation after every accepted tap.
-
Give the player two visible signals
Locate the target label’s conditional return and the status update. Run and flip the mirror. Check that Goal becomes Awake and the sentence below confirms the win.
return "Awake" if won else "Goal"Why it works The same Boolean changes two visible pieces. A shape, a word or a message helps users who cannot rely on the beam’s bright colour alone.
-
Close the room after a win
Read the top of _tap_tile. Run again, wake the exhibit and tap the mirror a second time. To retry this checkpoint, stop and run the scene again.
if won:Why it works A guard clause leaves the function before it changes the mirror. A completed puzzle remains visibly complete until a separate restart action resets it.
Watch the values change
-
Start a new game Path 10, 11, 12, 17, 22
What you see. The target 2 is absent and the exhibit is asleep. -
Turn mirror 12 Path 10, 11, 12, 7, 2
What you see. has(2) returns true and the exhibit wakes. -
Tap a solved mirror won is true
What you see. The guard returns early, keeping the win in view.
Predict before you run What changes if break happens before we append the target square?
Reveal the answer
The light will reach the target physically in the loop, but its number will be missing from active_path. The win check would say false.
Complete checkpoint
Tapping the mirror once wakes the exhibit and displays a victory caption. After winning, additional taps leave the puzzle solved.
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)
const WEST := Vector2i(-1, 0)
var grid: GridContainer
var status: Label
var tiles: Array[Button] = []
var active_path: Array[int] = []
var won := false
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 won:
return
if index == MIRROR:
orientation = 1 - orientation
_render()
func _to_index(cell: Vector2i) -> int:
return cell.y * WIDTH + cell.x
func _to_cell(index: int) -> Vector2i:
@warning_ignore("integer_division")
var row := index / WIDTH
return Vector2i(index % WIDTH, row)
func _inside(cell: Vector2i) -> bool:
return cell.x >= 0 and cell.y >= 0 and cell.x < WIDTH and cell.y < HEIGHT
func _trace_beam() -> Array[int]:
var travelled: Array[int] = []
var cell := _to_cell(SOURCE)
var heading: Vector2i = EAST
for step in range(WIDTH * HEIGHT * 4):
var spot := _to_index(cell)
travelled.append(spot)
if spot == MIRROR:
heading = _reflect(heading, orientation)
if spot == TARGET:
break
var next_cell := cell + heading
if not _inside(next_cell):
break
cell = next_cell
return travelled
func _reflect(heading: Vector2i, slant: int) -> Vector2i:
if slant == 0:
return Vector2i(-heading.y, -heading.x)
return Vector2i(heading.y, heading.x)
func _render() -> void:
active_path = _trace_beam()
won = active_path.has(TARGET)
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 = "Exhibit awake! Reset to play again." if won else "Turn the mirror to light the exhibit."
func _tile_label(index: int) -> String:
if index == SOURCE:
return "Lamp"
if index == TARGET:
return "Awake" if won else "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. -
const WEST := Vector2i(-1, 0)Creates a constant named WEST. 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 won := falseCreates a variable named won. The expression uses the yes-or-no value false. Godot infers the type from the first value. -
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 won:Checks won. The indented instructions below run only if this test passes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
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 _to_cell(index: int) -> Vector2i:Defines to cell, 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. -
@warning_ignore("integer_division")This annotation gives Godot extra information about the declaration that follows. -
var row := index / WIDTHCreates a variable named row. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value. -
return Vector2i(index % WIDTH, row)Finishes the function and hands its result back to the caller. The expression packs whole-number x and y into one grid position. -
func _inside(cell: Vector2i) -> bool:Defines inside, 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.x >= 0 and cell.y >= 0 and cell.x < WIDTH and cell.y < HEIGHTFinishes the function and hands its result back to the caller. The expression evaluates the test to see whether it is true or false. -
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. -
var cell := _to_cell(SOURCE)Creates a variable named cell. The expression calls _to_cell with the values in its parentheses. Godot infers the type from the first value. -
var heading: Vector2i = EASTCreates a variable named heading. The expression reads EAST. The type after the colon says what kind of value it can hold. -
for step in range(WIDTH * HEIGHT * 4):Repeats the indented instructions for each value in range(WIDTH * HEIGHT * 4). -
var spot := _to_index(cell)Creates a variable named spot. The expression calls _to_index with the values in its parentheses. Godot infers the type from the first value. -
travelled.append(spot)Adds an item to the end of a list. -
if spot == MIRROR:Checks spot == MIRROR. The indented instructions below run only if this test passes. -
heading = _reflect(heading, orientation)Stores the new value in heading. The expression calls _reflect with the values in its parentheses. -
if spot == TARGET:Checks spot == TARGET. The indented instructions below run only if this test passes. -
breakLeaves the loop immediately and continues with code after the loop. -
var next_cell := cell + headingCreates a variable named next_cell. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value. -
if not _inside(next_cell):Checks not _inside(next_cell). The indented instructions below run only if this test passes. -
breakLeaves the loop immediately and continues with code after the loop. -
cell = next_cellStores the new value in cell. The expression reads next_cell. -
return travelledFinishes the function and hands its result back to the caller. The expression reads travelled. -
func _reflect(heading: Vector2i, slant: int) -> Vector2i:Defines reflect, 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 slant == 0:Checks slant == 0. The indented instructions below run only if this test passes. -
return Vector2i(-heading.y, -heading.x)Finishes the function and hands its result back to the caller. The expression packs whole-number x and y into one grid position. -
return Vector2i(heading.y, heading.x)Finishes the function and hands its result back to the caller. The expression packs whole-number x and y into one grid position. -
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. -
won = active_path.has(TARGET)Stores the new value in won. The expression asks whether the collection contains the value or key. -
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 = "Exhibit awake! Reset to play again." if won else "Turn the mirror to light the exhibit."Updates status.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
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 "Awake" if won else "Goal"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 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 lamp touches the exhibit but the caption still says to turn the mirror, check that append happens before break and that TARGET is identical in the label and Array.has test.
Try a change
Change the target address to 22 and write down what you expect when the room opens. Stretch: discuss how you could set a starting slant that stays unsolved when the light starts.