London Market 09 · Find the market stalls by address

Market Day London · Lesson 09 of 50 · Beginner · About 29 min

A few letter signs are enough to label the training market. Nia can read the item each stall normally sells before she starts filling an order.

Watercolour of a dry London market lane lined with bread, produce and flower stalls.
What you’ll make Use a dictionary to place and inspect Produce, Flowers and Bakery stalls.

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

Read the stall data. Find the three keys. → Draw matching signs. Use the same grid positions. → Inspect the square. Choose the matching record or a useful prompt.
Read the stall data → Draw matching signs → Inspect the square
Godot checkpoint window for Find the market stalls by address
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson Dictionary, Vector2i keys, nested records, membership, drawing text, guard clauses.

  1. Read the map records

    Open Lesson 9 and locate STALLS. Find Produce’s coordinate, sign letter and item.

    Vector2i(1, 1): {"name": "Produce", "sign": "P", "item": "apple", "ink": Color("#f5c986")}

    Why it works Each top-level key is a grid position. Its value is a small dictionary with the label, letter, item and paint colour for that stall.

  2. Draw every stall

    In _draw, find the loop that reads the position keys from STALLS after the paving has been painted.

    for location: Vector2i in STALLS:

    Why it works The loop uses the same coordinates that the inspection rules will look up later. Moving a station’s key moves both its sign and its action.

  3. Keep the familiar scale

    Inspect the coloured squares and text after running with F5. Compare the P, F and B with the diagram.

    var counter_point := cell_centre(COUNTER)

    Why it works The signs use both colour and a letter. The collection counter uses an outline, keeping it distinct from places that supply goods.

  4. Inspect an ordinary square

    From the starting position (3,3), tap Inspect stall. Read the instruction and notice that Nia’s cell has not changed.

    if not STALLS.has(nia):

    Why it works has checks whether the current position is present among the dictionary keys. The guard stops before an absent record is read.

  5. Inspect the flower stall

    Press Up twice to reach (3,1), then tap Inspect stall. Trace the dictionary lookup using Nia’s new position.

    var stall: Dictionary = STALLS[nia]

    Why it works At the F square, STALLS[nia] returns the record whose name is Flowers and whose item is flowers.

  6. Reach the collection counter

    Walk to (6,3), tap Inspect stall and read the new message. Check the counter guard near the top of _inspect.

    if nia == COUNTER:

    Why it works The counter’s location is shared with its drawing code. Inspection explains its future purpose before any basket exists.

Watch the values change

  1. Inspect the starting square. (3,3) is not a stall key.
    What you see. The sign asks you to visit a P, F or B square.
  2. Inspect Flowers at (3,1). The record contains name Flowers and item flowers.
    What you see. The sign says Flowers sells flowers.
  3. Inspect (6,3). That position is the collection counter.
    What you see. The sign explains where orders go.

Predict before you run If Bakery’s coordinate key changes to (4,1), where will Nia look for bread?

Reveal the answer

At (4,1). The draw loop and inspection function both read the updated dictionary key.

Complete checkpoint

Three labelled stands and a collection outline appear. Inspect stall reports the correct item or a useful prompt without moving Nia.

main.gd

extends Control


const NIGHT := Color("#0d2335")
const PANEL := Color("#17374b")
const CREAM := Color("#fff4da")
const MINT := Color("#9ce4ce")
const GOLD := Color("#ffd789")
var board: Label

const ORIGIN := Vector2(47, 134)
const TILE := 57.0
const COLUMNS := 7
const ROWS := 5
var nia := Vector2i(3, 3)

const STALLS := {
	Vector2i(1, 1): {"name": "Produce", "sign": "P", "item": "apple", "ink": Color("#f5c986")},
	Vector2i(3, 1): {"name": "Flowers", "sign": "F", "item": "flowers", "ink": Color("#c5b4f0")},
	Vector2i(5, 1): {"name": "Bakery", "sign": "B", "item": "bread", "ink": Color("#f1a995")}
}
const COUNTER := Vector2i(6, 3)


