Tiny Shelf 04 · Find a shelf position with two numbers

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

The display has room for twelve tiny packages. Start by working out where to place the frame of a single slot.

Miniature packaging workshop with coloured paper models, tools and a teal cutting mat.
What you’ll make Use a Vector2i to locate one slot and turn its column and row into a screen position.

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

Name the grid address. Use (column, row). → Scale it. Multiply by the size of one tile. → Add the margin. Find the top-left corner.
Name the grid address → Scale it → Add the margin
Godot checkpoint window for Find a shelf position with two numbers
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson Vector2i, x and y, Vector2, constants, return, Rect2.

  1. Choose an address

    In the new checkpoint, add selected above _ready. Give it the whole-number pair (2, 1). Use the supplied shelf background so the numbers are easy to see.

    var selected := Vector2i(2, 1)

    Why it works Vector2i holds two ints. Here x is column and y is row. The top left starts at zero.

  2. Give the shelf a size and starting point

    Add the constants above selected. Keep _ready and the drawing functions as separate blocks.

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

    Why it works ORIGIN is the first slot's screen position. TILE contains a width and height. A constant stays the same during a run.

  3. Locate the corner in pixels

    Write cell_origin below the drawing code. Return the origin plus the coordinate multiplied by the tile size.

    func cell_origin(cell: Vector2i) -> Vector2:
    	return ORIGIN + Vector2(cell) * TILE

    Why it works The conversion Vector2(cell) lets Godot use a grid address in pixel arithmetic. Multiply x by 116 and y by 98, then add the margin.

  4. Draw one highlighted rectangle

    At the end of the supplied _draw method, add the point and the two draw_rect calls. Keep the slot a little smaller than TILE.

    	var point := cell_origin(selected)
    	draw_rect(Rect2(point, TILE - Vector2(9, 9)), Color("#625575"))
    	draw_rect(Rect2(point, TILE - Vector2(9, 9)), GOLD, false, 3.0)

    Why it works A Rect2 holds position and size. Reducing the draw size adds a gap without moving the next grid address.

  5. Show the address as text

    Write _refresh and call it inside _ready. Run and check the bulletin against the highlighted slot.

    func _refresh() -> void:
    	bulletin.text = "Selected column %d, row %d" % [selected.x, selected.y]

    Why it works selected.x and selected.y read the two parts of the address. Formatting them makes the invisible coordinates traceable.

  6. Move the initial address

    Try (0, 0) and (3, 2) in two runs. Put (2, 1) back before moving on.

    Why it works Only data changes. cell_origin and the drawing rule work for every allowed address.

Watch the values change

  1. Start at (2, 1). The offset in pixels is (232, 98).
    What you see. The slot appears in column two of row one.
  2. Add the shelf origin. (68, 171) plus (232, 98) gives (300, 269).
    What you see. The rectangle starts at that screen coordinate.
  3. Try (0, 0). Both offsets are zero.
    What you see. The frame sits at the top left.

Predict before you run What is the top-left screen corner of slot (3, 0)?

Reveal the answer

Its x coordinate is 68 + 3 × 116 = 416. Its y coordinate stays 171.

Complete checkpoint

One selected space appears in the third column and second row. The bulletin reads Selected column 2, row 1.

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("Follow the shelf map", Vector2(598, 181), 23, NAVY)
	make_label("Columns go right. Rows go down.", 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)
	var point := cell_origin(selected)
	draw_rect(Rect2(point, TILE - Vector2(9, 9)), Color("#625575"))
	draw_rect(Rect2(point, TILE - Vector2(9, 9)), GOLD, false, 3.0)
	print_on_shelf("SELECTED", point + Vector2(12, 48), 15, CREAM)


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]

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("Follow the shelf map", Vector2(598, 181), 23, NAVY)Give the workbench one explanatory caption at the requested pixel position.
  20. make_label("Columns go right. Rows go down.", 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. var point := cell_origin(selected)Find the top-left pixel of the selected slot.
  38. draw_rect(Rect2(point, TILE - Vector2(9, 9)), Color("#625575"))Paint the slot body nine pixels smaller than its column and row spacing to leave a gap.
  39. draw_rect(Rect2(point, TILE - Vector2(9, 9)), GOLD, false, 3.0)Draw a three-pixel gold edge. The false argument tells Godot to leave the centre unfilled.
  40. print_on_shelf("SELECTED", point + Vector2(12, 48), 15, CREAM)Put a word inside the frame so you can see which drawing follows selected.
  41. 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.
  42. 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.
  43. func cell_origin(cell: Vector2i) -> Vector2:Accept a slot address and return the top-left screen coordinate of that slot.
  44. 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.
  45. func _refresh() -> void:Define the single place that writes a shelf address onto the status bulletin.
  46. 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.

Find and fix a bug

If the frame moves vertically when you change the first number, check for swapped x and y in the starting address or the multiplication.

Try a change

Predict the corner of (1, 2). Change selected and check whether the highlight appears where you predicted.