Mobile Game 41 · Design a gallery that is yours

Museum of Lost Lights · Lesson 41 of 50 · Advanced · About 45 min

A moth exhibit is asleep in the midnight gallery. Your lantern can reach it only when your two mirrors both face the right way and a light gate opens.

A gouache painting of a Fossil Gallery with amber lights, tall prehistoric skeleton displays and a dark stone floor.
What you’ll make Design, test and explain a distinctive puzzle by adding a light-operated gate.

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/40 when the lesson builds on prior work, or copy the checkpoints/41 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.

Choose a mirror. Each button remembers its own list position. → Flip state. False becomes true, and true becomes false. → Check both values. Both must face the route. → Redraw the gallery. The state decides what the player sees.
Choose a mirror → Flip state → Check both values → Redraw the gallery

Build it step by step

Language and tools in this lesson const, Array[bool], bool, not, and, Callable.bind, for…in, range, modelling a new rule.

  1. Open the checkpoint in Godot

    Extract the course files. In Godot Project Manager, choose Import and point it to this lesson’s project.godot. Double-click main.gd in FileSystem to open the script. Press F6 to run the current scene once main.tscn is open, or F5 to run the project. Try one mirror at a time, and watch the gate.

    const UI = preload("res://lesson_ui.gd")

    Why it works This small project lets you see one release skill at a time. The finished Museum of Lost Lights is a second, independent project. Keep both projects in their own folders.

  2. Sketch your own gallery on paper

    Draw a 5 by 5 board. Mark a source, a destination, two mirror cells and two places where a light gate could sit. Add a character or exhibit of your own and name your gallery after it. Circle one new rule.

    Why it works A distinctive rule changes the puzzle a player solves. A simple rule you can explain with two pictures is easier to make and test.

  3. Write the first two mirror states

    In the Script editor, find rotations just below the constants. Each position stores whether that mirror has been turned. The model stores two false values at first.

    var rotations: Array[bool] = [false, false]

    Why it works Array[bool] is an ordered list of true-or-false values. The first value is rotations[0] because list positions start at zero.

  4. Connect each button to the mirror it turns

    Find the loop in _ready. Follow index through range(mirror_names.size()) and into _turn.bind(index). Click F5 and press the second mirror button. Check that only that label changes.

    var button := UI.button(column, MIRROR_NAMES[index], _turn.bind(index))

    Why it works range produces 0 and 1 here. bind remembers the index in the callable the button receives. The loop shares one function between both controls.

  5. Flip a mirror and decide when the gate opens

    Trace rotations[index] on both sides of the equals sign. Read the right side first. In _render, read both stored values and check them with and.

    rotations[index] = not rotations[index]

    Why it works not reverses one boolean. The and operator gives true only when both stored values are true. The game then shows the opened gate.

  6. Show which move changes the puzzle

    Click both mirrors. Undo the result by clicking either one again. Record the four separate combinations of the two booleans on paper, and tick one combination that lights the exhibit.

    var gate_open := rotations[0] and rotations[1]

    Why it works Listing every small input helps you spot an unsolvable design or a puzzle that opens on the first tap.

  7. Read the result, then apply it to your game

    Open a separate copy of the full Museum of Lost Lights project. Start by designing a level, an exhibit, artwork, a title and one original rule. Reuse the checkpoint’s idea in your own way. Test that the new rule changes at least two puzzle decisions.

    Why it works The checkpoint shows what to look for. Your game’s real result comes from testing your own build, assets and settings.

Watch the values change

  1. Start Mirror A false, mirror B false.
    What you see. The gate is shut.
  2. Turn mirror B A false, B true.
    What you see. The gate stays shut.
  3. Turn mirror A A true, B true.
    What you see. The gate opens and the moth lights up.
  4. Turn mirror B again A true, B false.
    What you see. The gate shuts again.

Predict before you run If mirror A is already true and you turn it again while mirror B is true, will the gate remain open?

Reveal the answer

No. Mirror A becomes false, so the and expression is false and the gate closes.

Complete checkpoint

Two buttons change their own mirror labels. Turning both makes the status read ‘Gate open. The moth exhibit is lit!’ Turning either again closes the gate.

