Tiny Shelf Studio · Lesson 10 of 50 · Beginner · About 30 min
The shop display is ready for visitors. A small product card should tell them what they're looking at, even if they can't see the colour clearly.

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/09 when the lesson builds on prior work, or copy the checkpoints/10 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 Dictionary.get, default values, String.is_empty, records, multiline text, UI state.
-
Add full catalogue records
Replace PACK_STYLE with PRODUCTS, leaving the PREVIEW mapping as it was. Keep each product ID unchanged and add its name, family and note.
const PRODUCTS := { "lemon_fizz": { "name": "Lemon Fizz", "family": "Drinks", "ink": Color("#ffd575"), "mark": "LEMON", "note": "A cheerful yellow soda carton." }, "honey_hoops": { "name": "Honey Hoops", "family": "Snacks", "ink": Color("#f58a9d"), "mark": "HONEY", "note": "Crunchy hoops in a pink carton." }, "mint_paste": { "name": "Mint Paste", "family": "Care", "ink": Color("#b4edce"), "mark": "MINT", "note": "A green toothpaste carton." }, }Why it works An ID is a stable lookup key. The human-readable name and product note can change without moving the product or renaming the ID.
-
Reuse each record when drawing
In draw_pack, get the product from PRODUCTS using item_id. Keep its ink and mark fields the same as the old drawing code.
var product: Dictionary = PRODUCTS[item_id]Why it works One catalogue now feeds the carton and its information card. The front mark stays short while the inspector can show the full name.
-
Add the inspector control
Declare detail as a Label and create it in _ready. Add Inspect item and connect the button to _inspect, then call _inspect once.
detail = make_label("", Vector2(598, 361), 16, NAVY) _add_button("Inspect item", Vector2(598, 414), _inspect) _inspect()Why it works The first selection is already slot 00, so the opening card can show Lemon Fizz. Future presses update it with the current selection.
-
Read the active slot safely
Begin _inspect by turning selected into an index. Use PREVIEW.get with an empty string as the fallback.
func _inspect() -> void: var index := cell_to_index(selected) var item_id: String = PREVIEW.get(index, "")Why it works Dictionary.get avoids looking up a missing key as though it existed. The fallback is a useful marker for an unoccupied slot.
-
Handle empty and filled slots
Add the is_empty check and early return. If an ID exists, read its record and render the name, family and note.
if item_id.is_empty(): detail.text = "Slot %02d is empty." % index return var product: Dictionary = PRODUCTS[item_id] detail.text = "%s\n%s · %s" % [product["name"], product["family"], product["note"]]Why it works The blank path tells players the slot is empty. For a valid ID, the backslash-n creates a line break between the name and the rest of the product card.
-
Test three different selections
Inspect slots 00, 01 and 07 in that order. After selecting a new shelf space, click Inspect item again to refresh the product card.
Why it works Changing selected and loading its product card are separate actions here. A later lesson can choose when the display should refresh.
Watch the values change
-
Run and inspect slot 00. The slot maps to "lemon_fizz".
What you see. The card reads Lemon Fizz and Drinks. -
Select slot 01 and inspect. The slot maps to "honey_hoops".
What you see. The card reads Honey Hoops and Snacks. -
Select slot 07 and inspect. The fallback item ID is an empty string.
What you see. Slot 07 is empty appears in the workbench.
Predict before you run If you change Honey Hoops' displayed name but leave its ID unchanged, what should change?
Reveal the answer
The inspector shows your new displayed name. The carton stays in slot 01 and keeps its front mark, because its ID and mark haven’t changed.
Complete checkpoint
The first three sample products have safe, readable cards. Selecting a different slot and inspecting it shows that slot's record or an empty message.
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
const PREVIEW := {0: "lemon_fizz", 1: "honey_hoops", 5: "mint_paste"}
const PRODUCTS := {
"lemon_fizz": {
"name": "Lemon Fizz", "family": "Drinks", "ink": Color("#ffd575"),
"mark": "LEMON", "note": "A cheerful yellow soda carton."
},
"honey_hoops": {
"name": "Honey Hoops", "family": "Snacks", "ink": Color("#f58a9d"),
"mark": "HONEY", "note": "Crunchy hoops in a pink carton."
},
"mint_paste": {
"name": "Mint Paste", "family": "Care", "ink": Color("#b4edce"),
"mark": "MINT", "note": "A green toothpaste carton."
},
}
var detail: Label
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 product to read", Vector2(598, 181), 23, NAVY)
_add_button("Up", Vector2(704, 232), _shift.bind(Vector2i.UP))
_add_button("Left", Vector2(598, 285), _shift.bind(Vector2i.LEFT))
_add_button("Down", Vector2(704, 285), _shift.bind(Vector2i.DOWN))
_add_button("Right", Vector2(810, 285), _shift.bind(Vector2i.RIGHT))
detail = make_label("", Vector2(598, 361), 16, NAVY)
_add_button("Inspect item", Vector2(598, 414), _inspect)
_inspect()
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)
if PREVIEW.has(index):
draw_pack(point + Vector2(31, 27), PREVIEW[index])
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:
if not Rect2i(0, 0, COLUMNS, ROWS).has_point(cell):
bulletin.text = "Shelf edge · Selection stays put."
return
selected = cell
_refresh()
queue_redraw()
func _shift(direction: Vector2i) -> void:
_select(selected + direction)
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
var shelf_rect := Rect2(ORIGIN, Vector2(COLUMNS, ROWS) * TILE)
if shelf_rect.has_point(event.position):
var offset: Vector2 = (event.position - ORIGIN) / TILE
_select(Vector2i(floori(offset.x), floori(offset.y)))
get_viewport().set_input_as_handled()
return
if not event is InputEventKey or not event.pressed or event.echo:
return
match event.keycode:
KEY_UP:
_shift(Vector2i.UP)
KEY_LEFT:
_shift(Vector2i.LEFT)
KEY_DOWN:
_shift(Vector2i.DOWN)
KEY_RIGHT:
_shift(Vector2i.RIGHT)
_:
return
get_viewport().set_input_as_handled()
func draw_pack(point: Vector2, item_id: String) -> void:
var product: Dictionary = PRODUCTS[item_id]
var ink: Color = product["ink"]
draw_rect(Rect2(point + Vector2(4, 4), Vector2(49, 53)), NAVY)
draw_rect(Rect2(point, Vector2(45, 53)), ink)
draw_rect(Rect2(point + Vector2(0, 2), Vector2(45, 5)), ink.lightened(0.35))
draw_rect(Rect2(point + Vector2(4, 15), Vector2(37, 20)), PAPER)
print_on_shelf(product["mark"], point + Vector2(6, 29), 10, NAVY)
func _inspect() -> void:
var index := cell_to_index(selected)
var item_id: String = PREVIEW.get(index, "")
if item_id.is_empty():
detail.text = "Slot %02d is empty." % index
return
var product: Dictionary = PRODUCTS[item_id]
detail.text = "%s\n%s · %s" % [product["name"], product["family"], product["note"]]
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). -
const PREVIEW := {0: "lemon_fizz", 1: "honey_hoops", 5: "mint_paste"}Record which slot contains each displayed product. The key is the slot ID. The value is a small product ID. -
const PRODUCTS := {Keep the whole product catalogue together. Each product key has a record with both art values and text for the inspector. -
"lemon_fizz": {Open the catalogue record for the yellow Lemon Fizz drink carton. -
"name": "Lemon Fizz", "family": "Drinks", "ink": Color("#ffd575"),Supply a readable name and family for the inspector and an ink colour for the drawing helper. -
"mark": "LEMON", "note": "A cheerful yellow soda carton."Set a short mark for the front of the carton and a longer description for the inspector. -
},Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
"honey_hoops": {Open the catalogue record for the pink Honey Hoops snack box. -
"name": "Honey Hoops", "family": "Snacks", "ink": Color("#f58a9d"),Supply a readable name and family for the inspector and an ink colour for the drawing helper. -
"mark": "HONEY", "note": "Crunchy hoops in a pink carton."Set a short mark for the front of the carton and a longer description for the inspector. -
},Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
"mint_paste": {Open the catalogue record for green Mint Paste in the care family. -
"name": "Mint Paste", "family": "Care", "ink": Color("#b4edce"),Supply a readable name and family for the inspector and an ink colour for the drawing helper. -
"mark": "MINT", "note": "A green toothpaste carton."Set a short mark for the front of the carton and a longer description for the inspector. -
},Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
}Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
var detail: LabelKeep the catalogue card available to the inspector function. -
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 product to read", Vector2(598, 181), 23, NAVY)Give the workbench one explanatory caption at the requested pixel position. -
_add_button("Up", Vector2(704, 232), _shift.bind(Vector2i.UP))Store the upward step to pass to _shift when the button fires. -
_add_button("Left", Vector2(598, 285), _shift.bind(Vector2i.LEFT))Store the left step to pass to _shift when the button fires. -
_add_button("Down", Vector2(704, 285), _shift.bind(Vector2i.DOWN))Store the downward step to pass to _shift when the button fires. -
_add_button("Right", Vector2(810, 285), _shift.bind(Vector2i.RIGHT))Store the right step to pass to _shift when the button fires. -
detail = make_label("", Vector2(598, 361), 16, NAVY)Create the empty information card below the movement controls. -
_add_button("Inspect item", Vector2(598, 414), _inspect)Run _inspect when the product-inspection button is activated. -
_inspect()Read the active slot immediately so the opening inspection area has something to show. -
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. -
if PREVIEW.has(index):Only draw a package if the current slot ID is a key in PREVIEW. -
draw_pack(point + Vector2(31, 27), PREVIEW[index])Place the miniature 31 pixels right and 27 pixels down from the slot's corner. -
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. -
if not Rect2i(0, 0, COLUMNS, ROWS).has_point(cell):Test the candidate against the four-by-three rectangle. not flips the result, so this block runs for an out-of-range address. -
bulletin.text = "Shelf edge · Selection stays put."Explain the blocked action while keeping the previous selection. -
returnLeave the function before touching selected or moving the highlight. -
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. -
func _shift(direction: Vector2i) -> void:Receive a direction from a button or key and turn it into a candidate address. -
_select(selected + direction)Combine the old selection and one step. The selection function will accept or reject that candidate. -
func _input(event: InputEvent) -> void:Let Godot send events to this Control so it can read taps or keys. -
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:Check the event is a mouse-style left press. Touch can generate this event when touch-to-mouse emulation is enabled. -
var shelf_rect := Rect2(ORIGIN, Vector2(COLUMNS, ROWS) * TILE)Build the full click region from ORIGIN and the total size of all four columns and three rows. -
if shelf_rect.has_point(event.position):Only convert clicks inside the shelf. A workbench button can still handle its own press. -
var offset: Vector2 = (event.position - ORIGIN) / TILESubtract the shelf's screen margin and divide by one tile size. The remaining decimals describe the place inside a shelf tile. -
_select(Vector2i(floori(offset.x), floori(offset.y)))Round both tile coordinates down and pass the resulting integer pair to the shared selection rule. -
get_viewport().set_input_as_handled()Tell Godot that the current event has been used by the shelf. -
returnLeave this input path. No selection update is needed after the branch has handled or declined the event. -
if not event is InputEventKey or not event.pressed or event.echo:Skip events that are not fresh keyboard presses. A key release or an echo should not change the address. -
returnLeave this input path. No selection update is needed after the branch has handled or declined the event. -
match event.keycode:Run the branch for the selected arrow key. An unrelated key reaches the underscore fallback. -
KEY_UP:Recognise the up arrow. -
_shift(Vector2i.UP)Propose a move one row upward. -
KEY_LEFT:Recognise the left arrow. -
_shift(Vector2i.LEFT)Propose a move one column to the left. -
KEY_DOWN:Recognise the down arrow. -
_shift(Vector2i.DOWN)Propose a move one row downward. -
KEY_RIGHT:Recognise the right arrow. -
_shift(Vector2i.RIGHT)Propose a move one column to the right. -
_:Handles every match value that the branches above did not handle. -
returnLeave this input path. No selection update is needed after the branch has handled or declined the event. -
get_viewport().set_input_as_handled()Tell Godot that the current event has been used by the shelf. -
func draw_pack(point: Vector2, item_id: String) -> void:Paint a small product given its screen point and catalogue ID. -
var product: Dictionary = PRODUCTS[item_id]Read the same catalogue record used by the inspector. It supplies colours, lettering and description. -
var ink: Color = product["ink"]Take the front colour from the product record. -
draw_rect(Rect2(point + Vector2(4, 4), Vector2(49, 53)), NAVY)Place the shadow first, a few pixels to the right and below the carton. -
draw_rect(Rect2(point, Vector2(45, 53)), ink)Draw the carton front over part of its shadow. -
draw_rect(Rect2(point + Vector2(0, 2), Vector2(45, 5)), ink.lightened(0.35))Draw a narrow pale strip at the top to show the fold of the package. -
draw_rect(Rect2(point + Vector2(4, 15), Vector2(37, 20)), PAPER)Lay a light label on the carton so dark text can be read. -
print_on_shelf(product["mark"], point + Vector2(6, 29), 10, NAVY)Paint the product's short front mark inside that label. -
func _inspect() -> void:Read the selected slot and show a catalogue description or an empty message. -
var index := cell_to_index(selected)Read a zero-based slot ID for this cell. -
var item_id: String = PREVIEW.get(index, "")Read that slot's item ID. Use an empty string when the slot is missing from PREVIEW. -
if item_id.is_empty():Choose the empty-slot path when no product ID is present. -
detail.text = "Slot %02d is empty." % indexShow the slot number and explain that it is empty. -
returnLeave now so an empty ID is never used to look up a catalogue record. -
var product: Dictionary = PRODUCTS[item_id]Read the same catalogue record used by the inspector. It supplies colours, lettering and description. -
detail.text = "%s\n%s · %s" % [product["name"], product["family"], product["note"]]Put the product name on the first line and the family and note on the second.
Find and fix a bug
If inspection fails only when you select an empty slot, check that the branch returns before reading PRODUCTS. An empty string has no product record.
Try a change
Write a new note under Mint Paste and inspect slot 05. Keep the note short enough to fit the workbench.