Jump to content
  • Advertisement
Sign in to follow this  
asdqwe

[C++] Timer for shot delay

This topic is 4211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need a function to make a gun (in a game) shoot at determined intervals instead of continuously. I thought about setting a function ShootCheck() to be executed each game cycle, to verify if iTimer (an integer) has reached zero. If yes, the gun is allowed to shoot. If not: iTimer--; But I want to do it without any global variables. Is it possible? Please send me the code...

Share this post


Link to post
Share on other sites
Advertisement
class Gun
{
public:
Gun()
{
lastFired = 0;
}

void shot()
{
if(lastFired - GetTime() >= 1000) // GetTime would return time in milliseconds
{
// BANG!
// Spawn a bullet and smoke, etc.

lastFired = GetTime();
}
}

private:
int lastFired;
}


Then just call the shot() function each update, it checks if it's has passed more than 1000 milliseconds (1 sec) since the last bullet was fired, if so, it shoots, else, it does nothing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!