London Market 32 · Glide Nia between tiles

Market Day London · Lesson 32 of 50 · Intermediate to advanced · About 35 min

Nia can zip down the lane when a customer calls. Keep the game state accurate even when the marker is halfway through a slide.

Watercolour of a dry London market lane lined with bread, produce and flower stalls.
What you’ll make Animate a market walk while the rules still use exact grid coordinates.

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

Receive. Read the requested direction. → Check. Keep the candidate in range. → Commit. Record the destination index. → Animate. Slide there without changing rules.
Receive → Check → Commit → Animate
Godot checkpoint window for Glide Nia between tiles
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson Vector2i, clampi, Tween, kill, tween_property.

  1. 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.

  2. Convert tile number to screen position

    Find point_for and temporarily change 23 to 14 in the first coordinate. Run and compare Nia on tile 0 and tile 2.

    return ORIGIN + Vector2(tile * SPACING + 23, 23)

    Why it works tile times SPACING finds the tile's start. The offset shifts the label inside the tile, independent of the grid's logic.

  3. Protect both edges

    Press Move left at tile 0. Read clampi in move. Run to the far right and try one extra press.

    var candidate := clampi(position_index + direction, 0, CELLS - 1)

    Why it works The minimum zero and the maximum CELLS minus one keep Nia on the seven painted tiles.

  4. Commit the destination before animation

    Press Move right once and read the status immediately. Look for the assignment before create_tween.

    position_index = candidate

    Why it works The position_index is the real destination. Other rules can read it while the marker is catching up.

  5. Avoid two animations fighting

    Change 0.24 to 0.7 at tween_property. Press Move right twice before the first slide ends, then restore 0.24.

    motion.kill()

    Why it works A new command kills a running Tween so only one animation writes the marker position.

  6. Reset while moving

    With a longer duration, press Reset walk before a slide finishes. Follow marker.position inside reset_path.

    marker.position = point_for(position_index)

    Why it works The reset cancels motion, restores tile zero and sets the visual position directly.

Watch the values change

  1. Move left at 0 The candidate clamps to zero.
    What you see. There is no slide.
  2. Move right twice position_index commits to 2.
    What you see. Nia glides to tile 2.
  3. Reset during a slide The active Tween is killed.
    What you see. Nia returns to tile 0.

Predict before you run If you press right twice quickly, does the second move start from logical tile 0 or tile 1?

Reveal the answer

Tile 1. The first command commits its destination before its picture slides.

Complete checkpoint

Nia glides between seven well-labelled tiles. Reset stops a slide and brings her back to the collection counter.

main.gd

extends Control

const CELLS := 7
const ORIGIN := Vector2(117, 211)
const SPACING := 103.0
var position_index := 0
var marker: Label
var motion: Tween
var status: Label

func _ready() -> void:
	heading("Glide Nia between tiles", "MOVEMENT")
	status = label("Tile 0 • collection counter", Vector2(44, 126), 21)
	marker = label("NIA", point_for(0), 21, GOLD)
	button("Move left", Vector2(44, 428), move.bind(-1))
	button("Move right", Vector2(222, 428), move.bind(1))
	button("Reset walk", Vector2(400, 428), reset_path)

func point_for(tile: int) -> Vector2:
	return ORIGIN + Vector2(tile * SPACING + 23, 23)

func move(direction: int) -> void:
	var candidate := clampi(position_index + direction, 0, CELLS - 1)
	if candidate == position_index:
		return
	position_index = candidate
	if motion and motion.is_running():
		motion.kill()
	motion = create_tween()
	motion.set_trans(Tween.TRANS_QUAD).set_ease(Tween.EASE_OUT)
	motion.tween_property(marker, "position", point_for(position_index), 0.24)
	status.text = "Tile %d • destination committed" % position_index

func reset_path() -> void:
	if motion:
		motion.kill()
	position_index = 0
	marker.position = point_for(position_index)
	status.text = "Tile 0 • collection counter"

