Tiny Shelf 03 · Post shelf notes with a reusable function

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

The studio needs a note before and after a label goes on a miniature. Use one short instruction instead of rewriting the whole bulletin twice.

Miniature packaging workshop with coloured paper models, tools and a teal cutting mat.
What you’ll make Write a named function that adds a message and the current count to the bulletin.

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

Supply a message. Pass text to post_note. → Read the count. The function sees waiting. → Post and print. Show both values together.
Supply a message → Read the count → Post and print
Godot checkpoint window for Post shelf notes with a reusable function
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson func, parameters, String, void, function calls, format arrays.

  1. Keep the count available

    Leave waiting as a variable above _ready so both functions can read it.

    var waiting: int = 3

    Why it works A variable declared on the script can be used by other functions on the same node.

  2. Define the new instruction

    Add post_note below _ready. Put message: String in parentheses and specify void after the arrow.

    func post_note(message: String) -> void:

    Why it works The caller supplies a text argument. void says the function doesn't hand a value back.

  3. Fill two placeholders

    Inside post_note, copy the bulletin assignment. Keep message and waiting in that order inside the square brackets.

    	bulletin.text = "%s  ·  Waiting: %d" % [message, waiting]

    Why it works %s takes the message and %d takes the number. The Array supplies two values in the same order as the placeholders.

  4. Send the note to Output

    Indent print on the next line inside post_note. Match the assignment's indentation.

    	print(bulletin.text)

    Why it works Every time the function runs, it both changes the screen message and logs it.

  5. Call the instruction twice

    In _ready, replace the old count-reporting lines with the first call, subtraction and second call.

    	post_note("Ready to label")
    	waiting -= 1
    	post_note("One label made")

    Why it works The first call sees waiting at 3. The next line changes it. The second call formats a different message and sees 2.

  6. Check the history

    Run and compare the workbench bulletin with the two lines in Output. Move the subtraction above both calls for one experiment and put it back afterwards.

    Why it works The order of three lines decides when each function reads waiting. The last call replaces the on-screen message. The Output panel keeps both lines.

Watch the values change

  1. Call post_note for the first time. message is "Ready to label" and waiting is 3.
    What you see. The bulletin and Output show Waiting: 3.
  2. Subtract once. waiting becomes 2.
    What you see. The next note will see 2.
  3. Call post_note again. message is "One label made" and waiting is 2.
    What you see. The final bulletin and new Output line show Waiting: 2.

Predict before you run If you call post_note again and don't subtract again, what count will it show?

Reveal the answer

It still shows 2. The function reads waiting. It does not change waiting.

Complete checkpoint

Output shows two notes in order. The final note says One label made and Waiting: 2.

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)
	post_note("Ready to label")
	waiting -= 1
	post_note("One label made")
	make_label("Post a shelf note", Vector2(598, 181), 26, NAVY)
	make_label("The same function reports both states.", Vector2(599, 226), 14, 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)


func post_note(message: String) -> void:
	bulletin.text = "%s  ·  Waiting: %d" % [message, waiting]
	print(bulletin.text)

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. post_note("Ready to label")Call the helper with the opening status. It reads waiting while the value is still three.
  15. waiting -= 1Read waiting, subtract one and put the new value back under the same name.
  16. post_note("One label made")Call the same helper after the subtraction. It formats its new message using the lower count.
  17. make_label("Post a shelf note", Vector2(598, 181), 26, NAVY)Give the workbench one explanatory caption at the requested pixel position.
  18. make_label("The same function reports both states.", Vector2(599, 226), 14, NAVY)Give the workbench one explanatory caption at the requested pixel position.
  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.
  48. func post_note(message: String) -> void:Make a reusable instruction that receives text. void means it updates the bulletin without returning an answer.
  49. bulletin.text = "%s · Waiting: %d" % [message, waiting]Fill %s with message and %d with waiting, then assign the completed text.
  50. print(bulletin.text)Read the current words on the bulletin and send those same words to Godot's Output panel.

Find and fix a bug

If the wrong word or number appears, compare the list order to the placeholders. Put message before waiting.

Try a change

Add a third call announcing a break. Try it before and after the subtraction and describe which line in Output changes place.