main.gd

extends Control

const UI = preload("res://lesson_ui.gd")
const MIRROR_NAMES: Array[String] = ["Mirror A", "Mirror B"]
var rotations: Array[bool] = [false, false]
var controls: Array[Button] = []
var status: Label

func _ready() -> void:
	var column := UI.build(self, "The midnight gallery", "Turn both mirrors so their light can open the gallery gate.")
	for index in range(MIRROR_NAMES.size()):
		var button := UI.button(column, MIRROR_NAMES[index], _turn.bind(index))
		controls.append(button)
	status = UI.message(column, "")
	_render()

func _turn(index: int) -> void:
	rotations[index] = not rotations[index]
	_render()

func _render() -> void:
	for index in range(controls.size()):
		var direction := "towards gate" if rotations[index] else "away from gate"
		controls[index].text = MIRROR_NAMES[index] + ": " + direction
	var gate_open := rotations[0] and rotations[1]
	status.text = "Gate open. The moth exhibit is lit!" if gate_open else "Gate shut. The moth exhibit is dark."

lesson_ui.gd

extends RefCounted

static func build(screen: Control, heading: String, opening: String) -> VBoxContainer:
	var margins := MarginContainer.new()
	screen.add_child(margins)
	margins.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	for edge in ["left", "right", "top", "bottom"]:
		margins.add_theme_constant_override("margin_" + edge, 24)
	var scroll := ScrollContainer.new()
	margins.add_child(scroll)
	var column := VBoxContainer.new()
	column.size_flags_horizontal = Control.SIZE_EXPAND_FILL
	scroll.add_child(column)
	column.add_theme_constant_override("separation", 12)
	var title := Label.new()
	title.text = heading
	title.add_theme_font_size_override("font_size", 30)
	column.add_child(title)
	message(column, opening)
	return column

static func message(column: VBoxContainer, text: String) -> Label:
	var line := Label.new()
	line.text = text
	line.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	column.add_child(line)
	return line

static func button(column: Container, caption: String, action: Callable) -> Button:
	var control := Button.new()
	control.text = caption
	control.custom_minimum_size.y = 48
	control.pressed.connect(action)
	column.add_child(control)
	return control

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
  1. extends ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules.
  2. const UI = preload("res://lesson_ui.gd")Creates a constant named UI. The expression prepares a resource at a fixed project path when the script is loaded.
  3. const MIRROR_NAMES: Array[String] = ["Mirror A", "Mirror B"]Creates a constant named MIRROR_NAMES. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  4. var rotations: Array[bool] = [false, false]Creates a variable named rotations. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  5. var controls: Array[Button] = []Creates a variable named controls. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  6. var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here.
  7. 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.
  8. var column := UI.build(self, "The midnight gallery", "Turn both mirrors so their light can open the gallery gate.")Creates a variable named column. The expression calls UI.build with the values in its parentheses. Godot infers the type from the first value.
  9. for index in range(MIRROR_NAMES.size()):Repeats the indented instructions for each value in range(MIRROR_NAMES.size()).
  10. var button := UI.button(column, MIRROR_NAMES[index], _turn.bind(index))Creates a variable named button. The expression calls UI.button with the values in its parentheses. Godot infers the type from the first value.
  11. controls.append(button)Adds an item to the end of a list.
  12. status = UI.message(column, "")Stores the new value in status. The expression calls UI.message with the values in its parentheses.
  13. _render()Calls _render with the values in its parentheses.
  14. func _turn(index: int) -> void:Defines turn, 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.
  15. rotations[index] = not rotations[index]Stores the new value in rotations[index]. The expression looks up the item or key written between the square brackets.
  16. _render()Calls _render with the values in its parentheses.
  17. 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.
  18. for index in range(controls.size()):Repeats the indented instructions for each value in range(controls.size()).
  19. var direction := "towards gate" if rotations[index] else "away from gate"Creates a variable named direction. The expression chooses one of two values by checking the condition in the middle. Godot infers the type from the first value.
  20. controls[index].text = MIRROR_NAMES[index] + ": " + directionUpdates controls[index].text, which controls the words a player sees. The expression looks up the item or key written between the square brackets.
  21. var gate_open := rotations[0] and rotations[1]Creates a variable named gate_open. The expression evaluates the test to see whether it is true or false. Godot infers the type from the first value.
  22. status.text = "Gate open. The moth exhibit is lit!" if gate_open else "Gate shut. The moth exhibit is dark."Updates status.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle.
