Mobile Game 07 · Put three galleries in one array

Museum of Lost Lights · Lesson 07 of 50 · Beginner · About 38 min

The wall map can show Ocean, Fossil or Space. Write one button that moves between all three rooms.

A gouache painting of an Ocean Gallery with glass jellyfish exhibits, fossils and a deep teal floor.
What you’ll make Make a gallery selector that keeps the names in order. Use an array index to read each name and circle back to the first gallery.

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

Array holds the gallery names. Positions begin at zero. → Move the index. The Next button adds one. → Take the remainder. A full trip around the list resets to zero. → Display the room. Look up the chosen name in the array.
Array holds the gallery names → Move the index → Take the remainder → Display the room

Build it step by step

Language and tools in this lesson Array[String], index, size, remainder %, VBoxContainer, control layout, zero-based indexing.

  1. Place the controls in a vertical stack

    Import Lesson 07 and follow stack from VBoxContainer.new to the two stack.add_child calls. Run and see what appears above the button.

    var stack := VBoxContainer.new()

    Why it works A container arranges its child controls for you. A VBoxContainer arranges them from top to bottom. Its separation setting controls the space between neighbours.

  2. Read the array and the current index

    Read the three strings in GALLERIES. Number them 0, 1 and 2 on paper. Find the starting value of gallery_index.

    const GALLERIES: Array[String] = ["Ocean", "Fossil", "Space"]

    Why it works An array keeps values in order. Square brackets around the type say it holds Strings. An index is a position number. Godot starts array position numbers at zero.

  3. Match the displayed room to the index

    Read the line that sets sign.text. Trace how GALLERIES[gallery_index] finds the room name, while gallery_index + 1 gives the number a visitor expects.

    GALLERIES[gallery_index]

    Why it works Square brackets after the array read one item. %s asks for text. Each %d asks for a whole number. The outside percent sign reads each element of the supplied array in order and fills the gaps.

  4. Wrap the next index around

    Find where pressing the button calls show_next_gallery. If gallery_index is 2, calculate the parentheses first, then take the remainder after dividing by the array size.

    gallery_index = (gallery_index + 1) % GALLERIES.size()

    Why it works size() counts three names. Between numbers, % means remainder. 3 divided by 3 has remainder zero, so the next tap after Space returns to Ocean.

  5. Check that a fourth room fits

    Run and tap the button three times. Stop, add "Garden" after "Space" with a comma between them, and rerun. Try four taps and read the updated total.

    Why it works The display and wrap both read the array’s current size. The function can adapt to more rooms without hard-coded index limits.

Watch the values change

  1. Start the map Index 0 points to Ocean
    What you see. The game shows Ocean, 1 of 3.
  2. Tap once (0 + 1) % 3 is 1
    What you see. The game shows Fossil, 2 of 3.
  3. Tap again (1 + 1) % 3 is 2
    What you see. The game shows Space, 3 of 3.
  4. Tap from Space (2 + 1) % 3 is 0
    What you see. The game wraps to Ocean.

Predict before you run If there are five rooms, what is the new index after moving forward from index 4?

Reveal the answer

Zero, because (4 + 1) % 5 is zero. The next tap wraps to the first room.

Complete checkpoint

The selector starts at Ocean Gallery, 1 of 3. Three taps show Fossil, Space, then Ocean again.

main.gd

extends Control

const GALLERIES: Array[String] = ["Ocean", "Fossil", "Space"]
var gallery_index: int = 0
var sign: Label

func _ready() -> void:
	make_heading()
	var stack := VBoxContainer.new()
	stack.position = Vector2(54, 114)
	stack.add_theme_constant_override("separation", 16)
	add_child(stack)

	sign = Label.new()
	sign.add_theme_font_size_override("font_size", 27)
	stack.add_child(sign)

	var next_button := Button.new()
	next_button.text = "Next gallery"
	next_button.custom_minimum_size = Vector2(250, 64)
	next_button.add_theme_font_size_override("font_size", 24)
	next_button.pressed.connect(show_next_gallery)
	stack.add_child(next_button)
	refresh_sign()

func show_next_gallery() -> void:
	gallery_index = (gallery_index + 1) % GALLERIES.size()
	refresh_sign()

