Tiny Shelf 32 · Build a product inspection card

Tiny Shelf Studio · Lesson 32 of 50 · Intermediate to advanced · About 35 min

Collectors need to see the product name and category before choosing a space. Bring one selected object into an enlarged, keyboard-friendly inspector.

Miniature packaging workshop with coloured paper models, tools and a teal cutting mat.
What you’ll make Open a readable catalogue card and explain what the product ID tells the game.

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

Read product key. Choose which record to open. → Look up a record. Handle an unknown ID. → Display four fields. Use the selected record. → Test focus. Tab and press Enter.
Read product key → Look up a record → Display four fields → Test focus
Godot checkpoint window for Build a product inspection card
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson Dictionary.get, bind, tooltip_text, Button.pressed, Label.text.

  1. Open the design bench

    Import this checkpoint's project.godot into Godot 4.7.2. Open main.gd from FileSystem. Press F5 for the starting view and F8 to stop it. Keep a copy to undo experiments.

    Why it works The checkpoint scene attaches main.gd to a Control. Each test starts from that known version, so compare one change with the original.

  2. Separate the ID from the displayed name

    Follow moon_milk in the lookup table. Rename the product to Evening Milk. Click the first button and compare the name and Product ID.

    const CATALOGUE := ["moon_milk", "comet_crisps", "pocket_paints"]

    Why it works The name can change for the artwork or a translation. Keep a stable ID so shelf rules still find the same product.

  3. Attach the right product to each button

    Read the call that creates a button. Temporarily change inspect.bind(id) to inspect.bind(CATALOGUE[0]) and click all the buttons. Restore it after checking.

    var control := place_button(str(PRODUCTS[id]["name"]), Vector2(58, 221 + 63 * i), inspect.bind(id), 250)

    Why it works bind keeps the ID for a later button press. Changing it to the first entry tells all the buttons to inspect the same product.

  4. Give the lookup a fallback

    Temporarily change the first inspect call in _ready to inspect("missing"). Run and confirm the card says Unknown product. Then restore the original line.

    var product: Dictionary = PRODUCTS.get(id, {})

    Why it works Dictionary.get supplies an empty Dictionary for an ID that is missing. Reading a fallback thereafter leaves the screen working.

  5. Fill the inspector from the same record

    Press Comet Crisps and compare its name, category and package with PRODUCTS. Change one displayed label prefix, then run it again.

    category_label.text = "Category · " + str(product.get("category", "–"))

    Why it works The string from the dictionary is joined to a helpful prefix, so the player knows what kind of detail they are reading.

  6. Test a keyboard selection

    Restart and look for a gold focus outline. Press Tab and then Enter. Check that the card changes to the focused button.

    first.grab_focus()

    Why it works grab_focus starts the keyboard on a real control. The button emits the same pressed signal with Enter and a click.

Watch the values change

  1. Open the bench picked is moon_milk.
    What you see. The card shows Drinks and Carton.
  2. Click Comet Crisps inspect receives comet_crisps.
    What you see. The card updates to Snacks and Tin.
  3. Call inspect with a missing ID The fallback Dictionary stays empty.
    What you see. The card says Unknown product and clears the product ID.

Predict before you run If a player clicks the product name after you rename it, which value is sent to inspect?

Reveal the answer

The button label changes but the bound id is still moon_milk. It is read from CATALOGUE.

Complete checkpoint

A catalogue with three large buttons and one inspector that changes for the current item.

main.gd

extends Control

const PRODUCTS := {
	"moon_milk": {"name": "Moon Milk", "category": "Drinks", "wrapper": "Carton"},
	"comet_crisps": {"name": "Comet Crisps", "category": "Snacks", "wrapper": "Tin"},
	"pocket_paints": {"name": "Pocket Paints", "category": "Crafts", "wrapper": "Box"},
}
const CATALOGUE := ["moon_milk", "comet_crisps", "pocket_paints"]
var picked := ""
var name_label: Label
var category_label: Label
var wrapper_label: Label
var id_label: Label
var first: Button