Read lesson_ui.gd line by line
  1. extends RefCountedStarts with Godot's RefCounted type. This script gets the abilities of that type and adds the game's own rules.
  2. static func build(screen: Control, heading: String, opening: String) -> VBoxContainer:Defines build, 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.
  3. var margins := MarginContainer.new()Creates a variable named margins. The expression creates a new MarginContainer object. Godot infers the type from the first value.
  4. screen.add_child(margins)Adds the new node to the scene tree so that Godot can run and draw it.
  5. margins.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Positions this Control relative to the bounds of its parent.
  6. for edge in ["left", "right", "top", "bottom"]:Repeats the indented instructions for each value in ["left", "right", "top", "bottom"].
  7. margins.add_theme_constant_override("margin_" + edge, 24)Calls margins.add_theme_constant_override with the values in its parentheses.
  8. var scroll := ScrollContainer.new()Creates a variable named scroll. The expression creates a new ScrollContainer object. Godot infers the type from the first value.
  9. margins.add_child(scroll)Adds the new node to the scene tree so that Godot can run and draw it.
  10. var column := VBoxContainer.new()Creates a variable named column. The expression creates a new VBoxContainer object. Godot infers the type from the first value.
  11. column.size_flags_horizontal = Control.SIZE_EXPAND_FILLStores the new value in column.size_flags_horizontal. The expression reads Control.SIZE_EXPAND_FILL.
  12. scroll.add_child(column)Adds the new node to the scene tree so that Godot can run and draw it.
  13. column.add_theme_constant_override("separation", 12)Calls column.add_theme_constant_override with the values in its parentheses.
  14. var title := Label.new()Creates a variable named title. The expression creates a new Label object. Godot infers the type from the first value.
  15. title.text = headingUpdates title.text, which controls the words a player sees. The expression reads heading.
  16. title.add_theme_font_size_override("font_size", 30)Calls title.add_theme_font_size_override with the values in its parentheses.
  17. column.add_child(title)Adds the new node to the scene tree so that Godot can run and draw it.
  18. message(column, opening)Calls message with the values in its parentheses.
  19. return columnFinishes the function and hands its result back to the caller. The expression reads column.
  20. static func message(column: VBoxContainer, text: String) -> Label:Defines message, 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.
  21. var line := Label.new()Creates a variable named line. The expression creates a new Label object. Godot infers the type from the first value.
  22. line.text = textUpdates line.text, which controls the words a player sees. The expression reads text.
  23. line.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTStores the new value in line.autowrap_mode. The expression reads TextServer.AUTOWRAP_WORD_SMART.
  24. column.add_child(line)Adds the new node to the scene tree so that Godot can run and draw it.
  25. return lineFinishes the function and hands its result back to the caller. The expression reads line.
  26. static func button(column: Container, caption: String, action: Callable) -> Button:Defines button, 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.
  27. var control := Button.new()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value.
  28. control.text = captionUpdates control.text, which controls the words a player sees. The expression reads caption.
  29. control.custom_minimum_size.y = 48Stores the new value in control.custom_minimum_size.y. The expression uses the whole number 48.
  30. control.pressed.connect(action)Links a signal to the function that should run when the event happens.
  31. column.add_child(control)Adds the new node to the scene tree so that Godot can run and draw it.
  32. return controlFinishes the function and hands its result back to the caller. The expression reads control.

Find and fix a bug

If both buttons always turn the same mirror, check that _turn.bind(index) sits inside the loop and that _turn uses its index parameter rather than a fixed cell.

Try a change

Ages 10–12: draw the four combinations with a ✓ beside the winning one. Ages 13–16: add a third mirror. Make another gate whose rules are different from the first one. List every winning combination before you code it.