Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

lets say i have a gun which fires at a rate of 10 shots every seconds.

so i every gameloop frame, i check if the necessary time has passed and instantiate a new shot.

now what if the framerate of the game actually drops below 10 fps. then the player will have a disadvantage because his gun doesnt fire accordingly anymore.

what is the usual workaround of this problem?

thanks!

share|improve this question
add comment

3 Answers

I would do something like this in Update(...):

_toFire += 10f * elapsedSeconds;
while (_toFire >= 1f)
{
    _toFire -= 1f;

    Bulet bullet = new Bullet(/*start posittion (gun position)*/, /*bullet speed*/);
    bullet.Update(_toFire);

    _bulletList.Add(bullet);
}

In my pseudo code I add as many bullets as should be in game by the time passed between the updates. Then I adjust their positions with update.

Something like that should do ...

share|improve this answer
add comment

A workaround is to determine how many shots need to be fired as a float and accumulate remainders.

Normally your loop is running fast enough that the shots needed is 1.0; sometimes your loop is slow and the shots needed is 1.2 and you accumulate 0.2 for that frame. Once you've accumulated more than an extra 1.0 then simply launch it as an extra shot that frame and subtract 1.0 from the accumulator.

Keep calm and accumulate on.

A second workaround would be to cause the one shot to have "shot * needed" power, so that on a normal frame your shot does 1.0 damage but on slow frames your shot does 1.2 damage.

share|improve this answer
    
The first one is much better than the second, since it is more clear to the player what is happening. –  Lohoris May 13 '12 at 18:54
add comment

Game loops can deploy mechanisms to prevent logic from not being executed if the other parts of the loop take too long. For example, if you'll lock your game logic at 30 frames per second and inside of the loop you check if it's the time to run the logic frame, then instead of putting it in an "if" statement, you put it in a loop that'll have a "max frame skip limit".

The idea is that if your game is going to render slowly then the next iteration the game logic will "catch up" by executing x amount of times instead of just 1.

If you have your bullet shooting tied to a timer then you can also put the logic that checks if it's the time to fire inside of a loop and just try to make up for frame rate drops.

Now, in general, this doesn't matter as much as you describe it. The player isn't REALLY at a disadvantage here. Everything needs to be processed inside of the game loop. If the player's gun doesn't fire, then the AI's gun doesn't fire aswell. So it's all about the implementation. Either you consider frame rate drops when dealing with AI and the player or neighter.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.