← Pixel Dreamer Studio

A fire rate that holds at any frame rate

Subtract the shot interval from the timer instead of resetting it, and allow more than one shot in a long frame.

Keep the time left over after each shot: add delta to a timer and, while it holds at least one interval, subtract the interval and fire. Setting cooldown = 1.0 / rate after each shot rounds every shot up to whole frames. In our Godot 4.7 test a gun set to 10 shots a second and held for 10 seconds fired 75 times at 30 fps, 86 at 60 fps and 96 at 144 and 240 fps. At 20 a second it fired 150 at both 30 and 60 fps. The script fired exactly 100 and 200 at all five frame rates.

The code

Save it as fire_rate.gd. Make one per gun with const FireRate := preload("res://fire_rate.gd") and var gun = FireRate.new(10.0) and in _process() call gun.update(delta, Input.is_action_pressed("fire")). It returns how many shots to fire this frame, usually 0 or 1, more after a long frame. The first press fires at once, and letting go does not save up a burst.

extends RefCounted
## Fire at the rate you set at any frame rate: keep the time left over after each shot
## instead of resetting the cooldown, and fire twice in one frame when a frame is long.

var rate := 10.0  # shots per second
var _t := 0.0
var _was_held := false


func _init(shots_per_second: float = 10.0) -> void:
	rate = shots_per_second
	_t = 1.0 / rate  # ready: the first press fires at once


## Call every frame. Returns how many shots to fire this frame (0, 1, or more on a long frame).
func update(delta: float, trigger_held: bool) -> int:
	var interval := 1.0 / rate
	if not trigger_held:
		_t = minf(_t + delta, interval)  # the cooldown runs, but no saved-up burst
		_was_held = false
		return 0
	if _was_held:
		_t += delta  # no time is added on the press frame itself: that frame is the first shot
	_was_held = true
	var shots := 0
	while _t >= interval - 0.000001:  # the small margin stops float error from skipping a shot
		_t -= interval
		shots += 1
	return shots

What we measured

We held the trigger for 10 seconds of game time with a fixed delta at 30, 60, 75, 144 and 240 fps and counted the shots. Two runs gave the same numbers.

Why it happens

With a reset, the cooldown counts down in whole frames, and the shot waits for the first frame where it reaches 0. Whatever that frame overshoots is thrown away, on every shot. At 30 fps 0.1 seconds should be exactly 3 frames, but 0.1 minus three steps of 1/30 is not 0 in floating point but a hair above it, 1.39e-17, so each shot waited a fourth frame: 300 frames, 75 shots. At 60 fps the same thing turned 6 frames into 7. Subtracting the interval keeps the overshoot for the next shot, so the average comes out exact, and the small margin in the script covers the float error.

What to change

The flash on every shot: FPS Gun VFX for Godot 4 has 39 effects, among them 7 muzzle flashes that light the scene with a real OmniLight3D. Call play() on every shot: in rapid fire, smoke and shells still in the air are kept, because play() fires a copy instead of cutting them off.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: FPS Gun VFX for Godot 4 has 7 muzzle flashes with a real light, tracers and shell ejects that keep up with rapid fire.

More how-tos for FPS Gun VFX for Godot 4: Hit sparks from a pool of emitters

More how-tos: Projectiles and aiming