Tiny Shelf 02 · Count the miniatures waiting for labels

Tiny Shelf Studio · Lesson 02 of 50 · Beginner · About 15 min

Your first display has three sample products. Each one needs a little name card, so your programme must keep count.

Miniature packaging workshop with coloured paper models, tools and a teal cutting mat.
What you’ll make Store a whole number and update it when a practice miniature receives its label.

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

Start with 3. Store the number as an int. → Subtract 1. Update the stored value. → Show 2. Format the new number.
Start with 3 → Subtract 1 → Show 2
Godot checkpoint window for Count the miniatures waiting for labels
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson var, int, assignment, -=, formatting %d, expressions.

  1. Add the starting count

    At the top of main.gd, above _ready, add waiting and give it the value 3.

    var waiting: int = 3

    Why it works int is a whole-number type. It matches a count of miniatures.

  2. Place a number inside text

    Replace the opening bulletin assignment with a label that uses %d and waiting.

    	bulletin.text = "Miniatures waiting: %d" % waiting

    Why it works %d is a placeholder for a whole number. The percent operator outside the quote fills it with the current count.

  3. Count one label made

    Put the subtraction line directly below the first bulletin assignment.

    	waiting -= 1

    Why it works waiting -= 1 means read waiting, subtract one, and store the answer back in waiting.

  4. Print the updated value

    Add the print call after the subtraction. Press F5 and compare it with the first bulletin.

    	print("After one practice label: ", waiting)

    Why it works print accepts several arguments separated by commas. They're shown in sequence. The older bulletin keeps the text assigned earlier.

  5. Show both moments

    Add the two workbench labels below print. Stop and rerun to refresh the starting count.

    	make_label("One label made", Vector2(598, 181), 26, NAVY)
    	make_label("%d still need a label." % waiting, Vector2(599, 226), 16, NAVY)

    Why it works The workbench text is built after the subtraction, so it reads waiting as 2. A new run creates a new script instance with waiting starting at 3.

  6. Check a different count

    Change the starting number from 3 to 5 and run again. Read the bulletin and workbench, then put 3 back for the next checkpoint.

    Why it works Only the starting number changed. The same subtraction and formatting should now give 5 before and 4 after.

Watch the values change

  1. Load the scene. waiting starts at 3.
    What you see. The script formats the first bulletin using 3.
  2. Make one practice label. waiting changes from 3 to 2.
    What you see. Output reports 2.
  3. Read the workbench. Its string is created after the change.
    What you see. Two miniatures still need a label.

Predict before you run The bulletin is created before subtracting. Does it update by itself after waiting changes?

Reveal the answer

No. The label holds the finished text. You can set it again with the new number. In this checkpoint you see the new number in the workbench.

Complete checkpoint

The opening bulletin reports 3. The workbench and Output report that 2 are left.

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")
var waiting: int = 3
var bulletin: Label


