Market Day London · Lesson 40 of 50 · Intermediate to advanced · About 35 min
Before the next rush, Nia tests a blocked road, order matching, full baskets and the exact turn target.

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/39 when the lesson builds on prior work, or copy the checkpoints/40 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 Array, Dictionary, assertion result, for, Array.sort.
-
Import the editable checkpoint
In the Godot 4.7.2 Project Manager, select Import and open the project.godot in this lesson folder. Open main.gd in the FileSystem dock. Press F5 to run the market and F8 to stop it before editing.
Why it works This checkpoint already has a Control root with its script attached, so you can compare your edits to a working market scene.
-
Run the complete suite
Run the project and press Run checks. Count the seven results before turning on the deliberate bug.
var checks := {Why it works Each check gives an input and compares the result to something a real player depends on.
-
Test a blocked route
Find the line for the rain tile. Change that case temporarily to Vector2i(1, 1), which is currently walkable, and rerun.
"Rain tile rejects movement": not legal(ARRIVAL),Why it works A nearby unblocked tile is a useful opposite case. The original check must use the blocked coordinate.
-
Test bag contents rather than pickup order
Read matches and run through apple and bread in both orders. Compare the duplicate-apple test below it.
sorted_basket.sort()Why it works Sorting creates the same ordering for matching contents while preserving quantities. A single apple still differs from two apples.
-
Catch a boundary error
Turn on Allow fourth item (bug). Read can_pick_up and compare less than with less than or equal to.
return size_now <= LIMIT if faulty.button_pressed else size_now < LIMITWhy it works With a capacity of three, a size of three means no room. The mistaken inclusive version opens a fourth space.
-
Repair and rerun
Turn off the deliberate bug and rerun the same suite. In a duplicate lab, add a check for earning gold one turn over the target.
results.text = "%d/%d passedWhy it works A repeatable report proves the repair fixed the behaviour you tested. A boundary pair catches both too much and too little allowance.
Watch the values change
-
Run normal rules All seven expected conditions hold.
What you see. The headline reads 7/7 passed. -
Enable the deliberate bug The full-basket guard is too generous.
What you see. Three items block a fourth shows a cross. -
Disable it and rerun The strict comparison is restored.
What you see. The headline returns to 7/7.
Predict before you run What stays red when the deliberate basket bug is enabled?
Reveal the answer
Three items block a fourth. Six of the seven checks still pass.
Complete checkpoint
Run checks reports 7/7 with normal rules, 6/7 with the deliberate bug and 7/7 when the rule is restored.
main.gd
extends Control
const LIMIT := 3
const ARRIVAL := Vector2i(2, 1)
var faulty: Button
var results: Label
func _ready() -> void:
heading("Test the market before opening day", "SCENARIO QA")
label("Run the checks, then switch on a deliberate bad rule.", Vector2(44, 129), 18, MINT)
faulty = button("Allow fourth item (bug)", Vector2(44, 187), run_suite, 225)
faulty.toggle_mode = true
button("Run checks", Vector2(284, 187), run_suite, 154)
results = label("", Vector2(44, 254), 20)
run_suite()
func can_pick_up(size_now: int) -> bool:
return size_now <= LIMIT if faulty.button_pressed else size_now < LIMIT
func legal(destination: Vector2i) -> bool:
return destination.x >= 0 and destination.x < 7 and destination.y >= 0 and destination.y < 6 and destination != ARRIVAL
func matches(basket: Array[String], demand: Array[String]) -> bool:
var sorted_basket := basket.duplicate()
var sorted_order := demand.duplicate()
sorted_basket.sort()
sorted_order.sort()
return sorted_basket == sorted_order
func is_gold(turns: int, target: int) -> bool:
return turns <= target
func run_suite() -> void:
var checks := {
"Rain tile rejects movement": not legal(ARRIVAL),
"Outside the lane rejects movement": not legal(Vector2i(7, 3)),
"Two items leave a space": can_pick_up(2),
"Three items block a fourth": not can_pick_up(3),
"Exact order ignores pickup sequence": matches(["apple", "bread"], ["bread", "apple"]),
"A missing duplicate item fails": not matches(["apple"], ["apple", "apple"]),
"The exact target earns gold": is_gold(14, 14)
}
var passed := 0
var lines: Array[String] = []
for description in checks:
var correct := bool(checks[description])
passed += int(correct)
lines.append(("%s " % ("✓" if correct else "✗")) + description)
results.text = "%d/%d passed
" % [passed, checks.size()] + "
".join(lines)
func _draw() -> void:
base()
const INK := Color("#173353")
const PAPER := Color("#fff4db")
const MINT := Color("#a8e0c1")
const GOLD := Color("#ffd291")
func heading(caption: String, topic: String) -> void:
label("MARKET DAY LONDON / " + topic, Vector2(44, 36), 15, GOLD)
label(caption, Vector2(44, 66), 31)
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 button(words: String, at: Vector2, action: Callable, width: float = 164.0) -> Button:
var control := Button.new()
control.text = words
control.position = at
control.custom_minimum_size = Vector2(width, 43)
control.pressed.connect(action)
add_child(control)
return control
func base(colour: Color = Color("#193652")) -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))
draw_rect(Rect2(Vector2(22, 20), size - Vector2(44, 40)), colour)
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 ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules. -
const LIMIT := 3Creates a constant named LIMIT. The expression uses the whole number 3. Godot infers the type from the first value. -
const ARRIVAL := Vector2i(2, 1)This coordinate is used as a sample blocked arrival in the rain scenario. -
var faulty: ButtonDeclares faulty for later use. A colon followed by a type says which kind of value belongs here. -
var results: LabelDeclares results for later use. A colon followed by a type says which kind of value belongs here. -
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. -
heading("Test the market before opening day", "SCENARIO QA")Calls heading with the values in its parentheses. -
label("Run the checks, then switch on a deliberate bad rule.", Vector2(44, 129), 18, MINT)Calls label with the values in its parentheses. -
faulty = button("Allow fourth item (bug)", Vector2(44, 187), run_suite, 225)Stores the new value in faulty. The expression calls button with the values in its parentheses. -
faulty.toggle_mode = trueStores the new value in faulty.toggle_mode. The expression uses the yes-or-no value true. -
button("Run checks", Vector2(284, 187), run_suite, 154)Calls button with the values in its parentheses. -
results = label("", Vector2(44, 254), 20)Stores the new value in results. The expression calls label with the values in its parentheses. -
run_suite()Calls run_suite with the values in its parentheses. -
func can_pick_up(size_now: int) -> bool:Defines can pick up, 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. -
return size_now <= LIMIT if faulty.button_pressed else size_now < LIMITThe bug permits a grab when the basket size already equals its limit. Compare the two operators to see the boundary change. -
func legal(destination: Vector2i) -> bool:Defines legal, 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. -
return destination.x >= 0 and destination.x < 7 and destination.y >= 0 and destination.y < 6 and destination != ARRIVALEvery condition must be true. The candidate needs to be inside both axes of the board and away from the blocked tile. -
func matches(basket: Array[String], demand: Array[String]) -> bool:Defines matches, 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 sorted_basket := basket.duplicate()Make a copy of the basket before sorting so the checking function preserves the on-screen pickup order. -
var sorted_order := demand.duplicate()Creates a variable named sorted_order. The expression copies the collection so a later change can leave the original alone. Godot infers the type from the first value. -
sorted_basket.sort()Calls sorted_basket.sort with the values in its parentheses. -
sorted_order.sort()Sort the order copy too. This gives both lists the same ordering before comparison. -
return sorted_basket == sorted_orderArray equality compares the sorted items and their counts. One apple will fail a two-apple demand. -
func is_gold(turns: int, target: int) -> bool:Defines is gold, 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. -
return turns <= targetUse <= because a shift completed exactly at the turn target earns gold. -
func run_suite() -> void:Defines run suite, 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 checks := {Creates a variable named checks. The expression makes a dictionary that looks up values by key. Godot infers the type from the first value. -
"Rain tile rejects movement": not legal(ARRIVAL),The phrase on the left describes what to expect, and the Boolean expression on the right checks it. -
"Outside the lane rejects movement": not legal(Vector2i(7, 3)),Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"Two items leave a space": can_pick_up(2),Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"Three items block a fourth": not can_pick_up(3),A basket with three items is already full. This is the check that should fail when the deliberate bug is active. -
"Exact order ignores pickup sequence": matches(["apple", "bread"], ["bread", "apple"]),Order changes never matter to a complete match. These arrays contain the same ingredients in reverse sequence. -
"A missing duplicate item fails": not matches(["apple"], ["apple", "apple"]),The second case protects duplicate demand. Sorting preserves duplicates for the equality check. -
"The exact target earns gold": is_gold(14, 14)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. -
var passed := 0Creates a variable named passed. The expression uses the whole number 0. Godot infers the type from the first value. -
var lines: Array[String] = []Creates a variable named lines. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold. -
for description in checks:Loop through the descriptions so the report can display each scenario’s name with its result. -
var correct := bool(checks[description])Creates a variable named correct. The expression calls bool with the values in its parentheses. Godot infers the type from the first value. -
passed += int(correct)Converting true to an integer adds one pass. Converting false adds zero. -
lines.append(("%s " % ("✓" if correct else "✗")) + description)Choose a checkmark or a cross then append it with the description to the list of report rows. -
results.text = "%d/%d passedShow the number that passed alongside the total, then join the individual results into one label. -
" % [passed, checks.size()] + "Continues this part of the script. Read the expression on this line in the context of the nearby declaration or function. -
".join(lines)Continues this part of the script. Read the expression on this line in the context of the nearby declaration or function. -
func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state. -
base()Calls base with the values in its parentheses. -
const INK := Color("#173353")Creates a constant named INK. The expression builds a colour from these values. Godot infers the type from the first value. -
const PAPER := Color("#fff4db")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value. -
const MINT := Color("#a8e0c1")Creates a constant named MINT. The expression builds a colour from these values. Godot infers the type from the first value. -
const GOLD := Color("#ffd291")Creates a constant named GOLD. The expression builds a colour from these values. Godot infers the type from the first value. -
func heading(caption: String, topic: String) -> void:Defines heading, 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. -
label("MARKET DAY LONDON / " + topic, Vector2(44, 36), 15, GOLD)Calls label with the values in its parentheses. -
label(caption, Vector2(44, 66), 31)Calls label with the values in its parentheses. -
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. -
var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value. -
item.text = wordsUpdates item.text, which controls the words a player sees. The expression reads words. -
item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at. -
item.add_theme_font_size_override("font_size", pixels)Calls item.add_theme_font_size_override with the values in its parentheses. -
item.add_theme_color_override("font_color", ink)Calls item.add_theme_color_override with the values in its parentheses. -
add_child(item)Adds the new node to the scene tree so that Godot can run and draw it. -
return itemFinishes the function and hands its result back to the caller. The expression reads item. -
func button(words: String, at: Vector2, action: Callable, width: float = 164.0) -> Button:Defines 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. -
var control := Button.new()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value. -
control.text = wordsUpdates control.text, which controls the words a player sees. The expression reads words. -
control.position = atUpdates control.position, which controls the node's local x and y location. The expression reads at. -
control.custom_minimum_size = Vector2(width, 43)Updates control.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value. -
control.pressed.connect(action)Links a signal to the function that should run when the event happens. -
add_child(control)Adds the new node to the scene tree so that Godot can run and draw it. -
return controlFinishes the function and hands its result back to the caller. The expression reads control. -
func base(colour: Color = Color("#193652")) -> void:Defines base, 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. -
draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))Paints a rectangle at the given position and size. -
draw_rect(Rect2(Vector2(22, 20), size - Vector2(44, 40)), colour)Paints a rectangle at the given position and size.
Find and fix a bug
If sorting makes the player's displayed basket jump, check that both arrays are copied before sorting.
Try a change
Ages 10–12 can add a gold target example one turn under par. Ages 13–16 can group these regression checks into movement, orders and results, then run them after each change.