Work out when each beat is due from the moment the music started, n * 60 / bpm seconds, and fire every beat whose time has passed. Starting a new 0.4-second timer after each beat adds up the lateness: in our Godot 4.7 test at 150 BPM it was 0.13 s late by beat 10 and 0.56 s late after 15 seconds, one beat short. The script below stayed within 0.015 s, one frame, for all 38 beats.
The code
Add this node, set bpm, and connect its beat signal to whatever should happen on the beat: launch a firework, flash the lights, pulse the UI. Call start() on the same frame you start the music. The signal passes the beat number, so you can launch something bigger every 4th or 16th beat.
extends Node
## Calls on_beat on every beat of a song or show without drifting late.
## Set bpm, connect the beat signal to whatever should launch, then call start() when the music starts.
signal beat(n: int)
@export var bpm := 120.0
var _start_usec := 0
var _next := 0
var _running := false
func start() -> void:
_start_usec = Time.get_ticks_usec()
_next = 0
_running = true
func stop() -> void:
_running = false
func _process(_delta: float) -> void:
if not _running:
return
var elapsed := (Time.get_ticks_usec() - _start_usec) / 1000000.0
var interval := 60.0 / bpm
# beat n is due at n * interval from the start, so a late frame never pushes the next beats back
while elapsed >= _next * interval:
beat.emit(_next)
_next += 1
What we measured
150 BPM, a beat every 0.4 s, for 15 seconds at 60 frames per second. For each beat we measured how late it fired against n x 0.4 s from the start. Both methods ran at the same time. Two runs gave the same numbers to within 0.001 s.
- A new
create_timer(0.4)after each beat: 0.131 s late at beat 10, 0.297 to 0.298 s at beat 20, 0.464 s at beat 30, and 0.564 s at the last one. 37 of the 38 beats in 15 seconds. - The script: 0.014 to 0.015 s late at every beat, the length of one frame. All 38 beats.
Why chained timers fall behind
A timer can only fire on a frame, so it fires up to one frame after it runs out. The next timer starts from that late moment, so each beat inherits the lateness of every beat before it. At 60 frames per second that was about 1/60 of a second per beat. Counting every beat from the same start time means a late frame delays only that one beat.
What to change
bpm: the tempo of your song.- If the first downbeat comes after a short intro, call
start()that many seconds after the music starts. stop()pauses the beats, for example when the player pauses the game. Callstart()again together with the music.- Following the music itself, if it can stutter, pause or be seeked: a song position that moves on every frame.
Fireworks that do not need sub-emitters: fireworks without sub-emitters.
A whole show on the beat: Fireworks Show Kit for Godot 4 plays cues written in seconds or in beats, counted from a BPM and a beat offset, and can build a show for you with an opening, a climb, a pause and a finale. It comes with launch racks, fuses and a harbor demo.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Fireworks Show Kit for Godot 4 plays firework cues written in seconds or in beats from a BPM, and can build a whole show with an opening, a climb and a finale.
More how-tos for Fireworks Show Kit for Godot 4: A song position that moves on every frame
More how-tos: Fire, smoke and explosions
- Make the explosion as big as its damage
- Fire that turns to smoke in a cone
- Bright fire with dark smoke
- Smoke particles leave a hard line on the ground
- Particles drawing in the wrong order
- Explosion effect in one burst
- 3D debris particles not rotating
- Particles all disappear at once
- Muzzle flash with a real light
- Glowing particles in Godot 4
- Glowing particles cast dark shadows
- Particles from the surface of a mesh, at runtime
- Fireworks without sub-emitters
- Make a firework rocket burst at the top of its arc at any launch speed
- Fire or dust over a whole building
- Steam that turns to black smoke when the food burns
- A torch light that flickers the same on every screen