Midnight Deck Dash · Lesson 33 of 50 · Intermediate to advanced · About 36 min
A bright bell tells the crew a parcel arrived. A passenger can mute the bell and still read the outcome.

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 AudioStreamPlayer, AudioStreamWAV, PackedByteArray, sine wave, CheckButton.toggled.
-
Open the editable checkpoint
In Godot Project Manager, choose Import, select this lesson's project.godot and click Import & Edit. Open main.gd from FileSystem and press F5 to see the finished checkpoint. Press F8 before trying a change.
Why it works The lesson scene already has a Control root with this script attached.
-
Create one sound player
Find AudioStreamPlayer.new in _ready. Make sure add_child comes before players can use the control. Test with your device volume low.
player = AudioStreamPlayer.new()Why it works The node belongs to the scene and plays whatever stream you give it.
-
Bind a message and a pitch
Compare the two button calls. Find 720.0 and 240.0, measured in hertz. Press both buttons.
button("Deliver parcel", 53, 168, feedback.bind("Delivery confirmed", 720.0))Why it works bind saves the message and the pitch until the signal calls feedback. One shared function handles both actions.
-
Fill the audio buffer
In tone read samples.resize and the frame loop. Look for frame * 2 in encode_s16.
samples.encode_s16(frame * 2, amplitude)Why it works Each signed 16-bit mono sample uses two bytes. The sine generates a wave, and the envelope eases the sound in and out.
-
Play the generated sound
Trace return stream back to the feedback function. Identify which values set format and playback rate.
player.stream = tone(pitch)Why it works WAV describes how Godot reads the raw samples. The result is passed to the sound node and play starts it.
-
Keep captions visible when muted
Turn Mute sound on, then press both action buttons again. Compare captions to the earlier test.
if mute.button_pressed:Why it works caption.text is set before the mute guard returns. Muting also stops any active sound.
Watch the values change
-
Deliver parcel feedback shows a caption and plays 720 Hz.
What you see. The successful delivery is visible and audible. -
Try blocked path feedback selects a new caption and 240 Hz.
What you see. The lower tone marks a block. -
Mute and deliver The caption changes and feedback returns.
What you see. The outcome is visible in silence.
Predict before you run Which instruction must come before the mute check?
Reveal the answer
caption.text = message sets the text before muted players leave the function.
Complete checkpoint
The two actions have different visible captions and short tones. Muting preserves the captions.
main.gd
extends Control
const RATE := 22050
const LENGTH := 0.18
var player: AudioStreamPlayer
var caption: Label
var mute: CheckButton
func _ready() -> void:
label("Midnight Deck Dash / Sound signals", 28)
caption = label("Ready for a new delivery.", 77)
player = AudioStreamPlayer.new()
add_child(player)
button("Deliver parcel", 53, 168, feedback.bind("Delivery confirmed", 720.0))
button("Try blocked path", 53, 236, feedback.bind("Blocked. No turn spent.", 240.0))
mute = CheckButton.new()
mute.text = "Mute sound"
mute.position = Vector2(53, 320)
mute.custom_minimum_size = Vector2(210, 48)
add_child(mute)
mute.toggled.connect(change_mute)
func change_mute(enabled: bool) -> void:
if enabled:
player.stop()
caption.text = "Sound muted." if enabled else "Sound enabled."
func feedback(message: String, pitch: float) -> void:
caption.text = message
if mute.button_pressed:
return
player.stream = tone(pitch)
player.play()
func tone(pitch: float) -> AudioStreamWAV:
var count := int(LENGTH * RATE)
var samples := PackedByteArray()
samples.resize(count * 2)
for frame in range(count):
var elapsed := float(frame) / RATE
var envelope := minf(1.0, minf(elapsed / 0.02, (LENGTH - elapsed) / 0.06))
var amplitude := int(sin(TAU * pitch * elapsed) * envelope * 13500.0)
samples.encode_s16(frame * 2, amplitude)
var stream := AudioStreamWAV.new()
stream.format = AudioStreamWAV.FORMAT_16_BITS
stream.mix_rate = RATE
stream.data = samples
return stream
func label(text: String, y: float, font_size: int = 25) -> Label:
var new_label := Label.new()
new_label.text = text
new_label.position = Vector2(52, y)
new_label.add_theme_font_size_override("font_size", font_size)
add_child(new_label)
return new_label
func button(text: String, x: float, y: float, action: Callable, width: float = 214.0) -> Button:
var control := Button.new()
control.text = text
control.position = Vector2(x, y)
control.custom_minimum_size = Vector2(width, 48)
add_child(control)
control.pressed.connect(action)
return control
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#112632"))
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 so the checkpoint can draw, size itself, place buttons and respond to input. -
const RATE := 22050Creates a constant named RATE. The expression uses the whole number 22050. Godot infers the type from the first value. -
const LENGTH := 0.18Creates a constant named LENGTH. The expression reads 0.18. Godot infers the type from the first value. -
var player: AudioStreamPlayerDeclares player for later use. A colon followed by a type says which kind of value belongs here. -
var caption: LabelDeclares caption for later use. A colon followed by a type says which kind of value belongs here. -
var mute: CheckButtonDeclares mute for later use. A colon followed by a type says which kind of value belongs here. -
func _ready() -> void:Godot calls this setup function when the scene enters the running tree. void means the function gives the caller no value back. -
label("Midnight Deck Dash / Sound signals", 28)Calls label with the values in its parentheses. -
caption = label("Ready for a new delivery.", 77)Stores the new value in caption. The expression calls label with the values in its parentheses. -
player = AudioStreamPlayer.new()Create the node that will play a sound resource when the player completes or blocks a delivery. -
add_child(player)Adds the new node to the scene tree so that Godot can run and draw it. -
button("Deliver parcel", 53, 168, feedback.bind("Delivery confirmed", 720.0))Calls button with the values in its parentheses. -
button("Try blocked path", 53, 236, feedback.bind("Blocked. No turn spent.", 240.0))Calls button with the values in its parentheses. -
mute = CheckButton.new()Stores the new value in mute. The expression creates a new CheckButton object. -
mute.text = "Mute sound"Updates mute.text, which controls the words a player sees. The expression uses the text inside the quotes. -
mute.position = Vector2(53, 320)Updates mute.position, which controls the node's local x and y location. The expression packs x and y into one value. -
mute.custom_minimum_size = Vector2(210, 48)Updates mute.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value. -
add_child(mute)Adds the new node to the scene tree so that Godot can run and draw it. -
mute.toggled.connect(change_mute)Run change_mute whenever the mute control changes its checked state. Godot passes the new Boolean. -
func change_mute(enabled: bool) -> void:Defines change 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. -
if enabled:Checks enabled. The indented instructions below run only if this test passes. -
player.stop()Stops this sound, timer or animation. -
caption.text = "Sound muted." if enabled else "Sound enabled."Updates caption.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
func feedback(message: String, pitch: float) -> void:Defines feedback, 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. -
caption.text = messageShow the outcome first. A muted player can read the same words as an unmuted player. -
if mute.button_pressed:Read whether the persistent CheckButton is currently checked. Return before preparing a sound when it is. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
player.stream = tone(pitch)Generate an AudioStreamWAV for the selected frequency and give it to the playback node. -
player.play()Starts this sound or animation. -
func tone(pitch: float) -> AudioStreamWAV:Defines 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 count := int(LENGTH * RATE)Multiply seconds by samples per second to find how many whole samples fit in the tone. -
var samples := PackedByteArray()Creates a variable named samples. The expression calls PackedByteArray with the values in its parentheses. Godot infers the type from the first value. -
samples.resize(count * 2)Reserve two bytes for each signed 16-bit audio sample. -
for frame in range(count):Repeats the indented instructions for each value in range(count). -
var elapsed := float(frame) / RATEDivide the frame number by the sample rate to get a fraction of a second. -
var envelope := minf(1.0, minf(elapsed / 0.02, (LENGTH - elapsed) / 0.06))Choose the weakest of full strength, fade-in and fade-out. This makes both edges of the generated tone gentler. -
var amplitude := int(sin(TAU * pitch * elapsed) * envelope * 13500.0)Calculate the sine wave value for this moment, scale it with the envelope and convert it to a signed sample. -
samples.encode_s16(frame * 2, amplitude)Write this signed value into the two bytes starting at the sample's byte offset. -
var stream := AudioStreamWAV.new()Creates a variable named stream. The expression creates a new AudioStreamWAV object. Godot infers the type from the first value. -
stream.format = AudioStreamWAV.FORMAT_16_BITSStores the new value in stream.format. The expression reads AudioStreamWAV.FORMAT_16_BITS. -
stream.mix_rate = RATETell Godot to play as many samples per second as the generator assumed when it set the wave frequency. -
stream.data = samplesAttach the finished byte array as the sound resource's actual samples. -
return streamFinishes the function and hands its result back to the caller. The expression reads stream. -
func label(text: String, y: float, font_size: int = 25) -> 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 new_label := Label.new()Create a Godot node for visible text. The colon and equals together ask GDScript to infer the type from Label.new(). -
new_label.text = textCopy the supplied string into the actual label so the learner can read it. -
new_label.position = Vector2(52, y)Use a fixed 52-pixel left margin and the caller's chosen height in local design pixels. -
new_label.add_theme_font_size_override("font_size", font_size)Give this label the caller's text size. Other labels can keep their own sizes. -
add_child(new_label)Attach the Label to the scene root so Godot can draw and manage it. -
return new_labelGive the caller this particular label so a later delivery or move can change its words. -
func button(text: String, x: float, y: float, action: Callable, width: float = 214.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()Create a GUI button. It can respond to pointer taps and to the keyboard when focused. -
control.text = textShow the caller's chosen action name on the new button. -
control.position = Vector2(x, y)Place the action at the coordinates passed by the caller. -
control.custom_minimum_size = Vector2(width, 48)Ask the Control layout for at least the supplied width and 48 design pixels of height. -
add_child(control)Connect the new button to the visible scene root. -
control.pressed.connect(action)Connect the Godot pressed signal to the Callable received as action. The action runs on a press later. -
return controlReturn this button so the caller can customise it or keep its reference. -
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("#112632"))Draw the full dark blue practice backdrop from local (0, 0) to the current size. Shapes drawn later can appear above it.
Find and fix a bug
If the tone is distorted or too high, compare RATE in the elapsed calculation with stream.mix_rate. The generated samples and playback need the same rate.
Try a change
Ages 10–12 can change one pitch and compare. Ages 13–16 can cache both generated sounds in _ready and verify the buttons and mute state still work.