Framerate Independent High Frequency Timers

Right now, I’m using an FTimer to handle automatic gun fire. The problem is that for fire rates higher than like 400 RPM, there’s a noticeable choppiness in the firing where the timing between each shot isn’t consistent. I need a way to handle automatic gun fire at very high frequencies (higher than FPS).

You probably don’t want a higher resolution timer, what you probably want is to handle firing the projectile as if it has fired at a previous time.

A typical way to handle this is, when your timer callback fires, determine how long ago the weapon should have fired. Then, fire the weapon and advance the projectile yourself to catch up with the current time.

There’s probably built in support for this in the Projectile Movement Component, can’t remember, check the code. Calling Tick Yourself with however long in the past you want the projectile to have fired might work. For instance, if your timer fires and you should have fired too projectiles, one 0.25/60 sec ago, and one 0.75/60 sec ago… Fire one, call Tick(0.75/60) to move it forward, then fire another and call Tick(0.25/60) on that one. There may be a better way in the UE code for this but that’s the general concept.

1 Like

Hmm interesting. So I’m using hit scan for my weapons, not projectiles, but I’d assume the concept is the same. To be clear, I’m noticing the choppiness from the gun firing sound. It fires far too fast to see if the actual rays are being casted consistently, but my assumption would be they’re not as well. I read this article on timers and the author mentions in the section regarding high frequency timers the problem I think you just described so I’ll give your suggestion a try.

Regarding line traces you can’t really go faster than your FPS. Timer is good option because it will stick to 400RPM regardless of frame rate. So when frame rate fluctuates it will sometimes trigger 4 times per frame, sometimes 5 etc. which is probably what you want.

For the sound problem you should consider setting up a looping sound cue, and use start/stop methods rather than playing every shot, because as mentioned above it would sometimes play 4 times per frames, sometimes 5… and obviously not be spaced at 400RPM unless you have 400fps.

The sound cue solution isn’t an option because the loop frequency is tied to the runtime of the audio clip/s and for weapon fire, the tail is quite important

The sound cue solution isn’t an option because the loop frequency is tied to the runtime of the audio clip/s and for weapon fire, the tail is quite important