Mobile Game 21 · Send light down two paths

Museum of Lost Lights · Lesson 21 of 50 · Intermediate · About 32 min

The Ocean Gallery has two sleeping jellies and a single lantern. A splitter tile has two jobs. Keep one ray going and make another turn. You will make the puzzle show both routes, then rotate the splitter to wake both jellies.

A gouache painting of a Fossil Gallery with amber lights, tall prehistoric skeleton displays and a dark stone floor.
What you’ll make Build a splitter that sends the original light forward and a second ray towards another exhibit.

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

Lantern sends east. The queue starts with tile 6 and direction 1. → Splitter adds two jobs. Tile 9 has a straight branch and a reflected branch. → Build beam segments. Each job travels one safe step before queuing the next. → Update the gallery. Draw both paths and count the exhibits reached.
Lantern sends east → Splitter adds two jobs → Build beam segments → Update the gallery

Build it step by step

Language and tools in this lesson Array[Vector2i], Vector2i, append, pop_front, while, continue, if and else, Dictionary, function return values.

  1. Open the puzzle and predict

    Open the lesson’s start project in Godot. In FileSystem, double-click main.gd. Run the starting project with F6 or the Play Current Scene button. Stop it and look for _trace. Find the demonstration layout above _ready.

    Why it works The layout describes where the pieces start. The tracing function converts a layout into visible beam segments. Keeping the two jobs separate lets the same code run another layout.

  2. Put the first ray in a queue

    In _trace, give pending one starting ray. Its x value is the source tile number and its y value is the direction. Take the oldest ray off the front at the top of the while loop.

    var pending: Array[Vector2i] = [Vector2i(int(level.source), int(level.direction))]

    Why it works A Vector2i normally stores two whole-number coordinates. Here it carries a pair of facts about a ray. A queue is an ordered waiting list. While keeps handling rays until the list is empty.

  3. Take exactly one grid step

    Read ray.x and ray.y. Recover the column using %, recover the row using floori, then add the compass step. Use the bounds check above next_tile. Trace tile 8 east on paper before pressing Play.

    var next_position := coordinates + STEPS[direction]

    Why it works The remainder gives the column. Dividing gives a row number that is rounded down. The Rect2i test keeps coordinates inside the board. Continue skips the rest of this ray when it leaves.

  4. Copy the decision into two jobs

    After recording a segment and checking walls, read the next tile’s splitter setting. Add the straight job to pending. Look up the reflected compass direction in TURNS and add that job too.

    pending.append(Vector2i(next_tile, direction))

    Why it works Append puts each job at the back. Both branches now get a turn through the same loop. The reflected direction depends on the splitter’s orientation and the incoming direction.

  5. Return the evidence used by the screen

    Find the returned dictionary with segments, lit and loop. Working from the checkpoint, highlight the line that draws a segment and the two places that check lit. Then rotate the splitter and compare both readings.

    return {"segments": segments, "lit": lit, "loop": false}

    Why it works A returned value moves from _trace to _refresh. Drawing uses the segments. The exhibit counter and awake circles use the lit list. One trace supplies both pieces of feedback.

  6. Check each branch separately

    From Play, leave the splitter in its first direction and point to the beam that goes off the north edge. Tap it and point to the south branch. Keep a sketch of both possibilities.

    Why it works A drawing can show a faulty branch even if the other branch is correct. Checking each route makes debugging easier.

Watch the values change

  1. Run the room without touching anything. Tile 9 reflects north and also sends a straight ray east.
    What you see. The straight ray wakes tile 11. The north ray misses the jellies.
  2. Tap the splitter once. Orientation changes from 0 to 1. East still continues and the reflected ray travels south.
    What you see. Tiles 11 and 21 both wake.
  3. Tap the same tile again. Orientation returns to 0 and the trace starts fresh.
    What you see. Tile 21 goes back to sleep. The old result has been replaced.

Predict before you run If you turn the splitter, which jelly will stay awake regardless of its orientation?

Reveal the answer

The jelly on tile 11 stays awake because the straight east branch always leaves the splitter in the same direction.

Complete checkpoint

The room starts with one awake exhibit. Tap the single splitter and the second jelly wakes.

main.gd

extends Control