func _ready() -> void:
	make_label("TINY SHELF STUDIO", Vector2(49, 30), 35, NAVY)
	bulletin = make_label("", Vector2(51, 81), 21, NAVY)
	bulletin.text = "Miniatures waiting: %d" % waiting
	waiting -= 1
	print("After one practice label: ", waiting)
	make_label("One label made", Vector2(598, 181), 26, NAVY)
	make_label("%d still need a label." % waiting, Vector2(599, 226), 16, 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)
	draw_rect(Rect2(78, 398, 443, 10), Color("#b98278"))
	draw_rect(Rect2(93, 409, 423, 9), NAVY)
	draw_rect(Rect2(140, 263, 74, 135), GOLD)
	draw_rect(Rect2(148, 290, 59, 56), CREAM)
	draw_circle(Vector2(178, 318), 17.0, Color("#e59646"))
	draw_rect(Rect2(257, 220, 102, 178), PINK)
	draw_rect(Rect2(267, 265, 82, 62), CREAM)
	draw_circle(Vector2(309, 297), 20.0, Color("#8176a9"))
	draw_rect(Rect2(405, 294, 72, 104), MINT)
	draw_rect(Rect2(412, 314, 58, 44), CREAM)
	draw_circle(Vector2(440, 335), 15.0, Color("#46876d"))


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)

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. var waiting: int = 3Hold the number of sample miniatures still waiting for labels as a whole number.
  10. var bulletin: LabelKeep a reference to the text node so other functions can edit its words.
  11. func _ready() -> void:Godot calls this when the Control enters the ready scene. Set up the initial title, labels and buttons here.
  12. 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.
  13. bulletin = make_label("", Vector2(51, 81), 21, NAVY)Create a second label and keep it in bulletin. You can set its text after placement.
  14. bulletin.text = "Miniatures waiting: %d" % waitingReplace %d with the value of waiting. This string is made before the practice count changes.
  15. waiting -= 1Read waiting, subtract one and put the new value back under the same name.
  16. print("After one practice label: ", waiting)Send the new count after the subtraction to Godot's Output panel.
  17. make_label("One label made", Vector2(598, 181), 26, NAVY)Give the workbench one explanatory caption at the requested pixel position.
  18. make_label("%d still need a label." % waiting, Vector2(599, 226), 16, NAVY)Show the current value after the subtraction in a separate workbench label.
  19. 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.
  20. var label := Label.new()Create a Label object. It needs to be added to the running scene before it can appear.
  21. label.text = textPut the helper's text argument into the new Label.
  22. label.position = pointPosition the label using the x and y pixel coordinates passed into the helper.
  23. label.modulate = inkTint the label with the colour passed into this call.
  24. label.add_theme_font_size_override("font_size", font_size)Override the theme's size for just this label.
  25. add_child(label)Make Godot manage and draw the new Label as a child of the Control.
  26. return labelGive the caller the Label for later changes.
  27. func _draw() -> void:Godot calls this when the view requires drawing. Draw from the current selection and catalogue without changing the data.
  28. draw_rect(get_viewport_rect(), PAPER)Cover the entire view with the chosen light background.
  29. draw_rect(Rect2(48, 126, 502, 360), SHELF)Place the main dark panel under the shelf. The numbers are x, y, width and height.
  30. draw_rect(Rect2(577, 126, 334, 360), Color("#f1ded4"))Place a separate panel behind the tools and item card.
  31. draw_rect(Rect2(48, 126, 8, 360), PINK)Draw a thin edge along the display board.
  32. print_on_shelf("TRAINING DISPLAY", Vector2(67, 153), 14, MINT)Draw the display's section label using a mint colour on the dark panel.
  33. print_on_shelf("DISPLAY WORKBENCH", Vector2(597, 155), 14, NAVY)Draw a dark section label on the light workbench panel.
  34. print_on_shelf("Quesbo Coding / Godot practice", Vector2(49, 520), 16, NAVY)Add a quiet footer below both panels.
  35. draw_rect(Rect2(78, 398, 443, 10), Color("#b98278"))Draw one visible part of the display using its x, y, width and height, then fill it with the supplied colour.
  36. draw_rect(Rect2(93, 409, 423, 9), NAVY)Draw one visible part of the display using its x, y, width and height, then fill it with the supplied colour.
  37. draw_rect(Rect2(140, 263, 74, 135), GOLD)Draw one visible part of the display using its x, y, width and height, then fill it with the supplied colour.
  38. draw_rect(Rect2(148, 290, 59, 56), CREAM)Draw one visible part of the display using its x, y, width and height, then fill it with the supplied colour.
  39. draw_circle(Vector2(178, 318), 17.0, Color("#e59646"))Place a round motif on the small product label using a centre point, radius and colour.
  40. draw_rect(Rect2(257, 220, 102, 178), PINK)Draw one visible part of the display using its x, y, width and height, then fill it with the supplied colour.
  41. draw_rect(Rect2(267, 265, 82, 62), CREAM)Draw one visible part of the display using its x, y, width and height, then fill it with the supplied colour.
  42. draw_circle(Vector2(309, 297), 20.0, Color("#8176a9"))Place a round motif on the small product label using a centre point, radius and colour.
  43. draw_rect(Rect2(405, 294, 72, 104), MINT)Draw one visible part of the display using its x, y, width and height, then fill it with the supplied colour.
  44. draw_rect(Rect2(412, 314, 58, 44), CREAM)Draw one visible part of the display using its x, y, width and height, then fill it with the supplied colour.
  45. draw_circle(Vector2(440, 335), 15.0, Color("#46876d"))Place a round motif on the small product label using a centre point, radius and colour.
  46. 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.
  47. 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.

Find and fix a bug

If the bulletin displays %d literally, check for the second percent sign after the closing quote and confirm it is followed by waiting.

Try a change

Start with 8. Make two practice labels with two subtraction lines and use Output to check that 6 remain.