Timers ARE dependent on framerate?

Are you using Tick to drive the timer? My guess is it has something to do with Tick which is driven by the time taken to render the previous frame. I’m checking source code, but are you doing this in c++ or blueprints?

I’m programming a gun for my game in c++ UE4.13. I’m using Timers to handle the time between firing bullets, similar to the ShooterTutorial.

I’ve noticed that my weapon’s firing rates vary depending on the framerate. This is especially noticeable on fast firing weapons.

I tried the same thing on the ShooterTutorial and I noticed the same problem. I even took a video:

(I set the machinegun to have a TimeBetweenShots to 0.075, the same firerate as the weapon in my game), the gun shoots significantly slower on 10fps than 30fps. It takes about 5 seconds to fire 50 bullets in 10fps, but 3.40 seconds to do the same on 30fps.

Are timers bad for weapon fire rates? I know it’s not likely for someone to play a game at 10fps, but it’s not unusual for a game to drop to 25 fps and even then it’s noticeably shooting slower. It definitely feels like it shoots faster on high framerates too (but my computer can’t record consistently at a high fps so I couldn’t really test it).

TLDR: Using timers to handle weapon fire rates. Rapid fire weapons shoot noticeably slower during low framerates. Is this a bug or is there a better way to handle fast shooting weapons?

Any help is appreciated. Thanks in advance! :slight_smile:

All in C++. I don’t have anything in tick in my weapon class. The ShooterTutorial doesn’t either… so I’m not sure if it’s a bug or if timers were never meant to be used that way :confused:

No there is an option to set up a timer that works off of a variable and uses the value of the value as seconds so if you had a float with a value of 30 the timer would be set to thirty seconds.

Tick is being used under the hood by FTimerManager. I could have sworn it wasn’t frame rate dependent but it might be. What is the frame rate drop in your video caused by? The tick/timer could be getting held up by some other program execution.

I’ve noticed the FPS drops when I have too many entities on screen. However in the video I posted above I used t.maxfps in the console to limit the framerate to 10 and the gun was consistently firing slower than when i tried t.maxfps 30.

That might be a specific case where you’re changing an internal engine variable that the tick event is driven off of. Timers should be frame rate independent but capping the frame rate in the engine like that might be different.

You could define Tick(float deltaSeconds) on the actor, in this case the gun. It’s a virtual function so you would have to override it, but you could just have the gun keep track of when it last fired and have the time since last shot variable get updated every tick. Essentially:

void Tick(float deltaSeconds){
  fTimeSinceLastFire += deltaSeconds;
}

void FireWeapon(){
  if(fTimeSinceLastFire >= WeaponCooldown){
      Weapon->Fire();
  }
}

This might be more helpful if you still want to use timers: http://www.tomlooman.com/using-timers-in-ue4/

Yup that’s what my code does and so does the ShooterTutorial.

Yeah I think I’m gonna try running it in tick and see if it has the same issue.

UPDATE:

After further testing, I’ve noticed EVERYTHING in my game becomes slower on low framerates - I’ve tested dropping physics objects, player movement etc. Even at 30 fps everything is much slower than at 60. I’ve tried it in the ShooterTutorial as well, but it isn’t really noticeable unless it drops to extremely low fps (under 10).

Anyone else experience this? My game’s framerate settings are left at default. I’m using 4.13. Could this possibly be an engine bug?

It’s just a part of the engine and computing in general. Framerate and rendering a frame is part of the work the game engine does every time it runs through a tick/cycle. So if you have some cpu operation that forces the thread or system to wait for a second it’s going to take that second before it moves on to updating physics, frame render, or anything else. It usually happens so quick you don’t notice that each thing is updated sequentially, but when that cpu crunch happens it’s slowing down the whole system, not just camera renders.

Sorry I’m not sure I understand :confused:

I don’t believe I’m forcing the engine to “wait” for anything, shouldn’t it still be running tick every frame? I’m not making it count number of frames but delta seconds so it shouldn’t be affected by fps.

For example I have a weapon that should fire 10 bullets a second (timer value of 0.1 before the next bullet fires). At 10 fps it should still be firing on time at 1 bullet per frame but it definitely can’t keep up. At 20 fps it still is noticeably slower even though it has more than enough ticks per second.

Just a note, you should always include a call to super when overriding Tick, e.g.

void Tick(float deltaSeconds)
{
	// Call super
	Super::Tick(deltaSeconds);
	
	// Do my own stuff
	fTimeSinceLastFire += deltaSeconds;
 }

Otherwise you might be missing some critical update logic happening in the parent class.