Tiny Shelf Studio · Lesson 06 of 50 · Beginner · About 20 min
A visitor should be able to pick out the top-left carton or jump to the bottom-right corner. Both buttons can use the same selection rule.

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/05 when the lesson builds on prior work, or copy the checkpoints/06 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 Button.new, Callable, pressed, connect, bind, queue_redraw.
-
Start at the first slot
Give selected the value Vector2i.ZERO instead of (2, 1). Keep the twelve-slot drawing loop.
var selected := Vector2i.ZEROWhy it works Vector2i.ZERO is the named whole-number pair (0, 0). The first grid space is selected at launch.
-
Create a button helper
Add _add_button below the other functions. Read the parameters as the label, screen point and instruction to call later.
func _add_button(caption: String, point: Vector2, action: Callable) -> void:Why it works A Callable represents a function which can be run later. The helper can accept a different one for each button.
-
Tell the button what to do
Inside the helper, create a Button, set its caption and size, connect its pressed signal and add the node to the screen.
var button := Button.new() button.text = caption button.position = point button.custom_minimum_size = Vector2(90, 46) button.pressed.connect(action) add_child(button)Why it works pressed is an announcement when the player activates the button. connect tells Godot which instruction should receive it.
-
Write the selection rule
Add _select. Set selected to the address passed in, refresh the bulletin and request a redraw.
func _select(cell: Vector2i) -> void: selected = cell _refresh() queue_redraw()Why it works One state value drives both what the text says and where the gold outline appears. queue_redraw asks Godot to draw the scene again.
-
Bind a coordinate to each button
In _ready, after refreshing, call _add_button twice. Use the two calls below to place them on separate lines in the workbench.
_add_button("Top left", Vector2(598, 240), _select.bind(Vector2i(0, 0))) _add_button("Bottom right", Vector2(598, 298), _select.bind(Vector2i(3, 2)))Why it works bind saves an argument until the button is activated. A press on Bottom right will pass (3, 2) to _select.
-
Test one round trip
Press F5. Click Bottom right, then Top left. Try Tab and Enter to activate one of the buttons as well.
Why it works Both inputs use the pressed signal. Compare the bulletin with the border and the ID printed on each space.
Watch the values change
-
Start the scene. selected is (0, 0).
What you see. The border begins around slot 00. -
Click Bottom right. _select receives (3, 2).
What you see. The border moves to slot 11 and the bulletin reads column 3, row 2. -
Click Top left. _select receives (0, 0).
What you see. The border and text return to their first values.
Predict before you run What happens if both buttons bind (3, 2)?
Reveal the answer
Both choose the bottom-right slot. The caption tells people what to expect. The argument decides what the button does.
Complete checkpoint
Top left selects slot 00 and Bottom right selects slot 11. Both buttons report matching coordinates.
main.gd
extends Control
const NAVY := Color("#18243d")
const PAPER := Color("#fff5e7")
const SHELF := Color("#302e4b")
const CREAM := Color("#fff2d3")
const PINK := Color("#f58a9d")
const MINT := Color("#b4edce")
const GOLD := Color("#ffd575")
const COLUMNS := 4
const ROWS := 3
const ORIGIN := Vector2(68, 171)
const TILE := Vector2(116, 98)
var selected := Vector2i.ZERO
var bulletin: Label
func _ready() -> void:
make_label("TINY SHELF STUDIO", Vector2(49, 30), 35, NAVY)
bulletin = make_label("", Vector2(51, 81), 21, NAVY)
_refresh()
make_label("Choose a shelf slot", Vector2(598, 181), 23, NAVY)
_add_button("Top left", Vector2(598, 240), _select.bind(Vector2i(0, 0)))
_add_button("Bottom right", Vector2(598, 298), _select.bind(Vector2i(3, 2)))
func make_label(text: String, point: Vector2, font_size: int, ink: Color) -> Label:
var label := Label.new()
label.text = text
label.position = point
label.modulate = ink
label.add_theme_font_size_override("font_size", font_size)
add_child(label)
return label
func _draw() -> void:
draw_rect(get_viewport_rect(), PAPER)
draw_rect(Rect2(48, 126, 502, 360), SHELF)
draw_rect(Rect2(577, 126, 334, 360), Color("#f1ded4"))
draw_rect(Rect2(48, 126, 8, 360), PINK)
print_on_shelf("TRAINING DISPLAY", Vector2(67, 153), 14, MINT)
print_on_shelf("DISPLAY WORKBENCH", Vector2(597, 155), 14, NAVY)
print_on_shelf("Quesbo Coding / Godot practice", Vector2(49, 520), 16, NAVY)
for row in range(ROWS):
for column in range(COLUMNS):
var cell := Vector2i(column, row)
var point := cell_origin(cell)
var slot := Rect2(point, TILE - Vector2(9, 9))
draw_rect(slot, Color("#554a69"))
var index := cell_to_index(cell)
print_on_shelf("%02d" % index, point + Vector2(9, 19), 13, CREAM)
if cell == selected:
draw_rect(slot, GOLD, false, 3.0)
func print_on_shelf(text: String, point: Vector2, font_size: int, ink: Color) -> void:
draw_string(ThemeDB.fallback_font, point, text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, ink)
func cell_origin(cell: Vector2i) -> Vector2:
return ORIGIN + Vector2(cell) * TILE
func _refresh() -> void:
bulletin.text = "Selected column %d, row %d" % [selected.x, selected.y]
func cell_to_index(cell: Vector2i) -> int:
return cell.y * COLUMNS + cell.x
func _add_button(caption: String, point: Vector2, action: Callable) -> void:
var button := Button.new()
button.text = caption
button.position = point
button.custom_minimum_size = Vector2(90, 46)
button.pressed.connect(action)
add_child(button)
func _select(cell: Vector2i) -> void:
selected = cell
_refresh()
queue_redraw()
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 ControlBase this script on a Godot Control. It can assemble text and buttons, receive input and draw directly into the scene. -
const NAVY := Color("#18243d")Give headings and package shadows a shared dark ink. The six-digit colour is read from a hexadecimal string. -
const PAPER := Color("#fff5e7")Use this warm, light colour behind the shelf and on front labels. -
const SHELF := Color("#302e4b")Set the dark display board colour. A constant lets later drawings refer to it by name. -
const CREAM := Color("#fff2d3")Use a light ink for numbers and letters placed against the dark display. -
const PINK := Color("#f58a9d")Add a warm edge to the display. This colour does not decide whether a slot is occupied. -
const MINT := Color("#b4edce")Use a light green marker in the workbench styling. -
const GOLD := Color("#ffd575")Reserve a high-contrast yellow for the frame around the selected slot. -
const COLUMNS := 4Set the shelf width to four slots. It also determines the step from the start of one row to the next in slot IDs. -
const ROWS := 3Limit the shelf to three rows. The valid row addresses are 0, 1 and 2. -
const ORIGIN := Vector2(68, 171)Place slot (0, 0) at x 68 and y 171 in the game window. -
const TILE := Vector2(116, 98)Give each grid location 116 pixels of width and 98 of height, including the gap before the next visible slot. -
var selected := Vector2i.ZEROStart at the top left. ZERO means the integer pair (0, 0). -
var bulletin: LabelKeep a reference to the text node so other functions can edit its words. -
func _ready() -> void:Godot calls this when the Control enters the ready scene. Set up the initial title, labels and buttons here. -
make_label("TINY SHELF STUDIO", Vector2(49, 30), 35, NAVY)Put the studio name near the top-left corner. The title uses 35-pixel dark text on the light background. -
bulletin = make_label("", Vector2(51, 81), 21, NAVY)Create a second label and keep it in bulletin. You can set its text after placement. -
_refresh()Synchronise the main bulletin with the current column and row. -
make_label("Choose a shelf slot", Vector2(598, 181), 23, NAVY)Give the workbench one explanatory caption at the requested pixel position. -
_add_button("Top left", Vector2(598, 240), _select.bind(Vector2i(0, 0)))Bind slot (0, 0) to the button. This argument is held until pressed. -
_add_button("Bottom right", Vector2(598, 298), _select.bind(Vector2i(3, 2)))Bind slot (3, 2) to the button. This is the last address in row-major order. -
func make_label(text: String, point: Vector2, font_size: int, ink: Color) -> Label:Accept a string, a point, a font size and an ink colour. This helper returns the new Label so the caller can keep updating it. -
var label := Label.new()Create a Label object. It needs to be added to the running scene before it can appear. -
label.text = textPut the helper's text argument into the new Label. -
label.position = pointPosition the label using the x and y pixel coordinates passed into the helper. -
label.modulate = inkTint the label with the colour passed into this call. -
label.add_theme_font_size_override("font_size", font_size)Override the theme's size for just this label. -
add_child(label)Make Godot manage and draw the new Label as a child of the Control. -
return labelGive the caller the Label for later changes. -
func _draw() -> void:Godot calls this when the view requires drawing. Draw from the current selection and catalogue without changing the data. -
draw_rect(get_viewport_rect(), PAPER)Cover the entire view with the chosen light background. -
draw_rect(Rect2(48, 126, 502, 360), SHELF)Place the main dark panel under the shelf. The numbers are x, y, width and height. -
draw_rect(Rect2(577, 126, 334, 360), Color("#f1ded4"))Place a separate panel behind the tools and item card. -
draw_rect(Rect2(48, 126, 8, 360), PINK)Draw a thin edge along the display board. -
print_on_shelf("TRAINING DISPLAY", Vector2(67, 153), 14, MINT)Draw the display's section label using a mint colour on the dark panel. -
print_on_shelf("DISPLAY WORKBENCH", Vector2(597, 155), 14, NAVY)Draw a dark section label on the light workbench panel. -
print_on_shelf("Quesbo Coding / Godot practice", Vector2(49, 520), 16, NAVY)Add a quiet footer below both panels. -
for row in range(ROWS):Run the body for each row from zero up to, but excluding, ROWS. -
for column in range(COLUMNS):For every row, repeat the inner drawing code for each column. -
var cell := Vector2i(column, row)Join the loop's column and row into one grid address. -
var point := cell_origin(cell)Convert this address to its top-left screen point. -
var slot := Rect2(point, TILE - Vector2(9, 9))Turn the point and a slightly smaller tile size into a rectangle with room for a visible gap. -
draw_rect(slot, Color("#554a69"))Paint the background of the current shelf slot. -
var index := cell_to_index(cell)Read a zero-based slot ID for this cell. -
print_on_shelf("%02d" % index, point + Vector2(9, 19), 13, CREAM)Format the slot ID with at least two digits and position it above the package space. -
if cell == selected:Compare whole addresses, so the outline appears on the one matching column and row. -
draw_rect(slot, GOLD, false, 3.0)Paint a gold outline around the selected slot. Leave the middle of the rectangle unchanged. -
func print_on_shelf(text: String, point: Vector2, font_size: int, ink: Color) -> void:Name the text, position, size and colour for a painted line of text. -
draw_string(ThemeDB.fallback_font, point, text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, ink)Use Godot's fallback font. point places the baseline of the text, which is where its letters sit. -
func cell_origin(cell: Vector2i) -> Vector2:Accept a slot address and return the top-left screen coordinate of that slot. -
return ORIGIN + Vector2(cell) * TILEMultiply the column by the tile width and the row by the tile height. Add the shelf margin to place the result. -
func _refresh() -> void:Define the single place that writes a shelf address onto the status bulletin. -
bulletin.text = "Selected column %d, row %d" % [selected.x, selected.y]Read selected.x for the column and selected.y for the row, then substitute each number into the status text. -
func cell_to_index(cell: Vector2i) -> int:Convert a two-part shelf address into one unique slot ID. -
return cell.y * COLUMNS + cell.xSkip the completed rows, four slots at a time, then add the column in this row. -
func _add_button(caption: String, point: Vector2, action: Callable) -> void:Build a reusable touch and keyboard control. The action argument points to the function that should run after a press. -
var button := Button.new()Create an unplaced Button object. -
button.text = captionSet the words the player reads on this button. -
button.position = pointUse the requested pixel coordinates for the upper-left corner of the button. -
button.custom_minimum_size = Vector2(90, 46)Give every workbench button a minimum width and height for easier viewing and clicking. -
button.pressed.connect(action)Listen to the pressed signal. Run the supplied callable when the activation happens. -
add_child(button)Put the configured Button on screen so it can handle clicks and keyboard focus. -
func _select(cell: Vector2i) -> void:Use one selection function whether the player taps the shelf or uses a button. -
selected = cellStore the requested address then let the label and highlight read it. -
_refresh()Synchronise the main bulletin with the current column and row. -
queue_redraw()Ask Godot to redraw the shelf so the highlight catches up with selected.
Find and fix a bug
If selected changes in the bulletin but the border stays put, check that queue_redraw still runs after _refresh.
Try a change
Move one button's binding to the centre-left space (0, 1) and rename its caption. Check which slot number lights up.