func _draw() -> void:
	base()
	for tile in range(CELLS):
		var at := ORIGIN + Vector2(tile * SPACING, 0)
		draw_rect(Rect2(at, Vector2(88, 68)), Color("#347165") if tile == 0 else Color("#3a5266"))
		draw_string(ThemeDB.fallback_font, at + Vector2(25, 99), "C" if tile == 0 else str(tile), HORIZONTAL_ALIGNMENT_LEFT, -1, 20, PAPER)

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
  1. extends ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules.
  2. const CELLS := 7Creates a constant named CELLS. The expression uses the whole number 7. Godot infers the type from the first value.
  3. const ORIGIN := Vector2(117, 211)Creates a constant named ORIGIN. The expression packs x and y into one value. Godot infers the type from the first value.
  4. const SPACING := 103.0Creates a constant named SPACING. The expression reads 103.0. Godot infers the type from the first value.
  5. var position_index := 0position_index is the last accepted tile, even while the NIA label is halfway across the screen.
  6. var marker: LabelDeclares marker for later use. A colon followed by a type says which kind of value belongs here.
  7. var motion: TweenDeclares motion for later use. A colon followed by a type says which kind of value belongs here.
  8. var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here.
  9. 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.
  10. heading("Glide Nia between tiles", "MOVEMENT")Calls heading with the values in its parentheses.
  11. status = label("Tile 0 • collection counter", Vector2(44, 126), 21)Stores the new value in status. The expression calls label with the values in its parentheses.
  12. marker = label("NIA", point_for(0), 21, GOLD)Stores the new value in marker. The expression calls label with the values in its parentheses.
  13. button("Move left", Vector2(44, 428), move.bind(-1))bind(-1) gives the shared move function its leftward direction. Godot runs it when the button is pressed.
  14. button("Move right", Vector2(222, 428), move.bind(1))Calls button with the values in its parentheses.
  15. button("Reset walk", Vector2(400, 428), reset_path)Calls button with the values in its parentheses.
  16. func point_for(tile: int) -> Vector2:Defines point for, 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.
  17. return ORIGIN + Vector2(tile * SPACING + 23, 23)Multiply the tile number by SPACING, then add the small offsets that place the marker inside its tile.
  18. func move(direction: int) -> void:Defines move, 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.
  19. var candidate := clampi(position_index + direction, 0, CELLS - 1)clampi() keeps a candidate index between zero and six, including both bounds.
  20. if candidate == position_index:At a fence, a clamped result can be the current tile. Return early to keep the active state and animation unchanged.
  21. returnFinishes the function right here. Any later instructions in this function are skipped.
  22. position_index = candidateStores the new value in position_index. The expression reads candidate.
  23. if motion and motion.is_running():The first movement has no Tween yet. The and operator checks motion before calling its method.
  24. motion.kill()Stop an unfinished animation so two tweens cannot fight over the same property.
  25. motion = create_tween()Create a fresh Tween for the new destination. It begins from the marker’s current screen position.
  26. motion.set_trans(Tween.TRANS_QUAD).set_ease(Tween.EASE_OUT)The quadratic easing slows the marker near its destination. It affects the drawing only.
  27. motion.tween_property(marker, "position", point_for(position_index), 0.24)Tween the position property to the destination over 0.24 seconds. The property name is a string.
  28. status.text = "Tile %d • destination committed" % position_indexUpdates status.text, which controls the words a player sees. The expression uses the text inside the quotes.
  29. func reset_path() -> void:Defines reset path, 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.
  30. if motion:Checks motion. The indented instructions below run only if this test passes.
  31. motion.kill()Calls motion.kill with the values in its parentheses.
  32. position_index = 0Stores the new value in position_index. The expression uses the whole number 0.
  33. marker.position = point_for(position_index)A reset sets the marker directly. Try resetting during a move to see why the earlier tween has to stop first.
  34. status.text = "Tile 0 • collection counter"Updates status.text, which controls the words a player sees. The expression uses the text inside the quotes.
  35. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  36. base()Calls base with the values in its parentheses.
  37. for tile in range(CELLS):Repeats the indented instructions for each value in range(CELLS).
  38. var at := ORIGIN + Vector2(tile * SPACING, 0)Creates a variable named at. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value.
  39. draw_rect(Rect2(at, Vector2(88, 68)), Color("#347165") if tile == 0 else Color("#3a5266"))Paints a rectangle at the given position and size.
  40. draw_string(ThemeDB.fallback_font, at + Vector2(25, 99), "C" if tile == 0 else str(tile), HORIZONTAL_ALIGNMENT_LEFT, -1, 20, PAPER)The collection counter uses C. All other tile numbers are converted into strings for draw_string().
  41. 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.
  42. 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.
  43. 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.
  44. 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.
  45. 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.
  46. label("MARKET DAY LONDON / " + topic, Vector2(44, 36), 15, GOLD)Calls label with the values in its parentheses.
  47. label(caption, Vector2(44, 66), 31)Calls label with the values in its parentheses.
  48. 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.
  49. var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value.
  50. item.text = wordsUpdates item.text, which controls the words a player sees. The expression reads words.
  51. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  52. item.add_theme_font_size_override("font_size", pixels)Calls item.add_theme_font_size_override with the values in its parentheses.
  53. item.add_theme_color_override("font_color", ink)Calls item.add_theme_color_override with the values in its parentheses.
  54. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  55. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  56. 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.
  57. var control := Button.new()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value.
  58. control.text = wordsUpdates control.text, which controls the words a player sees. The expression reads words.
  59. control.position = atUpdates control.position, which controls the node's local x and y location. The expression reads at.
  60. 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.
  61. control.pressed.connect(action)Links a signal to the function that should run when the event happens.
  62. add_child(control)Adds the new node to the scene tree so that Godot can run and draw it.
  63. return controlFinishes the function and hands its result back to the caller. The expression reads control.
  64. 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.
  65. draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))Paints a rectangle at the given position and size.
  66. 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 the marker jumps back to an old destination after reset, cancel the running Tween before assigning marker.position.

Try a change

Ages 10–12 can choose a different movement duration. Ages 13–16 can add a motion setting whose instant mode skips the Tween.