func refresh_sign() -> void:
	sign.text = "%s Gallery   •   %d of %d" % [GALLERIES[gallery_index], gallery_index + 1, GALLERIES.size()]

func make_heading() -> void:
	var heading := Label.new()
	heading.text = "Night shift · Gallery map"
	heading.position = Vector2(52, 36)
	heading.add_theme_font_size_override("font_size", 38)
	heading.add_theme_color_override("font_color", Color("#ffe1a8"))
	add_child(heading)

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 ControlThe scene root behaves like a Control, the kind of Godot node used for a game’s text, controls and other interface pieces.
  2. const GALLERIES: Array[String] = ["Ocean", "Fossil", "Space"]Hold the room names in order as Strings. Index zero names Ocean and index two names Space.
  3. var gallery_index: int = 0Store the position of the currently shown room. A fresh game begins at the first entry.
  4. var sign: LabelReserve a reference to the room label so later button actions can change it.
  5. func _ready() -> void:Godot automatically calls this setup function after bringing this node and its children into the scene. The code below it prepares the first frame.
  6. make_heading()Build the title before the room makes its other controls.
  7. var stack := VBoxContainer.new()Make a vertical container to arrange the sign and button in one column.
  8. stack.position = Vector2(54, 114)Use two numbers to give this control its position, measured right and down from the root.
  9. stack.add_theme_constant_override("separation", 16)Leave 16 pixels of space between children placed in this container.
  10. add_child(stack)Attach the container to the root. Its children will be displayed in the container’s area.
  11. sign = Label.new()Create the Label whose wording changes between galleries.
  12. sign.add_theme_font_size_override("font_size", 27)Set a larger or smaller font here without changing the typography on the other controls.
  13. stack.add_child(sign)Put the label first in the layout. The container will place later controls underneath it.
  14. var next_button := Button.new()Create the control that advances the current room index.
  15. next_button.text = "Next gallery"Updates next_button.text, which controls the words a player sees. The expression uses the text inside the quotes.
  16. next_button.custom_minimum_size = Vector2(250, 64)Updates next_button.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value.
  17. next_button.add_theme_font_size_override("font_size", 24)Set a larger or smaller font here without changing the typography on the other controls.
  18. next_button.pressed.connect(show_next_gallery)Connect a tap to the function that changes the gallery position.
  19. stack.add_child(next_button)Add the button as the second item, which the stack places underneath the sign.
  20. refresh_sign()Show the starting room while the index is still zero.
  21. func show_next_gallery() -> void:Group the update that moves forward and then redraws the sign.
  22. gallery_index = (gallery_index + 1) % GALLERIES.size()Add one, then take the remainder after dividing by the number of names. This wraps the last position back to zero.
  23. refresh_sign()Use the new index now that it has been wrapped into a valid position.
  24. func refresh_sign() -> void:Read the current position and display the matching room and human-friendly count.
  25. sign.text = "%s Gallery • %d of %d" % [GALLERIES[gallery_index], gallery_index + 1, GALLERIES.size()]Fill three gaps from the array after %. First read the gallery name at the current index, then count from 1 for the visitor, then show the total.
  26. func make_heading() -> void:Group the instructions that make the panel title. The room calls this once at startup.
  27. var heading := Label.new()Create a Label node and keep a reference under the name heading so the next lines all customise the same sign.
  28. heading.text = "Night shift · Gallery map"Show the name of this museum task above its controls.
  29. heading.position = Vector2(52, 36)Put the title 52 pixels to the right and 36 pixels down from the root’s upper-left corner.
  30. heading.add_theme_font_size_override("font_size", 38)Set this heading’s font size to 38. The other labels keep their own sizes.
  31. heading.add_theme_color_override("font_color", Color("#ffe1a8"))Give only this label a warm gold text colour. The six characters after # describe red, green and blue light.
  32. add_child(heading)Connect the title to the scene tree so Godot can display it.

Find and fix a bug

If the debugger says the index is out of bounds, check that the new index passes through the remainder calculation before it is used. An array of three items has no index 3.

Try a change

Ages 10–12: Add two of your own rooms and check the visitor numbers. Ages 13–16: Add a Previous gallery button. Add the list size before taking a remainder so the result stays in range when you move back from index zero.