Tiny Shelf Studio · Lesson 21 of 50 · Intermediate · About 27 min
A customer asks for a window display using the studio’s tiny products. You have twelve shelf spaces on your plan. Six are available for this first brief.

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/20 when the lesson builds on prior work, or copy the checkpoints/21 folder to try this lesson on its own. Read the file names in the steps before editing. Keep the checkpoint ZIP intact and make edits in an extracted working copy.


Build it step by step
Language and tools in this lesson coordinates, constant, row-major order, Array, Dictionary, signal binding.
-
Follow the window plan
Run the checkpoint. Count four columns and three rows from the screen. Read the labels on active spaces and look for the marks on the blocked ones.
const CAPACITY := WIDTH * HEIGHTWhy it works A 4 × 3 grid always keeps twelve positions. A brief can open fewer positions without changing where the remaining spaces are drawn.
-
Separate the plan from this attempt
Open shelf_rules.gd and find load_brief. Read the name, active slots and stock. Look up Lemon Fizz and Moon Milk inside the stock dictionary. Find the separate blank_board function.
board.fill("")Why it works The brief says what the studio can use. The board array holds this attempt and can be reset independently.
-
Find a square with remainder
Trace coordinates for slot 6. Divide by four and use the whole-number result as y. Find the remainder to get x. Press Locate display slots in the running project and compare.
return Vector2i(slot % WIDTH, slot / WIDTH)Why it works The remainder tells how far right the slot sits in its row. Integer division tells how many full rows come before it.
-
Turn coordinates back into a slot
Find slot_index. Use x = 2, y = 1 from the previous step. Multiply the row by four and add the column. On paper repeat for x = 0, y = 2.
return cell.y * WIDTH + cell.xWhy it works Each earlier row contributes four spaces. The x value adds the position in the current row.
-
Send one slot number with a click
Find build_display in main.gd. Notice that each button connects to choose_slot with its own slot. With Lemon Fizz selected, click R2 C3. Then try a blocked square.
var button := make_button("", box, choose_slot.bind(slot))Why it works bind provides the slot number later, when the button is pressed. The disabled guard stops unavailable squares from accepting a product.
-
Reset the board and read the count
Click Reset board and compare the twelve board spaces, the filled count and the stock buttons with what you saw at the start.
board = Shelf.blank_board()Why it works Reset builds a fresh board, and the stock totals are read from the brief each time the labels refresh.
Watch the values change
-
Locate slot 6 6 divided by 4 gives 1 whole row with 2 left.
What you see. The debug label reports x 2, y 1, or R2 C3. -
Click R2 C3 with held Lemon Fizz Slot 6 is active and the brief supplies one Lemon Fizz.
What you see. One slot fills and the remaining Lemon Fizz stock becomes zero. -
Reset board The brief still holds six open slots and its original stock.
What you see. The board is blank and Lemon Fizz stock returns to one.
Predict before you run What coordinates do you get for index 8?
Reveal the answer
The coordinates are (0, 2). Two complete rows come first and no spaces remain across. The display label is R3 C1.
Complete checkpoint
Locate display slots reports R2 C3 for 6. A stock selection can fill six allowed squares.
main.gd
extends Control
const Shelf = preload("res://shelf_rules.gd")
const UNIT_TITLE := "Build the brief and display grid"
const INTRO := "Build the grid and locate a display slot."
const BOARD_BG := Color("#1b3142")
const PAPER := Color("#f6ecdb")
const TINT := Color("#71bdd0")
const TOP := Vector2(45, 207)
const STEP := Vector2(122, 59)
var brief: Dictionary
var board: Array
var chosen := "lemon_fizz"
var last_slot := -1
var tile_buttons: Array[Button] = []
var stock_buttons := {}
var detail_label: Label
var output_label: Label
var count_label: Label
var left_caption: Label
func _ready() -> void:
brief = Shelf.load_brief()
board = Shelf.blank_board()
make_label("TINY SHELF STUDIO / LESSON 21", Rect2(45, 24, 590, 23), 14, TINT)
make_label(UNIT_TITLE, Rect2(45, 55, 855, 45), 32)
make_label(INTRO, Rect2(45, 107, 870, 47), 16, Color("#bbcdd2"), true)
left_caption = make_label("01 • DISPLAY GRID", Rect2(45, 163, 480, 29), 16)
make_label("02 • MINI PRODUCT STOCK", Rect2(578, 163, 336, 29), 16)
detail_label = make_label("", Rect2(580, 369, 335, 47), 15, TINT, true)
output_label = make_label("", Rect2(65, 480, 824, 43), 17, PAPER, true)
count_label = make_label("", Rect2(45, 388, 495, 30), 16, TINT)
if brief.is_empty():
output_label.text = "The brief file is missing or invalid. Repair the JSON and run again."
return
build_display()
refresh()
output_label.text = "%s | Choose a product and click an open shelf space." % brief["title"]
func build_display() -> void:
for slot in range(Shelf.CAPACITY):
var cell := Shelf.coordinates(slot)
var box := Rect2(TOP + Vector2(cell) * STEP, Vector2(111, 48))
var button := make_button("", box, choose_slot.bind(slot))
button.disabled = not brief["active"].has(slot)
tile_buttons.append(button)
var index := 0
for item in brief["stock"]:
@warning_ignore("integer_division")
var position := Vector2(578 + (index % 2) * 177, 207 + (index / 2) * 51)
var button := make_button("", Rect2(position, Vector2(165, 43)), choose_product.bind(item))
stock_buttons[item] = button
index += 1
make_button("Empty hand", Rect2(578, 432, 165, 35), choose_product.bind(""))
make_button("Reset board", Rect2(755, 432, 165, 35), reset_board)
make_button("Locate display slots", Rect2(45, 432, 245, 35), run_task)
func describe_slot(slot: int) -> String:
var cell := Shelf.coordinates(slot)
return "R%d C%d" % [cell.y + 1, cell.x + 1]
func choose_slot(slot: int) -> void:
var trial := Shelf.copy_for_placement(brief, board, slot, chosen)
if trial.is_empty():
output_label.text = "That shelf space or stock is unavailable. The display stayed the same."
return
board = trial
last_slot = slot
refresh()
output_label.text = "%s / %s" % [describe_slot(slot), Shelf.display_name(chosen)]
func choose_product(item: String) -> void:
chosen = item
refresh()
output_label.text = "Held product / %s" % Shelf.display_name(chosen)
func reset_board() -> void:
board = Shelf.blank_board()
last_slot = -1
refresh()
output_label.text = "Fresh display. Stock returned."
func refresh() -> void:
left_caption.text = "01 • DISPLAY GRID / %s" % brief["title"]
var placed := 0
for slot in range(tile_buttons.size()):
var button: Button = tile_buttons[slot]
var product: String = board[slot]
if button.disabled:
button.text = "—"
elif product.is_empty():
button.text = describe_slot(slot)
button.add_theme_color_override("font_color", Color("#a9c7d3"))
else:
var data: Array = Shelf.PRODUCTS[product]
button.text = "%s • %s" % [data[3], describe_slot(slot)]
button.add_theme_color_override("font_color", Color(data[2]))
placed += 1
if slot == last_slot and not button.disabled:
button.add_theme_color_override("font_hover_color", Color("#fae8a4"))
for item in stock_buttons:
var button: Button = stock_buttons[item]
var data: Array = Shelf.PRODUCTS[item]
var left := int(brief["stock"][item]) - board.count(item)
button.text = "%s%s ×%d" % ["› " if item == chosen else "", data[0], left]
button.add_theme_color_override("font_color", Color(data[2]))
detail_label.text = "In hand / %s" % Shelf.display_name(chosen)
count_label.text = "%d of %d display slots filled" % [placed, brief["active"].size()]
func run_task() -> void:
var cell := Shelf.coordinates(6)
output_label.text = "Slot 6 is x %d, y %d. R2 C3 in the studio." % [cell.x, cell.y]
func make_label(value: String, box: Rect2, points: int, ink: Color = PAPER, wraps: bool = false) -> Label:
var label := Label.new()
label.text = value
label.position = box.position
label.size = box.size
label.add_theme_font_size_override("font_size", points)
label.add_theme_color_override("font_color", ink)
if wraps:
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
add_child(label)
return label
func make_button(value: String, box: Rect2, action: Callable) -> Button:
var button := Button.new()
button.text = value
button.position = box.position
button.size = box.size
button.add_theme_font_size_override("font_size", 14)
button.add_theme_color_override("font_color", PAPER)
button.add_theme_color_override("font_disabled_color", Color("#6f8a9b"))
for mode in ["normal", "hover", "pressed", "disabled", "focus"]:
var panel := StyleBoxFlat.new()
panel.bg_color = Color("#233e52") if mode == "normal" else Color("#30526a")
if mode == "disabled":
panel.bg_color = Color("#1e3549")
panel.border_color = Color("#72b2c5") if mode == "focus" else Color("#42677e")
panel.set_border_width_all(1)
panel.set_corner_radius_all(7)
button.add_theme_stylebox_override(mode, panel)
button.pressed.connect(action)
add_child(button)
return button
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#132735"))
draw_rect(Rect2(30, 150, 517, 323), BOARD_BG)
draw_rect(Rect2(562, 150, 370, 323), BOARD_BG)
draw_rect(Rect2(45, 480, 870, 43), Color("#204452"))
draw_rect(Rect2(45, 480, 4, 43), TINT)
draw_line(Vector2(45, 144), Vector2(915, 144), Color("#436173"), 1.0)
shelf_rules.gd
extends RefCounted
const WIDTH := 4
const HEIGHT := 3
const CAPACITY := WIDTH * HEIGHT
const STOCK_PATH := "res://morning_window.json"
const PRODUCTS := {
"lemon_fizz": ["Lemon Fizz", "drinks", "#f4be5b", "FIZZ"],
"moon_milk": ["Moon Milk", "drinks", "#7993f0", "MOON"],
"jam_toast": ["Jam Toast", "snacks", "#e5829b", "JAM"],
"comet_crisps": ["Comet Crisps", "snacks", "#ec985e", "COMET"],
"rainbow_soap": ["Rainbow Soap", "care", "#72c3be", "SOAP"],
"mint_paste": ["Mint Paste", "care", "#a7d8a4", "MINT"],
}
static func coordinates(slot: int) -> Vector2i:
@warning_ignore("integer_division")
return Vector2i(slot % WIDTH, slot / WIDTH)
static func slot_index(cell: Vector2i) -> int:
return cell.y * WIDTH + cell.x
static func blank_board() -> Array:
var board: Array = []
board.resize(CAPACITY)
board.fill("")
return board
static func copy_for_placement(brief: Dictionary, board: Array, slot: int, item: String) -> Array:
if slot < 0 or slot >= CAPACITY or not brief["active"].has(slot):
return []
if not item.is_empty():
var total := int(brief["stock"].get(item, 0))
var used := board.count(item)
if board[slot] == item:
used -= 1
if used >= total:
return []
var next := board.duplicate()
next[slot] = item
return next
static func display_name(item: String) -> String:
return "Empty" if item.is_empty() else PRODUCTS[item][0]
static func load_brief() -> Dictionary:
return {"id": "morning_window", "title": "Morning Window", "chapter": "The Miniature Display",
"active": [0, 1, 2, 4, 5, 6], "stock": {"lemon_fizz": 1, "moon_milk": 1,
"jam_toast": 1, "comet_crisps": 1, "rainbow_soap": 1, "mint_paste": 1},
"fixed": [{"slot": 0, "product": "lemon_fizz"}],
"rows": [{"row": 0, "category": "snacks", "count": 2},
{"row": 1, "category": "care", "count": 2}],
"pairs": [{"left": "moon_milk", "right": "rainbow_soap"}]}
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 ControlUse a screen node that can hold the labels, stock controls and clickable board. -
const Shelf = preload("res://shelf_rules.gd")Load the shelf-rule script to make its static functions available without placing a second screen in the scene. -
const UNIT_TITLE := "Build the brief and display grid"Creates a constant named UNIT_TITLE. The expression uses the text inside the quotes. Godot infers the type from the first value. -
const INTRO := "Build the grid and locate a display slot."Creates a constant named INTRO. The expression uses the text inside the quotes. Godot infers the type from the first value. -
const BOARD_BG := Color("#1b3142")Creates a constant named BOARD_BG. The expression builds a colour from these values. Godot infers the type from the first value. -
const PAPER := Color("#f6ecdb")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value. -
const TINT := Color("#71bdd0")Creates a constant named TINT. The expression builds a colour from these values. Godot infers the type from the first value. -
const TOP := Vector2(45, 207)Creates a constant named TOP. The expression packs x and y into one value. Godot infers the type from the first value. -
const STEP := Vector2(122, 59)Creates a constant named STEP. The expression packs x and y into one value. Godot infers the type from the first value. -
var brief: DictionaryKeep the design rules and stock for this attempt. The board can change while this dictionary stays the same. -
var board: ArrayStore exactly one product ID or empty string for each shelf position. -
var chosen := "lemon_fizz"Hold the selected stock ID. The player chooses a position separately. -
var last_slot := -1Remember the last position visited so the view can give local feedback. -
var tile_buttons: Array[Button] = []Hold references to the twelve visible slots in array order. -
var stock_buttons := {}Match each available product ID to its button. -
var detail_label: LabelDeclares detail_label for later use. A colon followed by a type says which kind of value belongs here. -
var output_label: LabelDeclares output_label for later use. A colon followed by a type says which kind of value belongs here. -
var count_label: LabelDeclares count_label for later use. A colon followed by a type says which kind of value belongs here. -
var left_caption: LabelDeclares left_caption for later use. A colon followed by a type says which kind of value belongs here. -
func _ready() -> void:Build the user interface when Godot has added this Control node to its scene tree. -
brief = Shelf.load_brief()Get a validated client card before using any of its stored fields. -
board = Shelf.blank_board()Make a clean array of twelve shelf positions before drawing the display. -
make_label("TINY SHELF STUDIO / LESSON 21", Rect2(45, 24, 590, 23), 14, TINT)Calls make_label with the values in its parentheses. -
make_label(UNIT_TITLE, Rect2(45, 55, 855, 45), 32)Calls make_label with the values in its parentheses. -
make_label(INTRO, Rect2(45, 107, 870, 47), 16, Color("#bbcdd2"), true)Calls make_label with the values in its parentheses. -
left_caption = make_label("01 • DISPLAY GRID", Rect2(45, 163, 480, 29), 16)Stores the new value in left_caption. The expression calls make_label with the values in its parentheses. -
make_label("02 • MINI PRODUCT STOCK", Rect2(578, 163, 336, 29), 16)Calls make_label with the values in its parentheses. -
detail_label = make_label("", Rect2(580, 369, 335, 47), 15, TINT, true)Stores the new value in detail_label. The expression calls make_label with the values in its parentheses. -
output_label = make_label("", Rect2(65, 480, 824, 43), 17, PAPER, true)Stores the new value in output_label. The expression calls make_label with the values in its parentheses. -
count_label = make_label("", Rect2(45, 388, 495, 30), 16, TINT)Stores the new value in count_label. The expression calls make_label with the values in its parentheses. -
if brief.is_empty():Explain the data problem and return before trying to act on an invalid shelf plan. -
output_label.text = "The brief file is missing or invalid. Repair the JSON and run again."Updates output_label.text, which controls the words a player sees. The expression uses the text inside the quotes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
build_display()Calls build_display with the values in its parentheses. -
refresh()Calls refresh with the values in its parentheses. -
output_label.text = "%s | Choose a product and click an open shelf space." % brief["title"]Updates output_label.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func build_display() -> void:Create all twelve tiles, then attach stock and teaching controls. -
for slot in range(Shelf.CAPACITY):Make one button per position. range stops before capacity, so the final index is 11. -
var cell := Shelf.coordinates(slot)Convert the flat slot number into grid coordinates for the screen layout. -
var box := Rect2(TOP + Vector2(cell) * STEP, Vector2(111, 48))Multiply the cell by the display spacing to give the button a visible rectangle. -
var button := make_button("", box, choose_slot.bind(slot))Make a clickable shelf or stock control and give it the bound argument it will use on a later press. -
button.disabled = not brief["active"].has(slot)Show inactive shelf spaces while preventing them from receiving a player placement. -
tile_buttons.append(button)Adds an item to the end of a list. -
var index := 0Creates a variable named index. The expression uses the whole number 0. Godot infers the type from the first value. -
for item in brief["stock"]:Add a button for each product the current card supplies. -
@warning_ignore("integer_division")This annotation gives Godot extra information about the declaration that follows. -
var position := Vector2(578 + (index % 2) * 177, 207 + (index / 2) * 51)Creates a variable named position. The expression packs x and y into one value. Godot infers the type from the first value. -
var button := make_button("", Rect2(position, Vector2(165, 43)), choose_product.bind(item))Make a clickable shelf or stock control and give it the bound argument it will use on a later press. -
stock_buttons[item] = buttonStores the new value in stock_buttons[item]. The expression reads button. -
index += 1Reads index, adds to its old value, then stores the result back in the same place. -
make_button("Empty hand", Rect2(578, 432, 165, 35), choose_product.bind(""))Calls make_button with the values in its parentheses. -
make_button("Reset board", Rect2(755, 432, 165, 35), reset_board)Calls make_button with the values in its parentheses. -
make_button("Locate display slots", Rect2(45, 432, 245, 35), run_task)Calls make_button with the values in its parentheses. -
func describe_slot(slot: int) -> String:Defines describe slot, 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. -
var cell := Shelf.coordinates(slot)Convert the flat slot number into grid coordinates for the screen layout. -
return "R%d C%d" % [cell.y + 1, cell.x + 1]Finishes the function and hands its result back to the caller. The expression uses the text inside the quotes. -
func choose_slot(slot: int) -> void:Send a player placement through the same stock and slot guards every time. -
var trial := Shelf.copy_for_placement(brief, board, slot, chosen)Request a candidate board without mutating the currently displayed array. -
if trial.is_empty():The action was refused. Show the reason and leave the actual grid alone. -
output_label.text = "That shelf space or stock is unavailable. The display stayed the same."Updates output_label.text, which controls the words a player sees. The expression uses the text inside the quotes. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
board = trialReplace the live array only after the new placement passed its guards. -
last_slot = slotStores the new value in last_slot. The expression reads slot. -
refresh()Calls refresh with the values in its parentheses. -
output_label.text = "%s / %s" % [describe_slot(slot), Shelf.display_name(chosen)]Updates output_label.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func choose_product(item: String) -> void:Change what the player is holding. The selection does not fill a shelf position. -
chosen = itemStores the new value in chosen. The expression reads item. -
refresh()Calls refresh with the values in its parentheses. -
output_label.text = "Held product / %s" % Shelf.display_name(chosen)Updates output_label.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func reset_board() -> void:Clear the arrangement and reread available stock from the same brief. -
board = Shelf.blank_board()Make a clean array of twelve shelf positions before drawing the display. -
last_slot = -1Stores the new value in last_slot. The expression reads -1. -
refresh()Calls refresh with the values in its parentheses. -
output_label.text = "Fresh display. Stock returned."Updates output_label.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func refresh() -> void:Read the current game values and update the text and styling. -
left_caption.text = "01 • DISPLAY GRID / %s" % brief["title"]Updates left_caption.text, which controls the words a player sees. The expression uses the text inside the quotes. -
var placed := 0Creates a variable named placed. The expression uses the whole number 0. Godot infers the type from the first value. -
for slot in range(tile_buttons.size()):Visit every visible slot to redraw its label from the current array value. -
var button: Button = tile_buttons[slot]Creates a variable named button. 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. -
var product: String = board[slot]Creates a variable named product. 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. -
if button.disabled:Checks button.disabled. The indented instructions below run only if this test passes. -
button.text = "—"Updates button.text, which controls the words a player sees. The expression uses the text inside the quotes. -
elif product.is_empty():If the earlier branches did not run, checks product.is_empty() next. -
button.text = describe_slot(slot)Updates button.text, which controls the words a player sees. The expression calls describe_slot with the values in its parentheses. -
button.add_theme_color_override("font_color", Color("#a9c7d3"))Calls button.add_theme_color_override with the values in its parentheses. -
else:Runs the indented fallback instructions if none of the earlier tests passed. -
var data: Array = Shelf.PRODUCTS[product]Read the original packet's short label and colour from the catalogue. -
button.text = "%s • %s" % [data[3], describe_slot(slot)]Updates button.text, which controls the words a player sees. The expression uses the text inside the quotes. -
button.add_theme_color_override("font_color", Color(data[2]))Calls button.add_theme_color_override with the values in its parentheses. -
placed += 1Reads placed, adds to its old value, then stores the result back in the same place. -
if slot == last_slot and not button.disabled:Checks slot == last_slot and not button.disabled. The indented instructions below run only if this test passes. -
button.add_theme_color_override("font_hover_color", Color("#fae8a4"))Calls button.add_theme_color_override with the values in its parentheses. -
for item in stock_buttons:Repeats the indented instructions for each value in stock_buttons. -
var button: Button = stock_buttons[item]Creates a variable named button. 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. -
var data: Array = Shelf.PRODUCTS[item]Creates a variable named data. 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. -
var left := int(brief["stock"][item]) - board.count(item)Subtract displayed copies from the brief allowance to show how many remain. -
button.text = "%s%s ×%d" % ["› " if item == chosen else "", data[0], left]Updates button.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
button.add_theme_color_override("font_color", Color(data[2]))Calls button.add_theme_color_override with the values in its parentheses. -
detail_label.text = "In hand / %s" % Shelf.display_name(chosen)Updates detail_label.text, which controls the words a player sees. The expression uses the text inside the quotes. -
count_label.text = "%d of %d display slots filled" % [placed, brief["active"].size()]Updates count_label.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func run_task() -> void:Run this lesson’s checking action and show the result before editing the layout again. -
var cell := Shelf.coordinates(6)Convert the flat slot number into grid coordinates for the screen layout. -
output_label.text = "Slot 6 is x %d, y %d. R2 C3 in the studio." % [cell.x, cell.y]Updates output_label.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func make_label(value: String, box: Rect2, points: int, ink: Color = PAPER, wraps: bool = false) -> Label:Build consistent headings, caption text and feedback from one helper. -
var label := Label.new()Creates a variable named label. The expression creates a new Label object. Godot infers the type from the first value. -
label.text = valueUpdates label.text, which controls the words a player sees. The expression reads value. -
label.position = box.positionUpdates label.position, which controls the node's local x and y location. The expression reads box.position. -
label.size = box.sizeUpdates label.size, which controls the control's width and height. The expression reads box.size. -
label.add_theme_font_size_override("font_size", points)Calls label.add_theme_font_size_override with the values in its parentheses. -
label.add_theme_color_override("font_color", ink)Calls label.add_theme_color_override with the values in its parentheses. -
if wraps:Checks wraps. The indented instructions below run only if this test passes. -
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTAllow the sentence to wrap within the area reserved for it. -
add_child(label)Adds the new node to the scene tree so that Godot can run and draw it. -
return labelFinishes the function and hands its result back to the caller. The expression reads label. -
func make_button(value: String, box: Rect2, action: Callable) -> Button:Make controls with consistent style and room to select them. -
var button := Button.new()Creates a variable named button. The expression creates a new Button object. Godot infers the type from the first value. -
button.text = valueUpdates button.text, which controls the words a player sees. The expression reads value. -
button.position = box.positionUpdates button.position, which controls the node's local x and y location. The expression reads box.position. -
button.size = box.sizeUpdates button.size, which controls the control's width and height. The expression reads box.size. -
button.add_theme_font_size_override("font_size", 14)Calls button.add_theme_font_size_override with the values in its parentheses. -
button.add_theme_color_override("font_color", PAPER)Calls button.add_theme_color_override with the values in its parentheses. -
button.add_theme_color_override("font_disabled_color", Color("#6f8a9b"))Calls button.add_theme_color_override with the values in its parentheses. -
for mode in ["normal", "hover", "pressed", "disabled", "focus"]:Repeats the indented instructions for each value in ["normal", "hover", "pressed", "disabled", "focus"]. -
var panel := StyleBoxFlat.new()Creates a variable named panel. The expression creates a new StyleBoxFlat object. Godot infers the type from the first value. -
panel.bg_color = Color("#233e52") if mode == "normal" else Color("#30526a")Stores the new value in panel.bg_color. The expression builds a colour from these values. -
if mode == "disabled":Checks mode == "disabled". The indented instructions below run only if this test passes. -
panel.bg_color = Color("#1e3549")Stores the new value in panel.bg_color. The expression builds a colour from these values. -
panel.border_color = Color("#72b2c5") if mode == "focus" else Color("#42677e")Stores the new value in panel.border_color. The expression builds a colour from these values. -
panel.set_border_width_all(1)Calls panel.set_border_width_all with the values in its parentheses. -
panel.set_corner_radius_all(7)Calls panel.set_corner_radius_all with the values in its parentheses. -
button.add_theme_stylebox_override(mode, panel)Calls button.add_theme_stylebox_override with the values in its parentheses. -
button.pressed.connect(action)Route the button's signal to the action supplied when it was created. -
add_child(button)Adds the new node to the scene tree so that Godot can run and draw it. -
return buttonFinishes the function and hands its result back to the caller. The expression reads button. -
func _draw() -> void:Paint the lesson background and display panels without changing the game. -
draw_rect(Rect2(Vector2.ZERO, size), Color("#132735"))Paints a rectangle at the given position and size. -
draw_rect(Rect2(30, 150, 517, 323), BOARD_BG)Paints a rectangle at the given position and size. -
draw_rect(Rect2(562, 150, 370, 323), BOARD_BG)Paints a rectangle at the given position and size. -
draw_rect(Rect2(45, 480, 870, 43), Color("#204452"))Paints a rectangle at the given position and size. -
draw_rect(Rect2(45, 480, 4, 43), TINT)Paints a rectangle at the given position and size. -
draw_line(Vector2(45, 144), Vector2(915, 144), Color("#436173"), 1.0)Paints a line between two points with the chosen colour and width.
Read shelf_rules.gd line by line
-
extends RefCountedKeep the shelf rules independent from Godot buttons. The live scene, hint and test tray can all call this script. -
const WIDTH := 4Define four columns in each practice shelf row. -
const HEIGHT := 3Define three rows of practice shelf space. -
const CAPACITY := WIDTH * HEIGHTMultiply the dimensions to reserve all twelve array entries, including slots closed by a brief. -
const STOCK_PATH := "res://morning_window.json"This res:// path points to the default client card inside the extracted Godot checkpoint. -
const PRODUCTS := {Use original miniature IDs as keys. Each product holds a label, category, drawing colour and short tag. -
"lemon_fizz": ["Lemon Fizz", "drinks", "#f4be5b", "FIZZ"],Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"moon_milk": ["Moon Milk", "drinks", "#7993f0", "MOON"],Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"jam_toast": ["Jam Toast", "snacks", "#e5829b", "JAM"],Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"comet_crisps": ["Comet Crisps", "snacks", "#ec985e", "COMET"],Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"rainbow_soap": ["Rainbow Soap", "care", "#72c3be", "SOAP"],Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"mint_paste": ["Mint Paste", "care", "#a7d8a4", "MINT"],Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
}Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
static func coordinates(slot: int) -> Vector2i:Convert the index in the board array into its across-and-down position. -
@warning_ignore("integer_division")The division warning acknowledges that whole-number division is intentional. Here the fraction is discarded because the result is a row index. -
return Vector2i(slot % WIDTH, slot / WIDTH)The remainder gives the column. Whole-number division by four counts the complete rows that come before the slot. -
static func slot_index(cell: Vector2i) -> int:Turn the two coordinates back into the flat index used by the board array. -
return cell.y * WIDTH + cell.xCount every space in full rows above the cell, then add the number of spaces before it in its own row. -
static func blank_board() -> Array:Return a fresh board on start, restart or a brief audit. It begins without products. -
var board: Array = []Start with an empty dynamic collection for the game state. -
board.resize(CAPACITY)Reserve a value for every possible shelf slot before the drawing code indexes it. -
board.fill("")Replace each default null value with the empty string that illustrates an unfilled shelf space. -
return boardFinishes the function and hands its result back to the caller. The expression reads board. -
static func copy_for_placement(brief: Dictionary, board: Array, slot: int, item: String) -> Array:Calculate an independent result for the attempted click. A rejected action returns an empty array. -
if slot < 0 or slot >= CAPACITY or not brief["active"].has(slot):Reject positions outside the 4 × 3 board or marked unavailable by this brief before looking at their contents. -
return []Finishes the function and hands its result back to the caller. The expression makes a list whose items keep their order. -
if not item.is_empty():An empty hand clears a slot without asking for stock. A held product must pass the count guard. -
var total := int(brief["stock"].get(item, 0))Read the total stock for this miniature. The default zero rejects an ID that this brief cannot supply. -
var used := board.count(item)Count the number of that miniature already on display, including repeated products when the card supplies copies. -
if board[slot] == item:Putting the same miniature in its own square reuses the one already there instead of spending another. -
used -= 1Reads used, subtracts from its old value, then stores the result back in the same place. -
if used >= total:Only accept an addition when at least one copy remains available after the same-position adjustment. -
return []Finishes the function and hands its result back to the caller. The expression makes a list whose items keep their order. -
var next := board.duplicate()Copy the flat array. A player click may update the new board after the guard finishes. -
next[slot] = itemSet just one position of the copy to the currently selected product ID or the empty string. -
return nextFinishes the function and hands its result back to the caller. The expression reads next. -
static func display_name(item: String) -> String:Give the UI the longer label for an ID and the friendly name Empty for the empty hand. -
return "Empty" if item.is_empty() else PRODUCTS[item][0]Finishes the function and hands its result back to the caller. The expression chooses one of two values by checking the condition in the middle. -
static func load_brief() -> Dictionary:Use a dictionary in the first checkpoint to introduce a brief before reading a file. -
return {"id": "morning_window", "title": "Morning Window", "chapter": "The Miniature Display",This fixed brief identifies its edition, active shelf spaces and initial product counts. -
"active": [0, 1, 2, 4, 5, 6], "stock": {"lemon_fizz": 1, "moon_milk": 1,Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"jam_toast": 1, "comet_crisps": 1, "rainbow_soap": 1, "mint_paste": 1},Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"fixed": [{"slot": 0, "product": "lemon_fizz"}],Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"rows": [{"row": 0, "category": "snacks", "count": 2},Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
{"row": 1, "category": "care", "count": 2}],Starts a list or dictionary used as part of the instruction above. -
"pairs": [{"left": "moon_milk", "right": "rainbow_soap"}]}Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above.
Find and fix a bug
If R2 and R3 look mixed up, check whether you added 1 only in the readable label. Rule coordinates and array indexes start at 0.
Try a change
In the practice brief, replace active slot 6 with slot 8. Press F5, explain which button becomes available and place a mini there.