TL;DR: Using a timestep method that calls update()
multiple times per frame. After an event happens, need to add a wait time before it can happen again. What's the best way to do this so the wait times are always the same across all hardware?
Lets say that a bullet is spawned every time the player presses the mouse button. If the player holds the button down, a new bullet will spawn every frame (which is obviously WAY too many). The way I always got around this was to create a variable shotCoolDown
and set it equal to some arbitrary number (usually 60). Then, in the update()
function, I would subtract 1 from shotCoolDown
every frame, and only allow the player to shoot if shotCoolDown
was less than or equal to 0, and then after creating the bullet, set shotCoolDown
back up to 60. At 60 FPS, this created a maximum of 1 bullet per second.
This worked when calling update()
once per frame. However, I'm now using a new timestep method from this article, which calls update()
multiple (always different) times per frame. Because I don't know how many times update()
will be called, I can no longer use my original shotCoolDown
method.
I've also tried comparing the current time to the time at the mouse button press, and if the difference is more than 1000 ms (1 second), spawn a bullet. However, this doesn't exactly work. Since the computer's clock continues to tick while the game is paused, the player can simply shoot, pause the game and wait for the limit to pass, then unpause the game and shoot again.
Is there a better way to add these time limits? I know I can use timers, but some of my limits will be as small as 1/10th of a second; I'd rather not use a timer for such a small period of time.