Cabin Crew Express · Lesson 07 of 50 · Beginner · About 33 min
Training starts in row 2. Ari needs one visible forward control and one visible back control.

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/06 when the lesson builds on prior work, or copy the checkpoints/07 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 Button.new, pressed signal, connect, Callable, bind, clampi, queue_redraw.
-
Place movement in one function
Add _move underneath the drawing and label helpers. Read its step parameter as a signed change in row.
func _move(step: int) -> void: crew_row = clampi(crew_row + step, 0, LAST_ROW) _refresh_board() queue_redraw()Why it works You can send either direction to the same function. It updates the number, refreshes the sign and requests a fresh draw.
-
Design a reusable button builder
Add _add_button. Read caption, point and action as the three values each button needs.
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(152, 54) button.pressed.connect(action) add_child(button)Why it works The builder receives a piece of text, a pixel position and a callable that Godot can run later.
-
Connect the forward and back signals
Below _refresh_board in _ready, add the two button calls. Make the Forward bind argument negative.
_add_button("Forward", Vector2(510, 164), _move.bind(-1)) _add_button("Back", Vector2(680, 164), _move.bind(1))Why it works bind stores the step to use when the button is actually pressed. Forward means moving towards the galley, so its row number decreases.
-
Read the three move phases
Find the clampi line. Then find _refresh_board and queue_redraw inside _move.
crew_row = clampi(crew_row + step, 0, LAST_ROW)Why it works clampi keeps the candidate inside 0 to LAST_ROW. The label changes immediately and the draw request lets Godot show the new tile.
-
Check a route by tapping
Run the project. Tap Forward, Back, Back. Say the row before and after each tap.
Why it works Each button uses the same function and a different signed step. Movement does not affect any seat address.
-
Test the top boundary
Keep tapping Forward until Ari reaches the galley and tap once more.
Why it works clampi produces 0 again for a value below 0. The marker remains in the galley.
Watch the values change
-
Tap Forward 2 + (-1) gives 1.
What you see. Ari moves to row 1. -
Tap Back 1 + 1 gives 2.
What you see. Ari returns to row 2. -
Tap Back again 2 + 1 gives 3.
What you see. The sign and marker show row 3.
Predict before you run What happens if the Forward button binds 1 too?
Reveal the answer
Both buttons ask _move to add one, so both move toward the back until row 6.
Complete checkpoint
Ari moves one row for each valid tap and stops at either end of the cabin.
main.gd
extends Control
const GRID_ORIGIN := Vector2(64, 112)
const TILE := 54.0
const AISLE := 2
const LAST_ROW := 6
const NAVY := Color("#0a2138")
const AISLE_BLUE := Color("#4c8099")
const TEAL := Color("#8ce4ce")
var crew_row: int = 2
var board: Label
const SEAT_LETTERS: Array[String] = ["A", "B", "", "C", "D"]
func _ready() -> void:
board = Label.new()
board.position = Vector2(40, 48)
board.add_theme_font_size_override("font_size", 30)
add_child(board)
_refresh_board()
_add_button("Forward", Vector2(510, 164), _move.bind(-1))
_add_button("Back", Vector2(680, 164), _move.bind(1))
func _refresh_board() -> void:
var place := "Galley" if crew_row == 0 else "Row %d" % crew_row
board.text = "Cabin Crew Express | Ari at %s" % place
func cell_centre(cell: Vector2i) -> Vector2:
return GRID_ORIGIN + Vector2(cell) * TILE + Vector2.ONE * (TILE / 2.0)
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, get_viewport_rect().size), NAVY)
for row in range(LAST_ROW + 1):
for column in range(5):
if row == 0 and column != AISLE:
continue
var point := cell_centre(Vector2i(column, row))
var tile := Rect2(point - Vector2.ONE * 25.0, Vector2.ONE * 50.0)
var paint := AISLE_BLUE if column == AISLE else Color("#193f58")
draw_rect(tile, paint)
if column != AISLE:
var seat := seat_address(row, column)
draw_string(ThemeDB.fallback_font, point + Vector2(-10, 5), seat, HORIZONTAL_ALIGNMENT_LEFT, -1, 15, Color.WHITE)
var point := cell_centre(Vector2i(AISLE, crew_row))
draw_circle(point, 20.0, TEAL)
draw_string(ThemeDB.fallback_font, point + Vector2(-10, 5), "Ari", HORIZONTAL_ALIGNMENT_LEFT, -1, 15, NAVY)
func seat_address(row: int, column: int) -> String:
return "%d%s" % [row, SEAT_LETTERS[column]]
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(152, 54)
button.pressed.connect(action)
add_child(button)
func _move(step: int) -> void:
crew_row = clampi(crew_row + step, 0, LAST_ROW)
_refresh_board()
queue_redraw()
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 ControlAttach this script to a Control node so it can use labels and on-screen buttons. -
const GRID_ORIGIN := Vector2(64, 112)Use a fixed drawing origin so the aisle, seats and trainee share one coordinate system. -
const TILE := 54.0Centre neighbouring cells 54 pixels apart. The rectangles themselves are a little smaller. -
const AISLE := 2Column 2 is the third of the five visible columns. Ari stays within this centre aisle. -
const LAST_ROW := 6The last passenger row is 6. Row 0 is reserved for the galley. -
const NAVY := Color("#0a2138")Use this dark blue as the cabin background, and behind some text on the pale marker. -
const AISLE_BLUE := Color("#4c8099")Column 2 is the third of the five visible columns. Ari stays within this centre aisle. -
const TEAL := Color("#8ce4ce")Use the mint accent for Ari and for the small request indicators. -
var crew_row: int = 2This single integer identifies Ari’s position. The sign, marker and seat lookup all depend on it. -
var board: LabelKeep the same briefing Label available both to the setup and later callback functions. -
const SEAT_LETTERS: Array[String] = ["A", "B", "", "C", "D"]Keep one list entry per visual column. Its empty third entry lines up with the aisle. -
func _ready() -> void:Godot calls this once when the starting cabin Control is ready to create its children. -
board = Label.new()Create the actual Label and store its reference in the script-wide board variable. -
board.position = Vector2(40, 48)Give the sign a small upper-left margin on the 960 × 540 practice screen. -
board.add_theme_font_size_override("font_size", 30)Make the status easy to read without changing every Label in the project. -
add_child(board)Attach the briefing to this Control so Godot can draw it in the active scene. -
_refresh_board()Refresh what the player sees using the current cabin row. -
_add_button("Forward", Vector2(510, 164), _move.bind(-1))Create the forward button and remember a step of −1, because the front is row 0. -
_add_button("Back", Vector2(680, 164), _move.bind(1))Create the back button and remember a step of +1 towards the last cabin row. -
func _refresh_board() -> void:Work out what to call the current row before preparing the latest sign. -
var place := "Galley" if crew_row == 0 else "Row %d" % crew_rowShow Galley for row zero. For a passenger row, insert its number after Row. -
board.text = "Cabin Crew Express | Ari at %s" % placeDisplay the named place using the same row value that controls the marker. -
func cell_centre(cell: Vector2i) -> Vector2:Convert a whole-number tile address into the pixel centre used to draw a tile or Ari’s marker. -
return GRID_ORIGIN + Vector2(cell) * TILE + Vector2.ONE * (TILE / 2.0)Add a scaled address and a half-tile shift to the grid origin. The returned Vector2 is measured in pixels. -
func _draw() -> void:Godot calls here when the screen needs repainting. Draw the background first, then the cells and marker. -
draw_rect(Rect2(Vector2.ZERO, get_viewport_rect().size), NAVY)Paint over the whole viewport to provide the dark surface behind the cabin map. -
for row in range(LAST_ROW + 1):Visit row zero and the six passenger rows. range stops before LAST_ROW + 1. -
for column in range(5):For each row, inspect columns zero through four before advancing to the next row. -
if row == 0 and column != AISLE:At the galley, skip seats on both sides but keep the aisle square. -
continueSkip the rest of this inner-loop pass. Godot continues with the next column. -
var point := cell_centre(Vector2i(column, row))Calculate this one tile’s centre using the column as x and row as y. -
var tile := Rect2(point - Vector2.ONE * 25.0, Vector2.ONE * 50.0)Use a 50 × 50 rectangle centred on the point. Four pixels separate it from its neighbours. -
var paint := AISLE_BLUE if column == AISLE else Color("#193f58")Choose the lighter paint for column 2 and dark paint for any seat column. -
draw_rect(tile, paint)Paint the current tile. Its rectangle and colour were calculated on the two lines above. -
if column != AISLE:Only passenger seat tiles need an address label. -
var seat := seat_address(row, column)Ask the address helper to combine the current row number with this particular seat letter. -
draw_string(ThemeDB.fallback_font, point + Vector2(-10, 5), seat, HORIZONTAL_ALIGNMENT_LEFT, -1, 15, Color.WHITE)Draw the seat address just left of the tile centre. The default Godot font needs no extra files. -
var point := cell_centre(Vector2i(AISLE, crew_row))The trainee stays in the aisle column. crew_row decides how far down the aisle to draw Ari. -
draw_circle(point, 20.0, TEAL)Place Ari’s teal circle over the tile rectangles. -
draw_string(ThemeDB.fallback_font, point + Vector2(-10, 5), "Ari", HORIZONTAL_ALIGNMENT_LEFT, -1, 15, NAVY)Identify the trainee on the teal circle, so the marker does not rely on colour alone. -
func seat_address(row: int, column: int) -> String:Receive the current human-facing row number and the column index of a seat. -
return "%d%s" % [row, SEAT_LETTERS[column]]Look up the letter by column and join it directly after the row number, producing labels such as 3C. -
func _add_button(caption: String, point: Vector2, action: Callable) -> void:Make the four seat buttons and movement buttons with one builder. Their captions, positions and press actions differ. -
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 = captionUpdates button.text, which controls the words a player sees. The expression reads caption. -
button.position = pointUpdates button.position, which controls the node's local x and y location. The expression reads point. -
button.custom_minimum_size = Vector2(152, 54)Give the clickable control room around its caption, for easier mouse or touch use in the prototype. -
button.pressed.connect(action)Tell Godot to call the given action only after that button emits its pressed signal. -
add_child(button)Adds the new node to the scene tree so that Godot can run and draw it. -
func _move(step: int) -> void:Apply a signed step to the aisle row. Both touchscreen buttons and keyboard arrows use this function. -
crew_row = clampi(crew_row + step, 0, LAST_ROW)Calculate the requested row and keep the stored number inside the inclusive range from zero to six. -
_refresh_board()Refresh what the player sees using the current cabin row. -
queue_redraw()Request a new drawing pass so Ari’s marker moves to the same row now shown in the sign.
Find and fix a bug
If the buttons show but do nothing, find pressed.connect(action), confirm it runs in _add_button and leave bind(-1) in the Forward call.
Try a change
Age 10–12: explain the sign of each step. Age 13–16: add a Return to galley button connected to a function that sets crew_row to zero and redraws.