func _ready() -> void:
	title_page("CATALOGUE", "Build a product inspection card", "Choose a package to read its full name and category.")
	label("PRESS TO INSPECT", Vector2(59, 187), 15, GOLD)
	for i in range(CATALOGUE.size()):
		var id: String = CATALOGUE[i]
		var control := place_button(str(PRODUCTS[id]["name"]), Vector2(58, 221 + 63 * i), inspect.bind(id), 250)
		control.tooltip_text = str(PRODUCTS[id]["category"]) + " · See details"
		if i == 0:
			first = control
	name_label = label("", Vector2(376, 207), 31)
	category_label = label("", Vector2(378, 265), 21, SEA)
	wrapper_label = label("", Vector2(378, 308), 20)
	id_label = label("", Vector2(378, 356), 16, GOLD)
	inspect(CATALOGUE[0])
	first.grab_focus()

func inspect(id: String) -> void:
	var product: Dictionary = PRODUCTS.get(id, {})
	picked = id if not product.is_empty() else ""
	name_label.text = str(product.get("name", "Unknown product"))
	category_label.text = "Category · " + str(product.get("category", "–"))
	wrapper_label.text = "Package · " + str(product.get("wrapper", "–"))
	id_label.text = "Product ID · " + (picked if not picked.is_empty() else "–")

func _draw() -> void:
	backdrop()
	panel(Rect2(346, 169, 547, 254), Color("#244457"), GOLD)

const PAPER := Color("#fff4de")
const GOLD := Color("#f5bd79")
const SEA := Color("#9ddcd0")
const INK := Color("#172c3c")
const EDGE := Color("#54817f")

func title_page(unit: String, heading: String, subtitle: String) -> void:
	label("TINY SHELF STUDIO  /  " + unit, Vector2(44, 36), 14, GOLD)
	label(heading, Vector2(44, 65), 33)
	label(subtitle, Vector2(46, 119), 16, SEA)

func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> Label:
	var item := Label.new()
	item.text = words
	item.position = at
	item.add_theme_font_size_override("font_size", pixels)
	item.add_theme_color_override("font_color", ink)
	add_child(item)
	return item

func place_button(words: String, at: Vector2, action: Callable, width: float = 170.0) -> Button:
	var control := Button.new()
	control.text = words
	control.position = at
	control.custom_minimum_size = Vector2(width, 48)
	control.add_theme_font_size_override("font_size", 17)
	control.add_theme_color_override("font_color", PAPER)
	control.add_theme_stylebox_override("normal", style(Color("#244557"), EDGE))
	control.add_theme_stylebox_override("hover", style(Color("#365a62"), SEA))
	control.add_theme_stylebox_override("pressed", style(Color("#315b56"), SEA))
	control.add_theme_stylebox_override("focus", style(Color.TRANSPARENT, GOLD, 3))
	control.pressed.connect(action)
	add_child(control)
	return control

func style(fill: Color, edge: Color, line: int = 1) -> StyleBoxFlat:
	var block := StyleBoxFlat.new()
	block.bg_color = fill
	block.border_color = edge
	block.set_border_width_all(line)
	block.set_corner_radius_all(8)
	return block

func panel(rect: Rect2, fill: Color, edge: Color = EDGE) -> void:
	draw_style_box(style(fill, edge), rect)

func backdrop() -> void:
	draw_rect(Rect2(Vector2.ZERO, size), Color("#102333"))
	panel(Rect2(22, 19, size.x - 44, size.y - 38), Color("#183344"), Color("#325665"))

