Mobile Game 32 · Put clues beside the colours

Museum of Lost Lights · Lesson 32 of 50 · Intermediate to advanced · About 36 min

The fossil may wake in amber light, but the player should still know its state if amber and the room's background look similar.

A gouache painting of a Space Gallery with celestial exhibits, a midnight-blue floor and a domed ceiling.
What you’ll make Create an exhibit indicator that uses labels, shapes and colour, with a high-contrast setting.

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/31 when the lesson builds on prior work, or copy the checkpoints/32 folder to try this lesson on its own. Read the file names in the steps before editing.

Two Boolean settings. Exhibit state and colour preference live separately. → Read current state. Use if…else to choose icon, words and ink. → Render all clues. Update the label and button caption together.
Two Boolean settings → Read current state → Render all clues

Build it step by step

Language and tools in this lesson bool, not, if, conditional expressions, Color, add_theme_color_override.

  1. Open the indicator scene

    Import this lesson's checkpoint into Godot. Press F5. Before touching anything, read the status aloud. Identify the circle icon and the words that tell you the exhibit is asleep.

    Why it works An interface can share one fact in several ways. A player can use the shape, text or colour cue.

  2. Represent one yes-or-no fact

    Open main.gd. Find lit. Write false on paper, then write true beneath it. Click Toggle the exhibit twice.

    lit = not lit

    Why it works A Boolean stores either true or false. not flips the value and assignment keeps the flipped value for later.

  3. Decide the icon

    Find the symbol line in render_status. Read it aloud as choose the filled circle if lit is true, otherwise choose the empty circle.

    var symbol := "●" if lit else "○"

    Why it works An if…else expression produces one value on one line. The word choice uses the same lit value, so both give the same clue.

  4. Give contrast its own setting

    Press Turn high contrast on and compare the result while the exhibit is both awake and asleep. Find the indented ink line.

    ink = Color("#ffffff")

    Why it works This if statement only replaces the display colour. It leaves the puzzle's lit value intact, so turning on an aid never changes the puzzle.

  5. Update the label and button

    Find the last three lines of render_status. Highlight the line that combines shape and text before the line that applies ink.

    exhibit_status.add_theme_color_override("font_color", ink)

    Why it works One rendering function reads both settings and updates all the clues. The selected mode also changes the button's caption to say what pressing it will do next.

  6. Try it without relying on colour

    Ask someone to tell you the exhibit state from the words and shape first, then test whether they can find the contrast control using Tab.

    Why it works An observable test goes beyond guessing whether accessible options actually help a player.

Watch the values change

  1. Run lit = false, high_contrast = false.
    What you see. An empty circle and Unlit and sleeping appear.
  2. Toggle the exhibit lit becomes true.
    What you see. A filled circle and Lit and awake appear in amber.
  3. Turn contrast on high_contrast becomes true, lit remains true.
    What you see. The filled circle and words stay. The text turns white.

Predict before you run If the exhibit is asleep and you turn on high contrast, will the icon become a filled circle?

Reveal the answer

No. The empty circle comes from lit being false. High contrast only changes which ink colour is used.

Complete checkpoint

An exhibit state change affects its symbol, words and normal colour. High contrast changes the ink while the symbol and words stay accurate.

main.gd

extends Control

var lit := false
var high_contrast := false
var exhibit_status: Label
var contrast_button: Button

func _ready() -> void:
	var panel := make_panel("32 · Put clues beside the colours")
	make_label(panel, "Compare the icon and words in both colour modes.")
	exhibit_status = make_label(panel, "", 26)
	make_button(panel, "Toggle the exhibit", toggle_exhibit)
	contrast_button = make_button(panel, "Turn high contrast on", toggle_contrast)
	render_status()

func toggle_exhibit() -> void:
	lit = not lit
	render_status()

func toggle_contrast() -> void:
	high_contrast = not high_contrast
	render_status()

