Tiny Shelf 05 · Draw the whole 12-slot shelf

Tiny Shelf Studio · Lesson 05 of 50 · Beginner · About 20 min

Drawing twelve boxes by hand would be slow. Set the size once and let the computer draw every possible row and column.

Miniature packaging workshop with coloured paper models, tools and a teal cutting mat.
What you’ll make Use nested loops to draw every slot and assign it a unique zero-based number.

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

Loop rows. Visit rows 0 through 2. → Loop columns. Visit columns 0 through 3. → Draw a slot. Use its address and ID.
Loop rows → Loop columns → Draw a slot
Godot checkpoint window for Draw the whole 12-slot shelf
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson for, range, nested loops, row-major indexing, if, %02d.

  1. Look at the grid setup

    Keep 4 columns and 3 rows. Find the old single-slot block after the background drawing. You will replace the single-slot block only.

    const COLUMNS := 4
    const ROWS := 3
    const ORIGIN := Vector2(68, 171)
    const TILE := Vector2(116, 98)

    Why it works The background and coordinate helper already work. The new loop will call that helper once for each address.

  2. Loop through the rows and columns

    Create two nested for loops. Indent the column line once more than the row line. Inside both loops, create cell from column and row.

    	for row in range(ROWS):
    		for column in range(COLUMNS):
    			var cell := Vector2i(column, row)

    Why it works range(3) gives 0, 1, 2. Each row runs all four column values in range(4).

  3. Draw a tile at each address

    Inside the inner loop, calculate the point and slot rectangle, then draw it.

    			var point := cell_origin(cell)
    			var slot := Rect2(point, TILE - Vector2(9, 9))
    			draw_rect(slot, Color("#554a69"))

    Why it works Every pass has a different cell. The same drawing instructions use that pass's coordinates.

  4. Count slots across each row

    Add cell_to_index outside _draw. In the inner loop, call it and draw the number on the slot.

    func cell_to_index(cell: Vector2i) -> int:
    	return cell.y * COLUMNS + cell.x

    Why it works Each completed row takes up four IDs. Multiply row by COLUMNS, then add the column. %02d adds a leading zero to a one-digit number.

  5. Keep one selection border

    Place the if at the end of the inner loop. Indent the border draw one more level than the if.

    			if cell == selected:
    				draw_rect(slot, GOLD, false, 3.0)

    Why it works Only the cell that matches selected gets its gold border. All twelve still get a background.

  6. Read the slot numbers

    Run with selected at (2, 1). Trace the first space of each row to check the ID formula.

    Why it works If your labels start 00, 04 and 08 on successive rows, each new row is stepping by the shelf width.

Watch the values change

  1. Draw row 0. Its four columns have numbers 00 to 03.
    What you see. The top row is labelled left to right.
  2. Draw address (2, 1). 1 × 4 + 2 gives 6.
    What you see. Slot 06 has a gold border.
  3. Finish row 2. (3, 2) gives 2 × 4 + 3 = 11.
    What you see. The last shelf space says 11.

Predict before you run Which grid address produces ID 9?

Reveal the answer

Row 2 starts at 8. One column to the right gives (1, 2), whose ID is 9.

Complete checkpoint