func _ready() -> void:
	var title := Label.new()
	title.text = "Market Day London"
	title.position = Vector2(42, 34)
	title.add_theme_font_size_override("font_size", 34)
	title.modulate = CREAM
	add_child(title)
	board = Label.new()
	board.position = Vector2(45, 88)
	board.add_theme_font_size_override("font_size", 22)
	board.modulate = MINT
	add_child(board)
	_refresh()
	_add_button("Up", Vector2(635, 163), _move.bind(Vector2i.UP))
	_add_button("Left", Vector2(517, 221), _move.bind(Vector2i.LEFT))
	_add_button("Down", Vector2(635, 221), _move.bind(Vector2i.DOWN))
	_add_button("Right", Vector2(753, 221), _move.bind(Vector2i.RIGHT))
	_add_button("Inspect stall", Vector2(635, 285), _inspect)


func _draw() -> void:
	draw_rect(Rect2(Vector2.ZERO, get_viewport_rect().size), NIGHT)
	draw_rect(Rect2(32, 120, 431, 316), PANEL)
	draw_rect(Rect2(487, 120, 437, 316), PANEL)
	for row in range(ROWS):
		for column in range(COLUMNS):
			var point := cell_centre(Vector2i(column, row))
			var tile := Rect2(point - Vector2.ONE * 27.0, Vector2.ONE * 54.0)
			var tint := Color("#31596b") if (row + column) % 2 == 0 else Color("#284d61")
			draw_rect(tile, tint)
	for location: Vector2i in STALLS:
		var stall: Dictionary = STALLS[location]
		var point := cell_centre(location)
		draw_rect(Rect2(point - Vector2.ONE * 24.0, Vector2.ONE * 48.0), stall["ink"])
		draw_string(ThemeDB.fallback_font, point + Vector2(-5, 6), stall["sign"], HORIZONTAL_ALIGNMENT_LEFT, -1, 20, NIGHT)
	var counter_point := cell_centre(COUNTER)
	draw_rect(Rect2(counter_point - Vector2.ONE * 24.0, Vector2.ONE * 48.0), GOLD, false, 3.0)
	draw_string(ThemeDB.fallback_font, counter_point + Vector2(-16, 5), "Pick up", HORIZONTAL_ALIGNMENT_LEFT, -1, 10, CREAM)
	var marker := cell_centre(nia)
	draw_circle(marker, 18.0, MINT)
	draw_string(ThemeDB.fallback_font, marker + Vector2(-10, 5), "Nia", HORIZONTAL_ALIGNMENT_LEFT, -1, 14, NIGHT)
	draw_string(ThemeDB.fallback_font, Vector2(46, 468), "BRIAR LANE  •  TRAINING MAP", HORIZONTAL_ALIGNMENT_LEFT, -1, 16, GOLD)


func _refresh() -> void:
	board.text = "Briar Lane • Nia is at column %d, row %d" % [nia.x, nia.y]


func cell_centre(cell: Vector2i) -> Vector2:
	return ORIGIN + Vector2(cell) * TILE + Vector2.ONE * (TILE / 2.0)


func _add_button(caption: String, point: Vector2, action: Callable) -> void:
	var button := Button.new()
	button.text = caption
	button.position = point
	button.custom_minimum_size = Vector2(108, 50)
	button.pressed.connect(action)
	add_child(button)


func _move(step: Vector2i) -> void:
	var next := nia + step
	if not Rect2i(0, 0, COLUMNS, ROWS).has_point(next):
		board.text = "End of the lane • Nia stays put."
		return
	nia = next
	_refresh()
	queue_redraw()


func _input(event: InputEvent) -> void:
	if not event is InputEventKey or not event.pressed or event.echo:
		return
	match event.keycode:
		KEY_UP:
			_move(Vector2i.UP)
		KEY_LEFT:
			_move(Vector2i.LEFT)
		KEY_DOWN:
			_move(Vector2i.DOWN)
		KEY_RIGHT:
			_move(Vector2i.RIGHT)
		_:
			return
	get_viewport().set_input_as_handled()


