Museum of Lost Lights · Lesson 39 of 50 · Advanced · About 36 min
The museum's beam stays still between turns. Give Godot an update when there is a new beam to paint.

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/38 when the lesson builds on prior work, or copy the checkpoints/39 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 inner class, Control, _draw, Vector2, queue_redraw, return, bool.
-
Open the beam lab
Import the lesson project and run it with F5. Check where the cyan line ends, then choose Aim up.
Why it works The graphics show the visible result of one piece of state. Watching it helps you see when the rule and the picture agree.
-
Give beam drawing its own node
Look at the BeamPreview class at the top of main.gd. Scroll down to BeamPreview.new(). Find the two properties that belong to this object.
class BeamPreview extends Control:Why it works A class defines its own variables and functions. The main scene makes one instance and adds it to the scene tree, so Godot can ask it to draw.
-
Skip an unchanged request
In set_bend, follow the first if. Press Aim up twice in a row and compare the two messages.
if bends_left == next_bend: return falseWhy it works The equality comparison answers whether anything changed. The early return skips drawing work and sends false back to the caller.
-
Request a draw when it changes
Read the next three lines of set_bend. Identify which line sets the value, which line asks Godot to draw and which line reports success to the caller.
queue_redraw()Why it works queue_redraw schedules a draw. It lets the engine pick a good time to call _draw, which then reads the updated value.
-
Draw using a vector for each point
Find the three Vector2 values inside _draw. Trace start to mirror and mirror to finish in the code and the picture.
draw_line(mirror, finish, Color("#5ffff2"), 5.0, true)Why it works Vector2 stores x and y together. draw_line connects two points. The next line starts from the same mirror, so the two sections touch.
-
Compare counts on a steady window
Press Read the paint count and note the count. Press Aim down twice, allowing time between taps. Check the draw count again.
Why it works Only a fresh change requests a redraw here. Godot can also redraw for engine reasons, so expect the status messages to explain your requests even if window events affect the observed engine count.
Watch the values change
-
Run bends_left = false and the preview is shown.
What you see. The beam starts by turning down. -
Aim up State becomes true. A new draw is requested.
What you see. The beam bends up. -
Aim up again The existing and requested state are equal.
What you see. A status message confirms no redraw request.
Predict before you run Why is it easier to reason about this than moving the line independently inside each button callback?
Reveal the answer
Both actions go through one check and one stored value. The drawing function always reads that same value, which avoids the two controls disagreeing about the picture.
Complete checkpoint
The beam bends up and down, duplicate choices are skipped, and learners can check the visible count of completed draw calls.
main.gd
extends Control
class BeamPreview extends Control:
var bends_left := false
var paint_calls := 0
func set_bend(next_bend: bool) -> bool:
if bends_left == next_bend:
return false
bends_left = next_bend
queue_redraw()
return true
func _draw() -> void:
paint_calls += 1
var start := Vector2(48, 70)
var mirror := Vector2(190, 70)
var finish := Vector2(190, 26) if bends_left else Vector2(190, 112)
draw_line(start, mirror, Color("#5ffff2"), 5.0, true)
draw_line(mirror, finish, Color("#5ffff2"), 5.0, true)
draw_circle(mirror, 7.0, Color("#ffcc7e"))
var preview: BeamPreview
var readout: Label
func _ready() -> void:
var panel := make_panel("39 · Redraw the beam when it changes")
preview = BeamPreview.new()
preview.custom_minimum_size = Vector2(260, 138)
panel.add_child(preview)
readout = make_label(panel, "Pick a bend, then try the same one twice.")
make_button(panel, "Aim up", choose_bend.bind(true))
make_button(panel, "Aim down", choose_bend.bind(false))
make_button(panel, "Read the paint count", report_count)
func choose_bend(upwards: bool) -> void:
var changed := preview.set_bend(upwards)
readout.text = "Changed. Redraw requested." if changed else "Same beam. No redraw requested."
func report_count() -> void:
readout.text = "The beam was drawn %d times." % preview.paint_calls
func make_panel(heading: String, width: float = 580.0) -> VBoxContainer:
var margin := MarginContainer.new()
margin.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
for side in ["left", "right", "top", "bottom"]:
margin.add_theme_constant_override("margin_" + side, 24)
add_child(margin)
var centring := CenterContainer.new()
margin.add_child(centring)
var panel := VBoxContainer.new()
panel.custom_minimum_size.x = width
panel.add_theme_constant_override("separation", 12)
centring.add_child(panel)
make_label(panel, heading, 28)
return panel
func make_label(parent: Control, message: String, size: int = 18) -> Label:
var label := Label.new()
label.text = message
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
label.add_theme_font_size_override("font_size", size)
parent.add_child(label)
return label
func make_button(parent: Control, caption: String, handle_press: Callable) -> Button:
var button := Button.new()
button.text = caption
button.custom_minimum_size.y = 52
button.pressed.connect(handle_press)
parent.add_child(button)
return button
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 ControlGive this script the layout and input abilities of Godot's Control node. The code below turns the root Control into a particular museum practice scene. -
class BeamPreview extends Control:Define a small reusable Control inside this file. Each instance owns its bend state, draw count and picture. -
var bends_left := falseCreates a variable named bends_left. The expression uses the yes-or-no value false. Godot infers the type from the first value. -
var paint_calls := 0Creates a variable named paint_calls. The expression uses the whole number 0. Godot infers the type from the first value. -
func set_bend(next_bend: bool) -> bool:Defines set bend, 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 bends_left == next_bend:Compare the stored direction with the requested direction. A duplicate can return before asking the engine to draw. -
return falseFinishes the function and hands its result back to the caller. The expression uses the yes-or-no value false. -
bends_left = next_bendStore the different bend first so the drawing function can read the new value. -
queue_redraw()Schedule a draw callback for this preview. Drawing takes place after this function returns. -
return trueFinishes the function and hands its result back to the caller. The expression uses the yes-or-no value true. -
func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state. -
paint_calls += 1Each actual draw adds one to the diagnostic count so the player can observe what the engine drew. -
var start := Vector2(48, 70)Set the start at local x 48 and y 70. The point is measured from the top left of the preview. -
var mirror := Vector2(190, 70)Place the mirror farther right on the same row so the first beam segment runs horizontally. -
var finish := Vector2(190, 26) if bends_left else Vector2(190, 112)Keep the mirror's x coordinate and choose a finishing point above or below it. -
draw_line(start, mirror, Color("#5ffff2"), 5.0, true)Draw the first beam segment in cyan using a five-pixel line. True asks Godot to smooth its edges. -
draw_line(mirror, finish, Color("#5ffff2"), 5.0, true)Draw the second cyan line from the same mirror point to the chosen destination. Sharing the mirror point makes the lines join. -
draw_circle(mirror, 7.0, Color("#ffcc7e"))Paint an amber circle over the bend to show the mirror's position. -
var preview: BeamPreviewDeclares preview for later use. A colon followed by a type says which kind of value belongs here. -
var readout: LabelDeclares readout for later use. A colon followed by a type says which kind of value belongs here. -
func _ready() -> void:Godot calls this function once when the node is ready. The indented setup creates the interface and gives its controls their starting values. -
var panel := make_panel("39 · Redraw the beam when it changes")Creates a variable named panel. The expression calls make_panel with the values in its parentheses. Godot infers the type from the first value. -
preview = BeamPreview.new()Stores the new value in preview. The expression creates a new BeamPreview object. -
preview.custom_minimum_size = Vector2(260, 138)Updates preview.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value. -
panel.add_child(preview)Adds the new node to the scene tree so that Godot can run and draw it. -
readout = make_label(panel, "Pick a bend, then try the same one twice.")Stores the new value in readout. The expression calls make_label with the values in its parentheses. -
make_button(panel, "Aim up", choose_bend.bind(true))Calls make_button with the values in its parentheses. -
make_button(panel, "Aim down", choose_bend.bind(false))Calls make_button with the values in its parentheses. -
make_button(panel, "Read the paint count", report_count)Calls make_button with the values in its parentheses. -
func choose_bend(upwards: bool) -> void:Defines choose bend, 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 changed := preview.set_bend(upwards)Ask the preview to change direction and store whether it needed to request a redraw. -
readout.text = "Changed. Redraw requested." if changed else "Same beam. No redraw requested."Updates readout.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
func report_count() -> void:Defines report count, 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. -
readout.text = "The beam was drawn %d times." % preview.paint_callsUpdates readout.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func make_panel(heading: String, width: float = 580.0) -> VBoxContainer:Make a helper that takes the heading text and the panel's minimum width. If the caller omits the width, use 580 game pixels. Return the laid-out panel so the caller can add more controls. -
var margin := MarginContainer.new()Create a container that can reserve clear space between the game controls and the window edges. -
margin.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Choose the preset that aligns the margin's edges with all four edges of the parent Control. -
for side in ["left", "right", "top", "bottom"]:Visit the four names in order so the following indented line can set one margin on each trip through the loop. -
margin.add_theme_constant_override("margin_" + side, 24)Join margin_ to the current side's name. Set that edge to 24 game pixels, and repeat for the other sides. -
add_child(margin)Connect the completed margin container to the lesson's scene root so it can lay out its children. -
var centring := CenterContainer.new()Make a container that will centre the controls within the available space. -
margin.add_child(centring)Give the centring container the space inside the extra border, then let it place the panel within that space. -
var panel := VBoxContainer.new()Create a container that arranges its child controls in one vertical column. -
panel.custom_minimum_size.x = widthAsk the layout for at least the selected width, where x is the horizontal part of the size. -
panel.add_theme_constant_override("separation", 12)Leave 12 game pixels of space between each pair of controls in the vertical panel. -
centring.add_child(panel)Attach the vertical column to the centring container. Godot can now place the whole column together. -
make_label(panel, heading, 28)Put the supplied heading at the top of the column and ask for a larger 28-pixel font. -
return panelHand the panel reference back to the caller so it can add labels or buttons under the heading. -
func make_label(parent: Control, message: String, size: int = 18) -> Label:Accept the parent control, text to show and optional font size. Return the new Label so the caller can update it later. -
var label := Label.new()Make a new empty Label, which is Godot's text display node. -
label.text = messageCopy the incoming message into the label's visible words. -
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTLet the label wrap lines at sensible places so a long message can fit when the layout is narrow. -
label.add_theme_font_size_override("font_size", size)Set the chosen size on this one label so other text can keep different sizes. -
parent.add_child(label)Add the label to the supplied parent. It becomes part of that panel's layout and can appear on screen. -
return labelReturn the exact label you created so the caller can change its text without building a second label. -
func make_button(parent: Control, caption: String, handle_press: Callable) -> Button:Build and return one Button. The arguments identify its parent, the words on it and the action to run after pressing it. -
var button := Button.new()Create a Godot Button that supports pointer, touch and keyboard activation. -
button.text = captionPut the supplied caption on the visible control. -
button.custom_minimum_size.y = 52Reserve at least 52 game pixels vertically so the control remains a comfortable target. -
button.pressed.connect(handle_press)Connect the pressed signal to the supplied callable. Godot waits for activation before running the action. -
parent.add_child(button)Attach the Button to its parent and let that container position it. -
return buttonGive the finished Button to the caller, which can keep a reference to set focus or change the caption later.
Find and fix a bug
If the message says the bend changed but the beam looks the same, check both that queue_redraw ran and that _draw reads bends_left when it chooses the finish point.
Try a change
Ages 10–12 can vary the line width and read where it is used. Ages 13–16 can write down a frame rate observation with a stable window and compare re-drawing on every frame with this version.