Museum of Lost Lights · Lesson 38 of 50 · Advanced · About 36 min
The lantern's dashboard should stay readable on a small phone. Test a compact panel inside the editor before checking a real device.

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/37 when the lesson builds on prior work, or copy the checkpoints/38 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 BoxContainer, vertical, float, size, resized, size_flags_horizontal, if.
-
Open the layout checkpoint
Import lesson 38, run with F5 and find Turn, Undo and Hint. Try each control at the normal size.
Why it works The lab uses the same three controls on every screen. It changes their placement rather than creating new rules.
-
Use a box to arrange actions
Open main.gd and find controls. Follow the BoxContainer creation and its add_child line. Look at the separation override.
controls = BoxContainer.new()Why it works A BoxContainer arranges its children and the separation leaves a gap. One layout choice can move all three buttons.
-
Give controls room to expand
Find make_action. Compare what happens with and without Control.SIZE_EXPAND_FILL. Restore the line after testing.
button.size_flags_horizontal = Control.SIZE_EXPAND_FILLWhy it works SIZE_EXPAND_FILL asks a container to share spare horizontal space and lets the button fill its share. This gives each action a similar target.
-
Choose when to stack
Find adapt_controls and read the comparison out loud. Toggle compact preview, then try every action in the stack.
controls.vertical = available_width < NARROW_ROOMWhy it works A width below 520 selects the vertical layout. The controls keep their existing signals, because changing vertical only tells the same box to rearrange its children.
-
Listen for changes in size
Read the last two lines of _ready. If the running window lets you change width, drag the edge. If the stretch settings keep an internal layout size, use the provided compact preview.
resized.connect(adapt_controls)Why it works resized is a signal. The first explicit call fills the initial layout, while the signal handles a new size when Godot reports one.
-
Test a real small display
When you reach the export lessons, check touch targets and text on a real device. Include a narrow window and large text in your playtest notes.
Why it works A compact preview proves the arrangement changes. Physical devices also reveal screen margins, different pixel sizes and whether a thumb can reach each control.
Watch the values change
-
Run wide available_width at or above 520.
What you see. Turn, Undo and Hint sit on one row. -
Toggle compact preview available_width becomes 360 and vertical is true.
What you see. The three control targets stack. -
Press Hint Callable stays connected to Hint while layout changes.
What you see. Status confirms Hint works.
Predict before you run What does a width of exactly 520 choose with a less-than comparison?
Reveal the answer
It stays side by side. The expression is true only for widths strictly smaller than 520.
Complete checkpoint
Toggle the preview to alternate between a horizontal row and a vertical stack. All actions work in both arrangements.
main.gd
extends Control
const NARROW_ROOM := 520.0
var controls: BoxContainer
var status: Label
var simulate_phone := false
func _ready() -> void:
var panel := make_panel("38 · Fit a smaller screen", 240.0)
status = make_label(panel, "")
make_button(panel, "Toggle the compact preview", toggle_preview)
controls = BoxContainer.new()
controls.add_theme_constant_override("separation", 8)
panel.add_child(controls)
make_action("Turn")
make_action("Undo")
make_action("Hint")
resized.connect(adapt_controls)
adapt_controls()
func make_action(caption: String) -> void:
var button := make_button(controls, caption, show_action.bind(caption))
button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
func toggle_preview() -> void:
simulate_phone = not simulate_phone
adapt_controls()
func adapt_controls() -> void:
var available_width := 360.0 if simulate_phone else size.x
controls.vertical = available_width < NARROW_ROOM
status.text = "Stacked controls · compact layout" if controls.vertical else "Side-by-side controls · wide layout"
func show_action(caption: String) -> void:
status.text = "%s works in this layout." % caption
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. -
const NARROW_ROOM := 520.0Creates a constant named NARROW_ROOM. The expression reads 520.0. Godot infers the type from the first value. -
var controls: BoxContainerDeclares controls 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 simulate_phone := falseCreates a variable named simulate_phone. The expression uses the yes-or-no value false. Godot infers the type from the first value. -
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("38 · Fit a smaller screen", 240.0)Creates a variable named panel. The expression calls make_panel with the values in its parentheses. Godot infers the type from the first value. -
status = make_label(panel, "")Stores the new value in status. The expression calls make_label with the values in its parentheses. -
make_button(panel, "Toggle the compact preview", toggle_preview)Calls make_button with the values in its parentheses. -
controls = BoxContainer.new()Create a box that can hold all the action buttons and switch between vertical and horizontal arrangement. -
controls.add_theme_constant_override("separation", 8)Leave eight game pixels between the actions so their targets do not touch. -
panel.add_child(controls)Adds the new node to the scene tree so that Godot can run and draw it. -
make_action("Turn")Calls make_action with the values in its parentheses. -
make_action("Undo")Calls make_action with the values in its parentheses. -
make_action("Hint")Calls make_action with the values in its parentheses. -
resized.connect(adapt_controls)Ask the layout function to run when the root Control reports a new size. -
adapt_controls()Calls adapt_controls with the values in its parentheses. -
func make_action(caption: String) -> void:Defines make action, 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 button := make_button(controls, caption, show_action.bind(caption))Creates a variable named button. The expression calls make_button with the values in its parentheses. Godot infers the type from the first value. -
button.size_flags_horizontal = Control.SIZE_EXPAND_FILLTell the container this action can take a share of available width and use all of that share. -
func toggle_preview() -> void:Defines toggle preview, 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. -
simulate_phone = not simulate_phoneStores the new value in simulate_phone. The expression reads not simulate_phone. -
adapt_controls()Calls adapt_controls with the values in its parentheses. -
func adapt_controls() -> void:Defines adapt controls, 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 available_width := 360.0 if simulate_phone else size.xUse a 360-pixel width for the compact practice case. Otherwise read the actual horizontal width of the lesson root. -
controls.vertical = available_width < NARROW_ROOMChoose a vertical column if the available space is under the threshold. The same buttons keep their signals. -
status.text = "Stacked controls · compact layout" if controls.vertical else "Side-by-side controls · wide layout"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 show_action(caption: String) -> void:Defines show action, 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. -
status.text = "%s works in this layout." % captionUpdates status.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 your buttons keep lining up side by side in the compact preview, check that adapt_controls writes controls.vertical and that toggle_preview calls it.
Try a change
Ages 10–12 can test a new width limit and explain their choice. Ages 13–16 can add a settings option for a larger button minimum and verify layout at two sizes.