Museum of Lost Lights · Lesson 12 of 50 · Beginner to intermediate · About 35 min
The lamp’s beam needs to walk across the floor without stepping through a wall at the edge. Teach it how to take one grid step.

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/11 when the lesson builds on prior work, or copy the checkpoints/12 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 Vector2i addition, integer division, % remainder, and, not, @warning_ignore, break, typed variables.
-
Recover the column and row
Open _to_cell under _to_index. For tile 12, divide by 5 and keep only the whole number 2. Then take the remainder 2. Use the returned pair (2, 2) to mark the square on your paper grid.
return Vector2i(index % WIDTH, row)Why it works The two operands are integers, so / here produces the whole-number quotient. % returns the leftover column. The warning annotation explains to the editor that the truncated division is deliberate.
-
Store east as a movement instruction
Read the directions near the top of main.gd. Put a finger on the lamp. Add one to the column and zero to the row. Point to the tile you land on.
const EAST := Vector2i(1, 0)Why it works Vector addition matches components. EAST is an ordered pair (1, 0), so adding it moves right one column while keeping the same row. The explicit Vector2i type tells you which values heading can hold.
-
Protect the board edges
Read the four comparisons in _inside. Give the function a pretend coordinate (5, 2). Check the comparisons left to right and mark which is false.
return cell.x >= 0 and cell.y >= 0 and cell.x < WIDTH and cell.y < HEIGHTWhy it works The and operator requires all four answers to be true. Valid columns are 0 through 4, so 5 < 5 is false. Guarding both row and column prevents the beam from appearing on the opposite edge by accident.
-
Move through one loop turn
Read _trace_beam. Start with cell (0, 2) and heading east. Append the current address, calculate next_cell and copy it into cell. Trace the first and second trips on paper.
var next_cell := cell + headingWhy it works A local variable keeps its new value for the next iteration. cell + heading prepares a candidate, whereas cell = next_cell commits the step after the edge check.
-
Stop at a mirror before turning is ready
Find the mirror test in _trace_beam. Count three loop iterations from the lamp. Press F6 and confirm that the beam’s three squares end at the mirror even if you tap it.
if spot == MIRROR:Why it works break immediately leaves this loop and lets return hand back the partial path. A second break catches the boundary. The finite range adds a maximum number of visits.
Watch the values change
-
Start at lamp 10 Cell (0, 2), heading (1, 0)
What you see. Append 10 and prepare cell (1, 2). -
Repeat along the floor Cell (1, 2)
What you see. Append 11 and prepare (2, 2). -
Visit the mirror Cell (2, 2), address 12
What you see. Append 12, then leave the loop.
Predict before you run If the beam is in cell (4, 2) and heads east, will its next address be appended?
Reveal the answer
No. The candidate cell is (5, 2), so _inside returns false. break happens before cell receives the candidate.
Complete checkpoint
Three tiles light in a row from the lamp to the mirror, using a step-by-step loop with an edge check. Tapping the mirror still changes only the slant 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 _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:
break
var next_cell := cell + heading
if not _inside(next_cell):
break
cell = next_cell
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 = "The first ray stops at the mirror while we teach it to turn."
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 _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. -
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 _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 = "The first ray stops at the mirror while we teach it to turn."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 light jumps between rows, check that _to_cell uses % for the column and whole-number division for the row, and that the loop calls _inside before assigning next_cell.
Try a change
Sketch WEST as a two-number movement. As a stretch, temporarily start the beam facing west and explain why the beam stops after only the lamp square.