Twelve labelled cells show IDs 00–11. Slot 06 has a gold border.

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(2, 1)
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("12 places to display", Vector2(598, 181), 23, NAVY)
	make_label("Slot IDs run from 00 to 11.", Vector2(599, 226), 15, NAVY)


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

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 ControlBase this script on a Godot Control. It can assemble text and buttons, receive input and draw directly into the scene.
  2. const NAVY := Color("#18243d")Give headings and package shadows a shared dark ink. The six-digit colour is read from a hexadecimal string.
  3. const PAPER := Color("#fff5e7")Use this warm, light colour behind the shelf and on front labels.
  4. const SHELF := Color("#302e4b")Set the dark display board colour. A constant lets later drawings refer to it by name.
  5. const CREAM := Color("#fff2d3")Use a light ink for numbers and letters placed against the dark display.
  6. const PINK := Color("#f58a9d")Add a warm edge to the display. This colour does not decide whether a slot is occupied.
  7. const MINT := Color("#b4edce")Use a light green marker in the workbench styling.
  8. const GOLD := Color("#ffd575")Reserve a high-contrast yellow for the frame around the selected slot.
  9. 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.
  10. const ROWS := 3Limit the shelf to three rows. The valid row addresses are 0, 1 and 2.
  11. const ORIGIN := Vector2(68, 171)Place slot (0, 0) at x 68 and y 171 in the game window.
  12. 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.
  13. var selected := Vector2i(2, 1)Start with column 2 and row 1 in this lesson. Both counts start at zero.
  14. var bulletin: LabelKeep a reference to the text node so other functions can edit its words.
  15. func _ready() -> void:Godot calls this when the Control enters the ready scene. Set up the initial title, labels and buttons here.
  16. 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.
  17. bulletin = make_label("", Vector2(51, 81), 21, NAVY)Create a second label and keep it in bulletin. You can set its text after placement.
  18. _refresh()Synchronise the main bulletin with the current column and row.
  19. make_label("12 places to display", Vector2(598, 181), 23, NAVY)Give the workbench one explanatory caption at the requested pixel position.
  20. make_label("Slot IDs run from 00 to 11.", Vector2(599, 226), 15, NAVY)Give the workbench one explanatory caption at the requested pixel position.
  21. 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.
  22. var label := Label.new()Create a Label object. It needs to be added to the running scene before it can appear.
  23. label.text = textPut the helper's text argument into the new Label.
  24. label.position = pointPosition the label using the x and y pixel coordinates passed into the helper.
  25. label.modulate = inkTint the label with the colour passed into this call.
  26. label.add_theme_font_size_override("font_size", font_size)Override the theme's size for just this label.
  27. add_child(label)Make Godot manage and draw the new Label as a child of the Control.
  28. return labelGive the caller the Label for later changes.
  29. func _draw() -> void:Godot calls this when the view requires drawing. Draw from the current selection and catalogue without changing the data.
  30. draw_rect(get_viewport_rect(), PAPER)Cover the entire view with the chosen light background.
  31. draw_rect(Rect2(48, 126, 502, 360), SHELF)Place the main dark panel under the shelf. The numbers are x, y, width and height.
  32. draw_rect(Rect2(577, 126, 334, 360), Color("#f1ded4"))Place a separate panel behind the tools and item card.
  33. draw_rect(Rect2(48, 126, 8, 360), PINK)Draw a thin edge along the display board.
  34. print_on_shelf("TRAINING DISPLAY", Vector2(67, 153), 14, MINT)Draw the display's section label using a mint colour on the dark panel.
  35. print_on_shelf("DISPLAY WORKBENCH", Vector2(597, 155), 14, NAVY)Draw a dark section label on the light workbench panel.
  36. print_on_shelf("Quesbo Coding / Godot practice", Vector2(49, 520), 16, NAVY)Add a quiet footer below both panels.
  37. for row in range(ROWS):Run the body for each row from zero up to, but excluding, ROWS.
  38. for column in range(COLUMNS):For every row, repeat the inner drawing code for each column.
  39. var cell := Vector2i(column, row)Join the loop's column and row into one grid address.
  40. var point := cell_origin(cell)Convert this address to its top-left screen point.
  41. 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.
  42. draw_rect(slot, Color("#554a69"))Paint the background of the current shelf slot.
  43. var index := cell_to_index(cell)Read a zero-based slot ID for this cell.
  44. 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.
  45. if cell == selected:Compare whole addresses, so the outline appears on the one matching column and row.
  46. draw_rect(slot, GOLD, false, 3.0)Paint a gold outline around the selected slot. Leave the middle of the rectangle unchanged.
  47. 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.
  48. 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.
  49. func cell_origin(cell: Vector2i) -> Vector2:Accept a slot address and return the top-left screen coordinate of that slot.
  50. 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.
  51. func _refresh() -> void:Define the single place that writes a shelf address onto the status bulletin.
  52. 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.
  53. func cell_to_index(cell: Vector2i) -> int:Convert a two-part shelf address into one unique slot ID.
  54. return cell.y * COLUMNS + cell.xSkip the completed rows, four slots at a time, then add the column in this row.

Find and fix a bug

If the shelf only has four slots, check that the column loop starts inside the row loop and the draw commands start inside both.

Try a change

Temporarily add a fifth column and predict the new last ID. Parts of the extra column may pass the training panel. Use the numbers to check the rule, then restore four columns.