func _inspect() -> void:
	if nia == COUNTER:
		board.text = "Collection counter • Orders will be handed over here."
		return
	if not STALLS.has(nia):
		board.text = "Walk onto a P, F or B square to inspect a stall."
		return
	var stall: Dictionary = STALLS[nia]
	board.text = "%s sells %s" % [stall["name"], stall["item"]]

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 ControlAttach these screen-building instructions to a Godot Control. That gives the script positions, children and input events.
  2. const NIGHT := Color("#0d2335")Use this dark ink colour behind the market. The fixed colour is also used for lettering on the mint marker and brighter signs.
  3. const PANEL := Color("#17374b")Use one lighter shade to group the map and controls against the dark background.
  4. const CREAM := Color("#fff4da")Keep the title and labels light enough to read against dark panels.
  5. const MINT := Color("#9ce4ce")Use this accent to highlight the status label and Nia’s marker.
  6. const GOLD := Color("#ffd789")Mark utility labels and the collection point with a pale yellow that stands apart from the paving.
  7. var board: LabelSave the opening label so later methods can change it without needing to create a new one.
  8. const ORIGIN := Vector2(47, 134)Set the pixel position of the upper-left corner used to lay out the market grid.
  9. const TILE := 57.0Keep neighbouring tile centres 57 pixels apart.
  10. const COLUMNS := 7Give the training lane seven columns with indices from 0 to 6.
  11. const ROWS := 5Give the training lane five rows with indices from 0 to 4.
  12. var nia := Vector2i(3, 3)Start Nia at column 3 and row 3. Store grid values instead of pixel coordinates.
  13. const STALLS := {Pair each stall position with its teaching name, sign, sold item and colour.
  14. Vector2i(1, 1): {"name": "Produce", "sign": "P", "item": "apple", "ink": Color("#f5c986")},Store the Produce stall at the upper-left marked position. Its P sign stands for produce and it supplies apple.
  15. Vector2i(3, 1): {"name": "Flowers", "sign": "F", "item": "flowers", "ink": Color("#c5b4f0")},Store the Flowers stall in the middle of the upper marked row. Its F sign identifies flowers.
  16. Vector2i(5, 1): {"name": "Bakery", "sign": "B", "item": "bread", "ink": Color("#f1a995")}Store the Bakery at the upper-right marked position. Its B sign identifies the bread stop.
  17. }Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next.
  18. const COUNTER := Vector2i(6, 3)Place the collection counter at column 6, row 3. No goods can be collected directly from this practice location.
  19. func _ready() -> void:Godot calls this once when the Market scene is ready to create the title, notice and controls.
  20. var title := Label.new()Create an unattached Label for the permanent title. It becomes visible when it is added to the scene.
  21. title.text = "Market Day London"Put the game name into the title label. The title stays fixed while smaller words underneath change.
  22. title.position = Vector2(42, 34)Leave a margin of 42 pixels from the left and 34 from the top.
  23. title.add_theme_font_size_override("font_size", 34)Set this title to a larger local font size. The rest of the labels can keep different sizes.
  24. title.modulate = CREAMSet the title’s drawing colour to the pale cream chosen above.
  25. add_child(title)Add the title to the running scene so Godot draws and maintains it.
  26. board = Label.new()Create the changing notice label and store it in the field used by other methods.
  27. board.position = Vector2(45, 88)Position the variable status words underneath the game name.
  28. board.add_theme_font_size_override("font_size", 22)Set a smaller font for status messages, which can be longer than the title.
  29. board.modulate = MINTTint the notice mint so it stands out as the changing information.
  30. add_child(board)Attach the notice beneath the title so the player sees the current message.
  31. _refresh()Write the latest cell location into the notice without creating another label.
  32. _add_button("Up", Vector2(635, 163), _move.bind(Vector2i.UP))Place the Up button above the centre of the direction button cluster. It later supplies a step of (0,-1).
  33. _add_button("Left", Vector2(517, 221), _move.bind(Vector2i.LEFT))Place the Left button to the left of Down. It supplies a step of (-1,0).
  34. _add_button("Down", Vector2(635, 221), _move.bind(Vector2i.DOWN))Place the Down button below Up. It supplies a step of (0,1).
  35. _add_button("Right", Vector2(753, 221), _move.bind(Vector2i.RIGHT))Place the Right button beside Down. It supplies a step of (1,0).
  36. _add_button("Inspect stall", Vector2(635, 285), _inspect)Give the player one button whose meaning depends on Nia’s current tile.
  37. func _draw() -> void:Paint this section when Godot refreshes the Control. Draw the background first and the frontmost details last.
  38. draw_rect(Rect2(Vector2.ZERO, get_viewport_rect().size), NIGHT)Fill the visible window so there is a consistent dark background behind the game panels.
  39. draw_rect(Rect2(32, 120, 431, 316), PANEL)Draw the dark rectangle under the tile map.
  40. draw_rect(Rect2(487, 120, 437, 316), PANEL)Draw an adjacent rectangle behind the controls and basket information.
  41. for row in range(ROWS):Visit each row index from 0 through 4.
  42. for column in range(COLUMNS):For each row, visit column indices from 0 through 6.
  43. var point := cell_centre(Vector2i(column, row))Convert this loop pair into the pixel centre for one paving square.
  44. var tile := Rect2(point - Vector2.ONE * 27.0, Vector2.ONE * 54.0)Give the tile a 54-pixel square shape. The 57-pixel spacing leaves a small border between squares.
  45. var tint := Color("#31596b") if (row + column) % 2 == 0 else Color("#284d61")Use the remainder to alternate the paving colour. Equal parities use the same ink.
  46. draw_rect(tile, tint)Place the current square using the pixel rectangle and the chosen tint.
  47. for location: Vector2i in STALLS:Look at each position key in the stall map so every stand can be drawn.
  48. var stall: Dictionary = STALLS[location]Read the nested sign data attached to this particular coordinate.
  49. var point := cell_centre(location)Fit the sign into the same square the player must visit to inspect or collect.
  50. draw_rect(Rect2(point - Vector2.ONE * 24.0, Vector2.ONE * 48.0), stall["ink"])Fill the stall’s marked square with its own brighter colour.
  51. draw_string(ThemeDB.fallback_font, point + Vector2(-5, 6), stall["sign"], HORIZONTAL_ALIGNMENT_LEFT, -1, 20, NIGHT)Draw the stall letter on its square so people can tell the stalls apart without using colour alone.
  52. var counter_point := cell_centre(COUNTER)Convert the separate collection counter coordinate into a drawn point.
  53. draw_rect(Rect2(counter_point - Vector2.ONE * 24.0, Vector2.ONE * 48.0), GOLD, false, 3.0)Draw an outline around the future collection square, distinct from the filled stall squares.
  54. draw_string(ThemeDB.fallback_font, counter_point + Vector2(-16, 5), "Pick up", HORIZONTAL_ALIGNMENT_LEFT, -1, 10, CREAM)Add the compact Pick up sign inside the counter marker.
  55. var marker := cell_centre(nia)Convert the saved grid cell to the exact point where Nia’s visual marker belongs.
  56. draw_circle(marker, 18.0, MINT)Paint the small mint circle over the map paving or stall square.
  57. draw_string(ThemeDB.fallback_font, marker + Vector2(-10, 5), "Nia", HORIZONTAL_ALIGNMENT_LEFT, -1, 14, NIGHT)Put Nia’s name inside the circle so the marker does not rely on its colour alone.
  58. draw_string(ThemeDB.fallback_font, Vector2(46, 468), "BRIAR LANE • TRAINING MAP", HORIZONTAL_ALIGNMENT_LEFT, -1, 16, GOLD)Label the map with a quiet footer set apart from the playable square area.
  59. func _refresh() -> void:Update the status line from Nia’s currently stored map cell.
  60. board.text = "Briar Lane • Nia is at column %d, row %d" % [nia.x, nia.y]Read x as the column and y as the row, then show both values.
  61. func cell_centre(cell: Vector2i) -> Vector2:Convert a whole-number tile position into a pixel point for drawing.
  62. return ORIGIN + Vector2(cell) * TILE + Vector2.ONE * (TILE / 2.0)Multiply the tile coordinate by the 57-pixel spacing, start at ORIGIN and move another half tile to reach its centre.
  63. func _add_button(caption: String, point: Vector2, action: Callable) -> void:Receive a caption, screen position and callable to reuse the same control construction for different actions.
  64. var button := Button.new()Create a Godot button that the player will be able to activate after it is attached.
  65. button.text = captionUse the words provided by the caller to explain what this button does.
  66. button.position = pointPlace the button using the point sent by _ready.
  67. button.custom_minimum_size = Vector2(108, 50)Reserve a large area for the button so its label and touch target are easier to distinguish.
  68. button.pressed.connect(action)Tell Godot which stored action should run when the button emits its pressed signal.
  69. add_child(button)Put the button into the active scene so it can display and receive input.
  70. func _move(step: Vector2i) -> void:Receive one direction as a whole-number change to Nia’s column and row.
  71. var next := nia + stepCalculate the proposed grid cell. Nia is still at the saved cell while the game checks the proposal.
  72. if not Rect2i(0, 0, COLUMNS, ROWS).has_point(next):Ask whether the proposal is outside this zero-based rectangle. Points on the far right or bottom border are outside.
  73. board.text = "End of the lane • Nia stays put."Explain the rejected press while leaving the saved grid position alone.
  74. returnFinishes the function right here. Any later instructions in this function are skipped.
  75. nia = nextAccept the new position only after the bounds branch has been passed.
  76. _refresh()Write the latest cell location into the notice without creating another label.
  77. queue_redraw()Request a repaint so Nia’s marker now appears at the new cell.
  78. func _input(event: InputEvent) -> void:Receive a Godot input event. Keyboard events and button signals meet at the same move method.
  79. if not event is InputEventKey or not event.pressed or event.echo:Ignore non-key events, key releases and automatic repeats. Only a fresh key press goes further.
  80. returnFinishes the function right here. Any later instructions in this function are skipped.
  81. match event.keycode:Compare the key that arrived with the four arrow values.
  82. KEY_UP:Match the actual Up arrow, then ask for a one-tile move toward the top of the screen.
  83. _move(Vector2i.UP)Calls _move with the values in its parentheses.
  84. KEY_LEFT:Match the actual Left arrow, then ask for a one-column move to the left.
  85. _move(Vector2i.LEFT)Calls _move with the values in its parentheses.
  86. KEY_DOWN:Match the actual Down arrow, then ask for a one-tile move toward the bottom of the screen.
  87. _move(Vector2i.DOWN)Calls _move with the values in its parentheses.
  88. KEY_RIGHT:Match the actual Right arrow, then ask for a one-column move to the right.
  89. _move(Vector2i.RIGHT)Calls _move with the values in its parentheses.
  90. _:Recognise every other key as unrelated to market movement.
  91. returnFinishes the function right here. Any later instructions in this function are skipped.
  92. get_viewport().set_input_as_handled()Tell Godot this arrow was used for movement so another handler can avoid acting on it.
  93. func _inspect() -> void:Read the current market tile and produce a matching message without changing Nia’s stored location.
  94. if nia == COUNTER:Recognise a visit to the separate collection counter before the stall lookup.
  95. board.text = "Collection counter • Orders will be handed over here."Explain where orders will be handed over in the next unit.
  96. returnFinishes the function right here. Any later instructions in this function are skipped.
  97. if not STALLS.has(nia):Ask whether the current cell lacks a stall entry before trying to read that entry.
  98. board.text = "Walk onto a P, F or B square to inspect a stall."Guide Nia to the three labelled stall tiles. The marker stays where it was.
  99. returnFinishes the function right here. Any later instructions in this function are skipped.
  100. var stall: Dictionary = STALLS[nia]Load the record at Nia’s current position. The earlier membership check means there is a matching key.
  101. board.text = "%s sells %s" % [stall["name"], stall["item"]]Show the stall name and its item by reading the two matching fields from the current record.

Find and fix a bug

If the sign never reports a stall, use the coordinate caption to check you are standing on its square. Confirm its key is a Vector2i like nia, not a string such as "3,1".

Try a change

Ages 10–12 can visit every stall and write down what it offers. Ages 13–16 can update the training map so one stall has a different item, letter and location.