Cabin Crew Express · Lesson 33 of 50 · Intermediate to advanced · About 38 min
Passengers are waiting for the right items. Use a short sound to tell Ari what happened while keeping a clear text result.

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/32 when the lesson builds on prior work, or copy the checkpoints/33 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 PackedByteArray, AudioStreamWAV, AudioStreamPlayer, sin, toggle_mode.
-
Open the editable checkpoint
Start Godot 4.7.2, choose Import in Project Manager and select the project.godot file for this lesson. Open main.gd in FileSystem, then press F5. Stop the game with F8 before editing.
Why it works This folder is its own project with a Control scene and an attached script. You can run it without files from any other lesson.
-
Choose two pitches
Press both action buttons and listen, then inspect the lines with 660.0 and 330.0. Try changing the second number for one run.
success = make_tone(660.0)Why it works Pitch is how quickly the wave cycles. Evenly spaced higher cycles sound higher.
-
Budget bytes for the sample
Find total, then follow pcm.resize. Work out how many sample slots a 0.20 second tone uses.
pcm.resize(total * 2)Why it works RATE counts samples per second. A signed 16-bit sample holds two bytes, so this array needs double the sample count.
-
Fade the edges
Read the two sides of minf. Compare one near time zero with one near the clip end.
var fade := minf(time / 0.016, (1.0 - time / LENGTH) * 4.0)Why it works The smaller ramp is used so the sound begins and ends near zero. This cuts clicks.
-
Pair picture and sound
Follow cue. Press one action while Audio is off, then another while it is on.
status.text = "✓ Delivered to the correct seat." if correct else "↻ The seat is still waiting for its item."Why it works The status changes before the sound check. A muted run still explains why a delivery worked or failed.
-
Stop a playing tone when muted
Toggle Audio on and off a few times. Inspect the call under if quiet in toggle_mute.
player.stop()Why it works Stopping the player removes any remaining sample from the previous action immediately.
Watch the values change
-
Press Correct delivery correct is true and audio is on.
What you see. A check and message appear, and the higher tone plays. -
Press Try another item The other feedback message and clip are chosen.
What you see. The seat stays waiting and the lower tone plays. -
Mute then deliver cue updates status before returning early.
What you see. The confirmation still shows without sound.
Predict before you run What survives if you turn the speaker off and make a delivery?
Reveal the answer
The matching symbol and message still appear. The return under if quiet skips sound only.
Complete checkpoint
Correct and incorrect actions have distinct captions and pitches. Muting leaves the outcomes visible.
main.gd
extends Control
const RATE := 22050
const LENGTH := 0.20
var player: AudioStreamPlayer
var success: AudioStreamWAV
var retry: AudioStreamWAV
var quiet := false
var status: Label
var mute_button: Button
func _ready() -> void:
heading("Give cabin service a feedback chime", "AUDIO")
status = label("Choose an action to preview its feedback.", Vector2(48, 159))
player = AudioStreamPlayer.new()
add_child(player)
player.volume_db = -12.0
success = make_tone(660.0)
retry = make_tone(330.0)
button("✓ Correct delivery", Vector2(48, 255), cue.bind(true), 195)
button("↻ Try another item", Vector2(258, 255), cue.bind(false), 210)
mute_button = button("Audio: on", Vector2(48, 343), toggle_mute, 155)
mute_button.toggle_mode = true
func make_tone(pitch: float) -> AudioStreamWAV:
var total := int(RATE * LENGTH)
var pcm := PackedByteArray()
pcm.resize(total * 2)
for index in range(total):
var time := float(index) / RATE
var fade := minf(time / 0.016, (1.0 - time / LENGTH) * 4.0)
var sample := int(sin(TAU * pitch * time) * fade * 10000.0)
pcm.encode_s16(index * 2, sample)
var tone := AudioStreamWAV.new()
tone.format = AudioStreamWAV.FORMAT_16_BITS
tone.mix_rate = RATE
tone.data = pcm
return tone
func cue(correct: bool) -> void:
status.text = "✓ Delivered to the correct seat." if correct else "↻ The seat is still waiting for its item."
if quiet:
return
player.stream = success if correct else retry
player.play()
func toggle_mute() -> void:
quiet = mute_button.button_pressed
mute_button.text = "Audio: off" if quiet else "Audio: on"
if quiet:
player.stop()
func heading(caption: String, topic: String) -> void:
label("CABIN CREW EXPRESS / " + topic, Vector2(48, 41), 15, Color("#ffca7b"))
label(caption, Vector2(48, 73), 27)
func label(text: String, at: Vector2, pixels: int = 20, ink: Color = Color("#f7f4e9")) -> Label:
var item := Label.new()
item.text = text
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(text: String, at: Vector2, action: Callable, width: float = 155.0) -> Button:
var item := Button.new()
item.text = text
item.position = at
item.custom_minimum_size = Vector2(width, 47)
item.pressed.connect(action)
add_child(item)
return item
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))
draw_rect(Rect2(Vector2(28, 25), Vector2(904, 483)), Color("#18364b"))
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 RATE := 22050Count 22,050 short samples each second. The stream must use this same rate. -
const LENGTH := 0.20A fraction of a second keeps the sound short and out of the player’s way. -
var player: AudioStreamPlayerDeclares player for later use. A colon followed by a type says which kind of value belongs here. -
var success: AudioStreamWAVDeclares success for later use. A colon followed by a type says which kind of value belongs here. -
var retry: AudioStreamWAVDeclares retry for later use. A colon followed by a type says which kind of value belongs here. -
var quiet := falseCreates a variable named quiet. The expression uses the yes-or-no value false. Godot infers the type from the first value. -
var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here. -
var mute_button: ButtonDeclares mute_button 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("Give cabin service a feedback chime", "AUDIO")Calls heading with the values in its parentheses. -
status = label("Choose an action to preview its feedback.", Vector2(48, 159))Stores the new value in status. The expression calls label with the values in its parentheses. -
player = AudioStreamPlayer.new()Stores the new value in player. The expression creates a new AudioStreamPlayer object. -
add_child(player)Adds the new node to the scene tree so that Godot can run and draw it. -
player.volume_db = -12.0Start playback below its full digital volume. -
success = make_tone(660.0)The higher tone is the cue for a correct delivery. -
retry = make_tone(330.0)The lower tone asks the player to try a different item. -
button("✓ Correct delivery", Vector2(48, 255), cue.bind(true), 195)Calls button with the values in its parentheses. -
button("↻ Try another item", Vector2(258, 255), cue.bind(false), 210)Calls button with the values in its parentheses. -
mute_button = button("Audio: on", Vector2(48, 343), toggle_mute, 155)Stores the new value in mute_button. The expression calls button with the values in its parentheses. -
mute_button.toggle_mode = trueKeep the button pressed across taps until the next tap toggles the mute state. -
func make_tone(pitch: float) -> AudioStreamWAV:Defines make tone, 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 total := int(RATE * LENGTH)Multiply duration by the sample rate, then convert to a whole number of samples. -
var pcm := PackedByteArray()Creates a variable named pcm. The expression calls PackedByteArray with the values in its parentheses. Godot infers the type from the first value. -
pcm.resize(total * 2)Reserve exactly two bytes per signed 16-bit sample. -
for index in range(total):Repeats the indented instructions for each value in range(total). -
var time := float(index) / RATEConvert this sample’s index into elapsed time in seconds. -
var fade := minf(time / 0.016, (1.0 - time / LENGTH) * 4.0)Use the quieter of the opening and closing envelopes so the ends do not start or stop abruptly. -
var sample := int(sin(TAU * pitch * time) * fade * 10000.0)Turn a point of the sine wave into the signed whole number written to the sound file. -
pcm.encode_s16(index * 2, sample)Write one little-endian signed 16-bit sample into its two-byte space in the array. -
var tone := AudioStreamWAV.new()Creates a variable named tone. The expression creates a new AudioStreamWAV object. Godot infers the type from the first value. -
tone.format = AudioStreamWAV.FORMAT_16_BITSTell Godot to interpret every pair of bytes as one signed 16-bit sample. -
tone.mix_rate = RATEStores the new value in tone.mix_rate. The expression reads RATE. -
tone.data = pcmAttach the prepared waveform to the stream before returning it. -
return toneFinishes the function and hands its result back to the caller. The expression reads tone. -
func cue(correct: bool) -> void:Defines cue, 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. -
status.text = "✓ Delivered to the correct seat." if correct else "↻ The seat is still waiting for its item."Updates status.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
if quiet:Skip the sound part of an action while leaving its written result available. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
player.stream = success if correct else retrySelect the higher or lower note based on the result passed into cue. -
player.play()Starts this sound or animation. -
func toggle_mute() -> void:Defines toggle mute, 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. -
quiet = mute_button.button_pressedRead the toggle’s current on/off state instead of storing a second independent counter. -
mute_button.text = "Audio: off" if quiet else "Audio: on"Updates mute_button.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
if quiet:Checks quiet. The indented instructions below run only if this test passes. -
player.stop()Stops this sound, timer or animation. -
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("CABIN CREW EXPRESS / " + topic, Vector2(48, 41), 15, Color("#ffca7b"))Calls label with the values in its parentheses. -
label(caption, Vector2(48, 73), 27)Calls label with the values in its parentheses. -
func label(text: String, at: Vector2, pixels: int = 20, ink: Color = Color("#f7f4e9")) -> 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 = textUpdates item.text, which controls the words a player sees. The expression reads text. -
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(text: String, at: Vector2, action: Callable, width: float = 155.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 item := Button.new()Creates a variable named item. The expression creates a new Button object. Godot infers the type from the first value. -
item.text = textUpdates item.text, which controls the words a player sees. The expression reads text. -
item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at. -
item.custom_minimum_size = Vector2(width, 47)Updates item.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value. -
item.pressed.connect(action)Links a signal to the function that should run when the event happens. -
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 _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state. -
draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))Paints a rectangle at the given position and size. -
draw_rect(Rect2(Vector2(28, 25), Vector2(904, 483)), Color("#18364b"))Paints a rectangle at the given position and size.
Find and fix a bug
If both sounds are silent with Audio on, check the selected audio output and confirm player.stream is assigned before player.play().
Try a change
Ages 10–12 can choose their own two pitches. Ages 13–16 can change the fade curve and add a slider with a minimum comfortable level.