func paint_words(words: String, at: Vector2, pixels: int = 16, ink: Color = PAPER) -> void:
	draw_string(ThemeDB.fallback_font, at, words, HORIZONTAL_ALIGNMENT_LEFT, -1, pixels, ink)

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 ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules.
  2. const PRODUCTS := {Creates a constant named PRODUCTS. The expression makes a dictionary that looks up values by key. Godot infers the type from the first value.
  3. "moon_milk": {"name": "Moon Milk", "category": "Drinks", "wrapper": "Carton"},Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  4. "comet_crisps": {"name": "Comet Crisps", "category": "Snacks", "wrapper": "Tin"},Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  5. "pocket_paints": {"name": "Pocket Paints", "category": "Crafts", "wrapper": "Box"},Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
  6. }Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next.
  7. const CATALOGUE := ["moon_milk", "comet_crisps", "pocket_paints"]The stable IDs control the button order separately from the larger lookup table.
  8. var picked := ""Creates a variable named picked. The expression uses the text inside the quotes. Godot infers the type from the first value.
  9. var name_label: LabelDeclares name_label for later use. A colon followed by a type says which kind of value belongs here.
  10. var category_label: LabelDeclares category_label for later use. A colon followed by a type says which kind of value belongs here.
  11. var wrapper_label: LabelDeclares wrapper_label for later use. A colon followed by a type says which kind of value belongs here.
  12. var id_label: LabelDeclares id_label for later use. A colon followed by a type says which kind of value belongs here.
  13. var first: ButtonDeclares first for later use. A colon followed by a type says which kind of value belongs here.
  14. func _ready() -> void:Godot calls this once after the scene and its children are ready. Use it to set up the room and connect controls.
  15. title_page("CATALOGUE", "Build a product inspection card", "Choose a package to read its full name and category.")Calls title_page with the values in its parentheses.
  16. label("PRESS TO INSPECT", Vector2(59, 187), 15, GOLD)Calls label with the values in its parentheses.
  17. for i in range(CATALOGUE.size()):Repeats the indented instructions for each value in range(CATALOGUE.size()).
  18. var id: String = CATALOGUE[i]Creates a variable named id. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold.
  19. var control := place_button(str(PRODUCTS[id]["name"]), Vector2(58, 221 + 63 * i), inspect.bind(id), 250)Bind the current ID, so this button remembers exactly which product to pass to inspect later.
  20. control.tooltip_text = str(PRODUCTS[id]["category"]) + " · See details"Offer the category for pointer users. The larger inspection card repeats it as visible text.
  21. if i == 0:Keep the first button so it can receive focus when the scene opens.
  22. first = controlStores the new value in first. The expression reads control.
  23. name_label = label("", Vector2(376, 207), 31)Stores the new value in name_label. The expression calls label with the values in its parentheses.
  24. category_label = label("", Vector2(378, 265), 21, SEA)Stores the new value in category_label. The expression calls label with the values in its parentheses.
  25. wrapper_label = label("", Vector2(378, 308), 20)Stores the new value in wrapper_label. The expression calls label with the values in its parentheses.
  26. id_label = label("", Vector2(378, 356), 16, GOLD)Stores the new value in id_label. The expression calls label with the values in its parentheses.
  27. inspect(CATALOGUE[0])Make the card start on an existing product only after all of its labels have been built.
  28. first.grab_focus()Place keyboard focus on the first actual choice instead of making the player click before using keys.
  29. func inspect(id: String) -> void:Defines inspect, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  30. var product: Dictionary = PRODUCTS.get(id, {})Use an empty dictionary for an unrecognised key to keep the inspection card safe.
  31. picked = id if not product.is_empty() else ""Record the requested ID only after the lookup yields a product.
  32. name_label.text = str(product.get("name", "Unknown product"))Updates name_label.text, which controls the words a player sees. The expression turns a value into text for a player message.
  33. category_label.text = "Category · " + str(product.get("category", "–"))Updates category_label.text, which controls the words a player sees. The expression uses the text inside the quotes.
  34. wrapper_label.text = "Package · " + str(product.get("wrapper", "–"))If the wrapper field is unavailable, show a dash in the appropriate position.
  35. id_label.text = "Product ID · " + (picked if not picked.is_empty() else "–")Repeat the ID in the lab so the visible name and stable data key are easy to compare.
  36. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  37. backdrop()Calls backdrop with the values in its parentheses.
  38. panel(Rect2(346, 169, 547, 254), Color("#244457"), GOLD)Calls panel with the values in its parentheses.
  39. const PAPER := Color("#fff4de")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value.
  40. const GOLD := Color("#f5bd79")Creates a constant named GOLD. The expression builds a colour from these values. Godot infers the type from the first value.
  41. const SEA := Color("#9ddcd0")Creates a constant named SEA. The expression builds a colour from these values. Godot infers the type from the first value.
  42. const INK := Color("#172c3c")Creates a constant named INK. The expression builds a colour from these values. Godot infers the type from the first value.
  43. const EDGE := Color("#54817f")Creates a constant named EDGE. The expression builds a colour from these values. Godot infers the type from the first value.
  44. func title_page(unit: String, heading: String, subtitle: String) -> void:Defines title page, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  45. label("TINY SHELF STUDIO / " + unit, Vector2(44, 36), 14, GOLD)Make a small line above the main heading so you can tell which checkpoint is open.
  46. label(heading, Vector2(44, 65), 33)Calls label with the values in its parentheses.
  47. label(subtitle, Vector2(46, 119), 16, SEA)Calls label with the values in its parentheses.
  48. func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> Label:Defines label, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  49. var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value.
  50. item.text = wordsUpdates item.text, which controls the words a player sees. The expression reads words.
  51. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  52. item.add_theme_font_size_override("font_size", pixels)This Label has its own readable type size. Changing that value leaves the other labels alone.
  53. item.add_theme_color_override("font_color", ink)This label keeps the chosen text ink even when the default theme changes.
  54. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  55. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  56. func place_button(words: String, at: Vector2, action: Callable, width: float = 170.0) -> Button:Defines place button, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  57. var control := Button.new()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value.
  58. control.text = wordsUpdates control.text, which controls the words a player sees. The expression reads words.
  59. control.position = atUpdates control.position, which controls the node's local x and y location. The expression reads at.
  60. control.custom_minimum_size = Vector2(width, 48)Give the button room for its label and an easy target for taps. Containers and layouts can allocate more space.
  61. control.add_theme_font_size_override("font_size", 17)Calls control.add_theme_font_size_override with the values in its parentheses.
  62. control.add_theme_color_override("font_color", PAPER)Calls control.add_theme_color_override with the values in its parentheses.
  63. control.add_theme_stylebox_override("normal", style(Color("#244557"), EDGE))Calls control.add_theme_stylebox_override with the values in its parentheses.
  64. control.add_theme_stylebox_override("hover", style(Color("#365a62"), SEA))Calls control.add_theme_stylebox_override with the values in its parentheses.
  65. control.add_theme_stylebox_override("pressed", style(Color("#315b56"), SEA))Calls control.add_theme_stylebox_override with the values in its parentheses.
  66. control.add_theme_stylebox_override("focus", style(Color.TRANSPARENT, GOLD, 3))Draw a gold outline around keyboard focus. Other button styles can still indicate hover and press independently.
  67. control.pressed.connect(action)pressed fires on a successful control activation. Godot calls the function passed in without running it at setup time.
  68. add_child(control)Adds the new node to the scene tree so that Godot can run and draw it.
  69. return controlFinishes the function and hands its result back to the caller. The expression reads control.
  70. func style(fill: Color, edge: Color, line: int = 1) -> StyleBoxFlat:Defines style, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  71. var block := StyleBoxFlat.new()Creates a variable named block. The expression creates a new StyleBoxFlat object. Godot infers the type from the first value.
  72. block.bg_color = fillStores the new value in block.bg_color. The expression reads fill.
  73. block.border_color = edgeStores the new value in block.border_color. The expression reads edge.
  74. block.set_border_width_all(line)Apply one border line width to all four edges of the card.
  75. block.set_corner_radius_all(8)Round each corner of this reusable style by eight pixels.
  76. return blockFinishes the function and hands its result back to the caller. The expression reads block.
  77. func panel(rect: Rect2, fill: Color, edge: Color = EDGE) -> void:Defines panel, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  78. draw_style_box(style(fill, edge), rect)Calls draw_style_box with the values in its parentheses.
  79. func backdrop() -> void:Defines backdrop, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  80. draw_rect(Rect2(Vector2.ZERO, size), Color("#102333"))Paints a rectangle at the given position and size.
  81. panel(Rect2(22, 19, size.x - 44, size.y - 38), Color("#183344"), Color("#325665"))Subtract space on both sides of the canvas. As the window changes, the outer frame fits within it.
  82. func paint_words(words: String, at: Vector2, pixels: int = 16, ink: Color = PAPER) -> void:Defines paint words, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  83. draw_string(ThemeDB.fallback_font, at, words, HORIZONTAL_ALIGNMENT_LEFT, -1, pixels, ink)Godot draws the words from the baseline at the supplied pixel position using its fallback typeface.

Find and fix a bug

If labels stay empty, inspect when the first inspect call runs. Build the four Label nodes before accessing their text.

Try a change

Add a product with a new ID, name and category. Include its ID in CATALOGUE so the card can open through a button.