func render_status() -> void:
	var symbol := "●" if lit else "○"
	var words := "Lit and awake" if lit else "Unlit and sleeping"
	var ink := Color("#ffd37b") if lit else Color("#a9bdcb")
	if high_contrast:
		ink = Color("#ffffff")
	exhibit_status.text = "%s  %s" % [symbol, words]
	exhibit_status.add_theme_color_override("font_color", ink)
	contrast_button.text = "Turn high contrast off" if high_contrast else "Turn high contrast on"

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
  1. 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.
  2. var lit := falseCreates a variable named lit. The expression uses the yes-or-no value false. Godot infers the type from the first value.
  3. var high_contrast := falseCreates a variable named high_contrast. The expression uses the yes-or-no value false. Godot infers the type from the first value.
  4. var exhibit_status: LabelDeclares exhibit_status for later use. A colon followed by a type says which kind of value belongs here.
  5. var contrast_button: ButtonDeclares contrast_button for later use. A colon followed by a type says which kind of value belongs here.
  6. 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.
  7. var panel := make_panel("32 · Put clues beside the colours")Creates a variable named panel. The expression calls make_panel with the values in its parentheses. Godot infers the type from the first value.
  8. make_label(panel, "Compare the icon and words in both colour modes.")Calls make_label with the values in its parentheses.
  9. exhibit_status = make_label(panel, "", 26)Stores the new value in exhibit_status. The expression calls make_label with the values in its parentheses.
  10. make_button(panel, "Toggle the exhibit", toggle_exhibit)Calls make_button with the values in its parentheses.
  11. contrast_button = make_button(panel, "Turn high contrast on", toggle_contrast)Stores the new value in contrast_button. The expression calls make_button with the values in its parentheses.
  12. render_status()Calls render_status with the values in its parentheses.
  13. func toggle_exhibit() -> void:Defines toggle exhibit, 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.
  14. lit = not litFlip the exhibit variable. False becomes true and true becomes false.
  15. render_status()Calls render_status with the values in its parentheses.
  16. func toggle_contrast() -> void:Defines toggle contrast, 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.
  17. high_contrast = not high_contrastSwitch the display preference between its two states independently of the exhibit.
  18. render_status()Calls render_status with the values in its parentheses.
  19. func render_status() -> void:Defines render status, 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.
  20. var symbol := "●" if lit else "○"Use a filled circle for an awake exhibit and an open circle for a sleeping one.
  21. var words := "Lit and awake" if lit else "Unlit and sleeping"Use the same state to choose understandable words for readers who cannot distinguish the symbols or colours.
  22. var ink := Color("#ffd37b") if lit else Color("#a9bdcb")Choose warm amber ink for awake and cool grey-blue ink for sleeping. The prefix # marks a colour written in hexadecimal notation.
  23. if high_contrast:Only the indented colour adjustment runs when the contrast preference is switched on.
  24. ink = Color("#ffffff")Make the ink white in contrast mode while retaining the current exhibit state.
  25. exhibit_status.text = "%s %s" % [symbol, words]Place the chosen symbol and state text side by side, so both describe the same state.
  26. exhibit_status.add_theme_color_override("font_color", ink)Apply the calculated ink to this label's font without changing its content.
  27. contrast_button.text = "Turn high contrast off" if high_contrast else "Turn high contrast on"Label the next action. For example, when the setting is already on, offer to turn it off.
  28. 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.
  29. var margin := MarginContainer.new()Create a container that can reserve clear space between the game controls and the window edges.
  30. 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.
  31. 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.
  32. 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.
  33. add_child(margin)Connect the completed margin container to the lesson's scene root so it can lay out its children.
  34. var centring := CenterContainer.new()Make a container that will centre the controls within the available space.
  35. margin.add_child(centring)Give the centring container the space inside the extra border, then let it place the panel within that space.
  36. var panel := VBoxContainer.new()Create a container that arranges its child controls in one vertical column.
  37. panel.custom_minimum_size.x = widthAsk the layout for at least the selected width, where x is the horizontal part of the size.
  38. panel.add_theme_constant_override("separation", 12)Leave 12 game pixels of space between each pair of controls in the vertical panel.
  39. centring.add_child(panel)Attach the vertical column to the centring container. Godot can now place the whole column together.
  40. make_label(panel, heading, 28)Put the supplied heading at the top of the column and ask for a larger 28-pixel font.
  41. return panelHand the panel reference back to the caller so it can add labels or buttons under the heading.
  42. 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.
  43. var label := Label.new()Make a new empty Label, which is Godot's text display node.
  44. label.text = messageCopy the incoming message into the label's visible words.
  45. 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.
  46. label.add_theme_font_size_override("font_size", size)Set the chosen size on this one label so other text can keep different sizes.
  47. parent.add_child(label)Add the label to the supplied parent. It becomes part of that panel's layout and can appear on screen.
  48. return labelReturn the exact label you created so the caller can change its text without building a second label.
  49. 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.
  50. var button := Button.new()Create a Godot Button that supports pointer, touch and keyboard activation.
  51. button.text = captionPut the supplied caption on the visible control.
  52. button.custom_minimum_size.y = 52Reserve at least 52 game pixels vertically so the control remains a comfortable target.
  53. button.pressed.connect(handle_press)Connect the pressed signal to the supplied callable. Godot waits for activation before running the action.
  54. parent.add_child(button)Attach the Button to its parent and let that container position it.
  55. 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 contrast button changes its words but the label keeps its old colour, check that toggle_contrast calls render_status after changing the Boolean.

Try a change

Ages 10–12 can pick a square and a diamond for the two icons and check they remain recognisable. Ages 13–16 can add another independent setting for bigger text.