The Gun Fires Slower Than Its Fire Rate
When a weapon set to four shots a second fires fewer while the trigger is held, and a different number on a faster screen, the timer between shots is probably looked at before the frame's time is taken off it. Each shot then waits one frame too many. Our own ammo blueprint did exactly that: 38 shots in ten seconds at 60 frames a second, where its setting promised 40.
What you see
- Holding the trigger, the gun fires a little slower than its stats say. Counted over ten seconds, a few shots are missing.
- The same gun fires at a different rate at 60 and at 120 frames a second. In our test both fell short of 40, by different amounts; the output is below.
- Taps spaced further apart than the fire interval are not affected: only a held trigger waits on the timer to the last frame.
Why does every shot come a frame late?
Each frame, the code of our ammo and reload blueprint first asked "is the wait since the last shot over?" and only then took this frame's time off the wait. In the frame where the wait reached zero it had already said no, so the shot came in the frame after, and the next wait started one frame late. That frame is lost on every shot, and a frame is longer on a slower screen, so a slow screen loses more.
The TLA+ check of the blueprint found it: TLC walked through every frame of a held trigger and found the frame in which a shot should have come and did not.
How to check your game for it
Count. Hold the trigger in the test for ten seconds at 60 and at 120 frames a second, and compare with what the setting promises: at 0.25 seconds between shots that is 40 either way. A second test makes sure a shot still never comes before its wait is over.
The test: fix.test.ts
import assert from "node:assert/strict";
import test from "node:test";
import { holdTrigger } from "./fix.ts";
/** Shots from holding the trigger for `seconds` at `fps` frames a second. */
function shotsIn(seconds: number, fps: number, fireInterval: number): number {
let wait = 0;
let shots = 0;
for (let frame = 0; frame < seconds * fps; frame++) {
const r = holdTrigger(wait, fireInterval, 1 / fps);
wait = r.wait;
if (r.fired) shots++;
}
return shots;
}
test("a held trigger fires four times a second at any frame rate", () => {
// a fire interval of 0.25 s is 15 frames at 60 a second and 30 at 120: 40 shots in ten seconds
const shots = { 60: shotsIn(10, 60, 0.25), 120: shotsIn(10, 120, 0.25) };
assert.deepEqual(shots, { 60: 40, 120: 40 }, "shots in ten seconds, by frames a second");
});
test("no second shot before the wait is over", () => {
const first = holdTrigger(0, 0.25, 0.1);
assert.equal(first.fired, true);
const second = holdTrigger(first.wait, 0.25, 0.1);
assert.equal(second.fired, false);
assert.ok(Math.abs(second.wait - 0.15) < 1e-9, `the wait is ${second.wait}`);
});
The catch:
- a held trigger fires four times a second at any frame rate
What it said on the code with the bug put back
✖ a held trigger fires four times a second at any frame rate
shots in ten seconds, by frames a second
got { '60': 38, '120': 39 }, expected { '60': 40, '120': 40 }
✔ no second shot before the wait is over
2 tests: 1 passed, 1 failedWhat it said on the fixed code
✔ a held trigger fires four times a second at any frame rate
✔ no second shot before the wait is over
2 tests: 2 passed, 0 failedThe fix
Take the frame's time off the wait first, then look whether it is over. The shot then comes in the frame in which the wait runs out, at any frame rate.
One limit stays, and Z3 showed it on the same blueprint: a timer shorter than one frame cannot fire more than once a frame this way. At 0.02 seconds between shots, a held trigger fires every frame at 30 frames a second and every fifth frame at 240, which is 48 shots a second against 30. If your weapons fire that fast, the wait has to allow several shots in one frame and keep the time left over.
What changed
Returns whether a shot fired this frame and the wait after it. */
export function holdTrigger(wait: number, fireInterval: number, dt: number): { fired: boolean; wait: number } {
- const canShoot = wait <= EPSILON;
+ // Count this frame's time off first: the shot may come in the frame in which the wait runs out.
const left = Math.max(0, wait - dt);
+ const canShoot = left <= EPSILON;
if (canShoot) return { fired: true, wait: fireInterval };
return { fired: false, wait: left };The fixed code: fix.ts
/* A held trigger that fires once every `fireInterval` seconds, whatever the frame rate. The wait
since the last shot is counted down first, then looked at, so the shot comes in the frame in which
the wait runs out. From the ammo and reload blueprint:
https://spielerando.com/en/mechanics/ammo-and-reload */
/** Rounding error left by adding up frame times: a wait this close to 0 is over. */
const EPSILON = 1e-9;
/** One frame with the trigger held. `wait` is the time left before the next shot may come, s.
Returns whether a shot fired this frame and the wait after it. */
export function holdTrigger(wait: number, fireInterval: number, dt: number): { fired: boolean; wait: number } {
// Count this frame's time off first: the shot may come in the frame in which the wait runs out.
const left = Math.max(0, wait - dt);
const canShoot = left <= EPSILON;
if (canShoot) return { fired: true, wait: fireInterval };
return { fired: false, wait: left };
}
Paste this to your AI
In my game a weapon fires slower than its fire rate when the trigger is held, and the rate seems to change with the frame rate. I think the timer between shots is looked at before this frame's time is taken off, so every shot waits one frame too many.
1. Before you change anything, find every timer that decides when something may happen again: shots, attacks, spells, spawns. For each, write down in which order it does three things in a frame: take the frame's time off, look whether the wait is over, start the next wait.
2. Write a test like the one below for each held action: count what happens in ten seconds at 60 and at 120 frames a second, and compare with what the setting promises. Run it and paste me the real output.
3. Where it falls short, fix it the way the code below does: take the frame's time off first, then look. Run the tests again and paste the output.
4. Swap the two lines back for a moment, show me that the test fails, and undo it.
5. Tell me which of my timers are shorter than one frame at 30 frames a second: those cannot keep their rate this way, and I need to decide what they should do.
Do not tell me it is fixed unless the test output shows it.
The code, from https://spielerando.com/en/fixes/gun-fires-slower-than-its-fire-rate (TypeScript, tested):
```ts
/* A held trigger that fires once every `fireInterval` seconds, whatever the frame rate. The wait
since the last shot is counted down first, then looked at, so the shot comes in the frame in which
the wait runs out. From the ammo and reload blueprint:
https://spielerando.com/en/mechanics/ammo-and-reload */
/** Rounding error left by adding up frame times: a wait this close to 0 is over. */
const EPSILON = 1e-9;
/** One frame with the trigger held. `wait` is the time left before the next shot may come, s.
Returns whether a shot fired this frame and the wait after it. */
export function holdTrigger(wait: number, fireInterval: number, dt: number): { fired: boolean; wait: number } {
// Count this frame's time off first: the shot may come in the frame in which the wait runs out.
const left = Math.max(0, wait - dt);
const canShoot = left <= EPSILON;
if (canShoot) return { fired: true, wait: fireInterval };
return { fired: false, wait: left };
}
```
The test that catches it:
```ts
import assert from "node:assert/strict";
import test from "node:test";
import { holdTrigger } from "./fix.ts";
/** Shots from holding the trigger for `seconds` at `fps` frames a second. */
function shotsIn(seconds: number, fps: number, fireInterval: number): number {
let wait = 0;
let shots = 0;
for (let frame = 0; frame < seconds * fps; frame++) {
const r = holdTrigger(wait, fireInterval, 1 / fps);
wait = r.wait;
if (r.fired) shots++;
}
return shots;
}
test("a held trigger fires four times a second at any frame rate", () => {
// a fire interval of 0.25 s is 15 frames at 60 a second and 30 at 120: 40 shots in ten seconds
const shots = { 60: shotsIn(10, 60, 0.25), 120: shotsIn(10, 120, 0.25) };
assert.deepEqual(shots, { 60: 40, 120: 40 }, "shots in ten seconds, by frames a second");
});
test("no second shot before the wait is over", () => {
const first = holdTrigger(0, 0.25, 0.1);
assert.equal(first.fired, true);
const second = holdTrigger(first.wait, 0.25, 0.1);
assert.equal(second.fired, false);
assert.ok(Math.abs(second.wait - 0.15) < 1e-9, `the wait is ${second.wait}`);
});
```
In my game a weapon fires slower than its fire rate when the trigger is held, and the rate seems to change with the frame rate. I think the timer between shots is looked at before this frame's time is taken off, so every shot waits one frame too many.
1. Before you change anything, find every timer that decides when something may happen again: shots, attacks, spells, spawns. For each, write down in which order it does three things in a frame: take the frame's time off, look whether the wait is over, start the next wait.
2. Write a test like the one below for each held action: count what happens in ten seconds at 60 and at 120 frames a second, and compare with what the setting promises. Run it and paste me the real output.
3. Where it falls short, fix it the way the code below does: take the frame's time off first, then look. Run the tests again and paste the output.
4. Swap the two lines back for a moment, show me that the test fails, and undo it.
5. Tell me which of my timers are shorter than one frame at 30 frames a second: those cannot keep their rate this way, and I need to decide what they should do.
Do not tell me it is fixed unless the test output shows it.Where we found it
The blueprint the bug was in, and the test that guards it there:
- Ammo and Reload:
a held trigger fires every fire interval, not one frame later
How we found them: the bugs our proofs found in our own code
Was this page helpful?
Next step
Blueprint
Ability Cooldowns
Spells and abilities wait on timers too; the cooldown blueprint lets time pass before it checks the press and keeps leftover time, so one long frame can return several charges at once.