const STEPS: Array[Vector2i] = [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT]
const TURNS: Array = [[1, 0, 3, 2], [3, 2, 1, 0]]
const ORIGIN := Vector2(32, 139)
const PITCH := 78
const TILE := 70
const DEMO: Dictionary = {"title": "The sleepy jellies", "width": 6, "height": 4, "source": 6, "direction": 1, "mirrors": {}, "splitters": {"9": 0}, "targets": [11, 21], "walls": []}


var level: Dictionary
var turns: Dictionary = {}
var beam: Dictionary
var tile_buttons: Dictionary = {}
var board_layer: Control
var status: Label


func _ready() -> void:
	_add_label(Vector2(31, 21), "MUSEUM OF LOST LIGHTS", 29)
	_add_label(Vector2(32, 64), "Send light down two paths", 15)
	status = _add_label(Vector2(576, 174), "", 17)
	status.custom_minimum_size = Vector2(302, 141)
	status.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART

	board_layer = Control.new()
	board_layer.mouse_filter = Control.MOUSE_FILTER_IGNORE
	add_child(board_layer)
	_load_level(DEMO)

func _add_label(at: Vector2, text: String, text_size: int) -> Label:
	var label := Label.new()
	label.position = at
	label.text = text
	label.add_theme_font_size_override("font_size", text_size)
	label.add_theme_color_override("font_color", Color("#fff1d4"))
	add_child(label)
	return label

func _load_level(data: Dictionary) -> void:
	level = data
	turns.clear()

	tile_buttons.clear()
	for child in board_layer.get_children():
		child.queue_free()
	for tile in range(int(level.width) * int(level.height)):
		var is_mirror := _turn_at(level.mirrors, tile) >= 0
		var is_splitter := _turn_at(level.splitters, tile) >= 0
		if is_mirror or is_splitter:
			var button := Button.new()
			button.position = _cell(tile)
			button.size = Vector2(TILE, TILE)
			button.tooltip_text = "Rotate " + ("splitter" if is_splitter else "mirror")
			button.pressed.connect(_turn_tile.bind(tile))
			board_layer.add_child(button)
			tile_buttons[tile] = button
	_refresh()

func _turn_at(layout: Dictionary, tile: int) -> int:
	return int(layout.get(str(tile), -1))

func _orientation(tile: int, state: Dictionary) -> int:
	var start := _turn_at(level.mirrors, tile)
	if start < 0:
		start = _turn_at(level.splitters, tile)
	return int(state.get(tile, start))

func _turn_tile(tile: int) -> void:

	turns[tile] = 1 - _orientation(tile, turns)
	_refresh()


func _trace(state: Dictionary) -> Dictionary:
	var segments: Array[Vector2i] = []
	var lit: Array[int] = []
	var pending: Array[Vector2i] = [Vector2i(int(level.source), int(level.direction))]
	while not pending.is_empty():
		var ray: Vector2i = pending.pop_front()
		var tile := ray.x
		var direction := ray.y
		var coordinates := Vector2i(tile % int(level.width), floori(float(tile) / int(level.width)))
		var next_position := coordinates + STEPS[direction]
		if not Rect2i(0, 0, int(level.width), int(level.height)).has_point(next_position):
			continue
		var next_tile := next_position.y * int(level.width) + next_position.x
		segments.append(Vector2i(tile, next_tile))
		if level.targets.has(next_tile) and not lit.has(next_tile):
			lit.append(next_tile)
		if level.walls.has(next_tile):
			continue
		var splitter := _turn_at(level.splitters, next_tile)
		if splitter >= 0:
			pending.append(Vector2i(next_tile, direction))
			var branch: int = TURNS[_orientation(next_tile, state)][direction]
			pending.append(Vector2i(next_tile, branch))
		else:
			var mirror := _turn_at(level.mirrors, next_tile)
			var onward: int = TURNS[_orientation(next_tile, state)][direction] if mirror >= 0 else direction
			pending.append(Vector2i(next_tile, onward))
	return {"segments": segments, "lit": lit, "loop": false}

func _refresh() -> void:
	beam = _trace(turns)
	for tile in tile_buttons:
		var mirrored := _orientation(tile, turns)
		var button: Button = tile_buttons[tile]
		var glyph := "/" if mirrored == 0 else "\\"
		button.text = ("Split " if _turn_at(level.splitters, tile) >= 0 else "") + glyph
	var complete: bool = beam.lit.size() == level.targets.size()
	var message := "All exhibits awake!" if complete else "Tap a mirror or splitter to turn it."

	status.text = "%s\n\nExhibits awake: %d of %d\n%s" % [level.title, beam.lit.size(), level.targets.size(), message]

	queue_redraw()

func _cell(tile: int) -> Vector2:
	var col := tile % int(level.width)
	var row := floori(float(tile) / int(level.width))
	return ORIGIN + Vector2(col, row) * PITCH

func _center(tile: int) -> Vector2:
	return _cell(tile) + Vector2(TILE * 0.5, TILE * 0.5)

func _draw() -> void:
	draw_rect(Rect2(Vector2.ZERO, Vector2(900, 540)), Color("#132b3c"))

	if level.is_empty():
		return
	for column in range(int(level.width)):
		draw_string(ThemeDB.fallback_font, _cell(column) + Vector2(30, -8), str(column + 1), HORIZONTAL_ALIGNMENT_LEFT, -1, 13, Color("#f0d096"))
	for row in range(int(level.height)):
		draw_string(ThemeDB.fallback_font, _cell(row * int(level.width)) + Vector2(-17, 43), str(row + 1), HORIZONTAL_ALIGNMENT_LEFT, -1, 13, Color("#f0d096"))
	for tile in range(int(level.width) * int(level.height)):
		var box := Rect2(_cell(tile), Vector2(TILE, TILE))
		draw_rect(box, Color("#1d4859"))
		draw_rect(box, Color("#63858b"), false, 1.0)
		if tile == int(level.source):
			draw_circle(box.get_center(), 17.0, Color("#ffcc79"))
		elif level.targets.has(tile):
			var awake: bool = beam.get("lit", []).has(tile)
			draw_circle(box.get_center(), 16.0, Color("#9effd5") if awake else Color("#526580"))
			draw_arc(box.get_center(), 20.0, 0.0, TAU, 24, Color("#fbf0d1"), 2.0, true)
	for segment in beam.get("segments", []):
		draw_line(_center(segment.x), _center(segment.y), Color("#8cf5de"), 4.0, true)

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 STEPS: Array[Vector2i] = [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT]Creates a constant named STEPS. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  3. const TURNS: Array = [[1, 0, 3, 2], [3, 2, 1, 0]]Creates a constant named TURNS. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  4. const ORIGIN := Vector2(32, 139)Creates a constant named ORIGIN. The expression packs x and y into one value. Godot infers the type from the first value.
  5. const PITCH := 78Creates a constant named PITCH. The expression uses the whole number 78. Godot infers the type from the first value.
  6. const TILE := 70Creates a constant named TILE. The expression uses the whole number 70. Godot infers the type from the first value.
  7. const DEMO: Dictionary = {"title": "The sleepy jellies", "width": 6, "height": 4, "source": 6, "direction": 1, "mirrors": {}, "splitters": {"9": 0}, "targets": [11, 21], "walls": []}Creates a constant named DEMO. The expression makes a dictionary that looks up values by key. The type after the colon says what kind of value it can hold.
  8. var level: DictionaryDeclares level for later use. A colon followed by a type says which kind of value belongs here.
  9. var turns: Dictionary = {}Creates a variable named turns. The expression makes a dictionary that looks up values by key. The type after the colon says what kind of value it can hold.
  10. var beam: DictionaryDeclares beam for later use. A colon followed by a type says which kind of value belongs here.
  11. var tile_buttons: Dictionary = {}Creates a variable named tile_buttons. The expression makes a dictionary that looks up values by key. The type after the colon says what kind of value it can hold.
  12. var board_layer: ControlDeclares board_layer for later use. A colon followed by a type says which kind of value belongs here.
  13. var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here.
  14. 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.
  15. _add_label(Vector2(31, 21), "MUSEUM OF LOST LIGHTS", 29)Calls _add_label with the values in its parentheses.
  16. _add_label(Vector2(32, 64), "Send light down two paths", 15)Calls _add_label with the values in its parentheses.
  17. status = _add_label(Vector2(576, 174), "", 17)Stores the new value in status. The expression calls _add_label with the values in its parentheses.
  18. status.custom_minimum_size = Vector2(302, 141)Updates status.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value.
  19. status.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTStores the new value in status.autowrap_mode. The expression reads TextServer.AUTOWRAP_WORD_SMART.
  20. board_layer = Control.new()Stores the new value in board_layer. The expression creates a new Control object.
  21. board_layer.mouse_filter = Control.MOUSE_FILTER_IGNOREUpdates board_layer.mouse_filter, which controls how this control participates in pointer and touch input. The expression reads Control.MOUSE_FILTER_IGNORE.
  22. add_child(board_layer)Adds the new node to the scene tree so that Godot can run and draw it.
  23. _load_level(DEMO)Calls _load_level with the values in its parentheses.
  24. func _add_label(at: Vector2, text: String, text_size: int) -> Label:Defines add 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.
  25. var label := Label.new()Creates a variable named label. The expression creates a new Label object. Godot infers the type from the first value.
  26. label.position = atUpdates label.position, which controls the node's local x and y location. The expression reads at.
  27. label.text = textUpdates label.text, which controls the words a player sees. The expression reads text.
  28. label.add_theme_font_size_override("font_size", text_size)Calls label.add_theme_font_size_override with the values in its parentheses.
  29. label.add_theme_color_override("font_color", Color("#fff1d4"))Calls label.add_theme_color_override with the values in its parentheses.
  30. add_child(label)Adds the new node to the scene tree so that Godot can run and draw it.
  31. return labelFinishes the function and hands its result back to the caller. The expression reads label.
  32. func _load_level(data: Dictionary) -> void:Defines load level, 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.
  33. level = dataStores the new value in level. The expression reads data.
  34. turns.clear()Empties this collection.
  35. tile_buttons.clear()Empties this collection.
  36. for child in board_layer.get_children():Repeats the indented instructions for each value in board_layer.get_children().
  37. child.queue_free()Asks Godot to remove this node safely at the end of the frame.
  38. for tile in range(int(level.width) * int(level.height)):Repeats the indented instructions for each value in range(int(level.width) * int(level.height)).
  39. var is_mirror := _turn_at(level.mirrors, tile) >= 0Creates a variable named is_mirror. The expression calls _turn_at with the values in its parentheses. Godot infers the type from the first value.
  40. var is_splitter := _turn_at(level.splitters, tile) >= 0Creates a variable named is_splitter. The expression calls _turn_at with the values in its parentheses. Godot infers the type from the first value.
  41. if is_mirror or is_splitter:Checks is_mirror or is_splitter. The indented instructions below run only if this test passes.
  42. var button := Button.new()Creates a variable named button. The expression creates a new Button object. Godot infers the type from the first value.
  43. button.position = _cell(tile)Updates button.position, which controls the node's local x and y location. The expression calls _cell with the values in its parentheses.
  44. button.size = Vector2(TILE, TILE)Updates button.size, which controls the control's width and height. The expression packs x and y into one value.
  45. button.tooltip_text = "Rotate " + ("splitter" if is_splitter else "mirror")Stores the new value in button.tooltip_text. The expression chooses one of two values by checking the condition in the middle.
  46. button.pressed.connect(_turn_tile.bind(tile))Links a signal to the function that should run when the event happens.
  47. board_layer.add_child(button)Adds the new node to the scene tree so that Godot can run and draw it.
  48. tile_buttons[tile] = buttonStores the new value in tile_buttons[tile]. The expression reads button.
  49. _refresh()Calls _refresh with the values in its parentheses.
  50. func _turn_at(layout: Dictionary, tile: int) -> int:Defines turn at, 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.
  51. return int(layout.get(str(tile), -1))Finishes the function and hands its result back to the caller. The expression converts a suitable value to a whole number.
  52. func _orientation(tile: int, state: Dictionary) -> int:Defines orientation, 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.
  53. var start := _turn_at(level.mirrors, tile)Creates a variable named start. The expression calls _turn_at with the values in its parentheses. Godot infers the type from the first value.
  54. if start < 0:Checks start < 0. The indented instructions below run only if this test passes.
  55. start = _turn_at(level.splitters, tile)Stores the new value in start. The expression calls _turn_at with the values in its parentheses.
  56. return int(state.get(tile, start))Finishes the function and hands its result back to the caller. The expression converts a suitable value to a whole number.
  57. func _turn_tile(tile: int) -> void:Defines turn 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.
  58. turns[tile] = 1 - _orientation(tile, turns)Stores the new value in turns[tile]. The expression calculates a new value from the expressions on either side of the operator.
  59. _refresh()Calls _refresh with the values in its parentheses.
  60. func _trace(state: Dictionary) -> Dictionary:Defines trace, 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.
  61. var segments: Array[Vector2i] = []Creates a variable named segments. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  62. var lit: Array[int] = []Creates a variable named lit. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  63. var pending: Array[Vector2i] = [Vector2i(int(level.source), int(level.direction))]Creates a variable named pending. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  64. while not pending.is_empty():Keeps repeating while not pending.is_empty() stays true. The loop body should eventually change the result or leave with break.
  65. var ray: Vector2i = pending.pop_front()Creates a variable named ray. The expression takes the first item out of the list and returns it. The type after the colon says what kind of value it can hold.
  66. var tile := ray.xCreates a variable named tile. The expression reads ray.x. Godot infers the type from the first value.
  67. var direction := ray.yCreates a variable named direction. The expression reads ray.y. Godot infers the type from the first value.
  68. var coordinates := Vector2i(tile % int(level.width), floori(float(tile) / int(level.width)))Creates a variable named coordinates. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value.
  69. var next_position := coordinates + STEPS[direction]Creates a variable named next_position. The expression looks up the item or key written between the square brackets. Godot infers the type from the first value.
  70. if not Rect2i(0, 0, int(level.width), int(level.height)).has_point(next_position):Checks not Rect2i(0, 0, int(level.width), int(level.height)).has_point(next_position). The indented instructions below run only if this test passes.
  71. continueSkips the rest of this turn through the loop and moves on to the next item.
  72. var next_tile := next_position.y * int(level.width) + next_position.xCreates a variable named next_tile. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value.
  73. segments.append(Vector2i(tile, next_tile))Adds an item to the end of a list.
  74. if level.targets.has(next_tile) and not lit.has(next_tile):Checks level.targets.has(next_tile) and not lit.has(next_tile). The indented instructions below run only if this test passes.
  75. lit.append(next_tile)Adds an item to the end of a list.
  76. if level.walls.has(next_tile):Checks level.walls.has(next_tile). The indented instructions below run only if this test passes.
  77. continueSkips the rest of this turn through the loop and moves on to the next item.
  78. var splitter := _turn_at(level.splitters, next_tile)Creates a variable named splitter. The expression calls _turn_at with the values in its parentheses. Godot infers the type from the first value.
  79. if splitter >= 0:Checks splitter >= 0. The indented instructions below run only if this test passes.
  80. pending.append(Vector2i(next_tile, direction))Adds an item to the end of a list.
  81. var branch: int = TURNS[_orientation(next_tile, state)][direction]Creates a variable named branch. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold.
  82. pending.append(Vector2i(next_tile, branch))Adds an item to the end of a list.
  83. else:Runs the indented fallback instructions if none of the earlier tests passed.
  84. var mirror := _turn_at(level.mirrors, next_tile)Creates a variable named mirror. The expression calls _turn_at with the values in its parentheses. Godot infers the type from the first value.
  85. var onward: int = TURNS[_orientation(next_tile, state)][direction] if mirror >= 0 else directionCreates a variable named onward. The expression chooses one of two values by checking the condition in the middle. The type after the colon says what kind of value it can hold.
  86. pending.append(Vector2i(next_tile, onward))Adds an item to the end of a list.
  87. return {"segments": segments, "lit": lit, "loop": false}Finishes the function and hands its result back to the caller. The expression makes a dictionary that looks up values by key.
  88. func _refresh() -> void:Defines refresh, 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.
  89. beam = _trace(turns)Stores the new value in beam. The expression calls _trace with the values in its parentheses.
  90. for tile in tile_buttons:Repeats the indented instructions for each value in tile_buttons.
  91. var mirrored := _orientation(tile, turns)Creates a variable named mirrored. The expression calls _orientation with the values in its parentheses. Godot infers the type from the first value.
  92. var button: Button = tile_buttons[tile]Creates a variable named button. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold.
  93. var glyph := "/" if mirrored == 0 else "\\"Creates a variable named glyph. The expression chooses one of two values by checking the condition in the middle. Godot infers the type from the first value.
  94. button.text = ("Split " if _turn_at(level.splitters, tile) >= 0 else "") + glyphUpdates button.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle.
  95. var complete: bool = beam.lit.size() == level.targets.size()Creates a variable named complete. The expression counts the items in this collection. The type after the colon says what kind of value it can hold.
  96. var message := "All exhibits awake!" if complete else "Tap a mirror or splitter to turn it."Creates a variable named message. The expression chooses one of two values by checking the condition in the middle. Godot infers the type from the first value.
  97. status.text = "%s\n\nExhibits awake: %d of %d\n%s" % [level.title, beam.lit.size(), level.targets.size(), message]Updates status.text, which controls the words a player sees. The expression uses the text inside the quotes.
  98. queue_redraw()Asks Godot to call _draw again on a later frame so the screen matches the new state.
  99. func _cell(tile: int) -> Vector2:Defines 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.
  100. var col := tile % int(level.width)Creates a variable named col. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value.
  101. var row := floori(float(tile) / int(level.width))Creates a variable named row. The expression calls floori with the values in its parentheses. Godot infers the type from the first value.
  102. return ORIGIN + Vector2(col, row) * PITCHFinishes 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.
  103. func _center(tile: int) -> Vector2:Defines center, 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.
  104. return _cell(tile) + Vector2(TILE * 0.5, TILE * 0.5)Finishes the function and hands its result back to the caller. The expression calls _cell with the values in its parentheses.
  105. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  106. draw_rect(Rect2(Vector2.ZERO, Vector2(900, 540)), Color("#132b3c"))Paints a rectangle at the given position and size.
  107. if level.is_empty():Checks level.is_empty(). The indented instructions below run only if this test passes.
  108. returnFinishes the function right here. Any later instructions in this function are skipped.
  109. for column in range(int(level.width)):Repeats the indented instructions for each value in range(int(level.width)).
  110. draw_string(ThemeDB.fallback_font, _cell(column) + Vector2(30, -8), str(column + 1), HORIZONTAL_ALIGNMENT_LEFT, -1, 13, Color("#f0d096"))Paints a piece of text with a font at a position measured from the node's origin.
  111. for row in range(int(level.height)):Repeats the indented instructions for each value in range(int(level.height)).
  112. draw_string(ThemeDB.fallback_font, _cell(row * int(level.width)) + Vector2(-17, 43), str(row + 1), HORIZONTAL_ALIGNMENT_LEFT, -1, 13, Color("#f0d096"))Paints a piece of text with a font at a position measured from the node's origin.
  113. for tile in range(int(level.width) * int(level.height)):Repeats the indented instructions for each value in range(int(level.width) * int(level.height)).
  114. var box := Rect2(_cell(tile), Vector2(TILE, TILE))Creates a variable named box. The expression describes a position and a size. Godot infers the type from the first value.
  115. draw_rect(box, Color("#1d4859"))Paints a rectangle at the given position and size.
  116. draw_rect(box, Color("#63858b"), false, 1.0)Paints a rectangle at the given position and size.
  117. if tile == int(level.source):Checks tile == int(level.source). The indented instructions below run only if this test passes.
  118. draw_circle(box.get_center(), 17.0, Color("#ffcc79"))Paints a filled circle or outline in this node's local coordinates.
  119. elif level.targets.has(tile):If the earlier branches did not run, checks level.targets.has(tile) next.
  120. var awake: bool = beam.get("lit", []).has(tile)Creates a variable named awake. The expression looks up a named or indexed value, using a fallback when the call gives one. The type after the colon says what kind of value it can hold.
  121. draw_circle(box.get_center(), 16.0, Color("#9effd5") if awake else Color("#526580"))Paints a filled circle or outline in this node's local coordinates.
  122. draw_arc(box.get_center(), 20.0, 0.0, TAU, 24, Color("#fbf0d1"), 2.0, true)Calls draw_arc with the values in its parentheses.
  123. for segment in beam.get("segments", []):Repeats the indented instructions for each value in beam.get("segments", []).
  124. draw_line(_center(segment.x), _center(segment.y), Color("#8cf5de"), 4.0, true)Paints a line between two points with the chosen colour and width.

Find and fix a bug

If both branches look identical, check that the second job uses branch rather than direction. If the beam crosses a board edge, check the Rect2i guard runs before the tile-number calculation.

Try a change

Guided: draw an arrow for each direction in STEPS. Stretch: move the east jelly to another valid tile on its path and explain when it lights.