   Cooldown timer
    Hello ppl ,   i am about to start coding a RTS in C++ and i was thinking a structure where all of the map was moving in turns , for example a time ticker would make all the moves for that second and it would refresh everything for the other second.    at first my only problem was a cooldown timer  i thought of the function wait() but wait would freeze all the game wouldnt it ? so if i just took the time at the thing was cast / done and until the cooldown time was on it wouldnt let it do it again.    But i'm not sure how that works excatly i mean; im going to put the timer into a loop thus , preventing other code to be functional so  im afraid that the code wont let other units / buildings do anything for that time being , so i would write another sleep() function..    im really confused about this , can plz some1 point this out?  i googled the problem but couldnt get any results.   
  you just want a timer (float cooldown) that you decrement each timestep (cooldown -= elapsedTime), and when it is <0, you are ready to go again.so if each tick of the map is 1 second, you update your cooldowns each second with elapsedTime = 1.0f or something.If each tick is just a frame, you setup a system timer to get the difference in frame times, and update the cooldown timer in realtime.;  so ur suggesting that i should make a seperate cooldown class which makes absoulty much sense :D thank you  , sorry about this stupid problem though.. my head is in a mess im taking care of everything.. which makes me a little confused on little problems   ;  If you use time based movement (frame rate independent), you almost certainly calculate the number of milliseconds the loop took. What I do is this:Have the following variablesint loopMS,coolTime,cooling,coolCfgWait;bool cooling,firing; //I dont care how you hande your firing, replace firing with whatever you use to indicate firing//When you fire, start the cooling timeif(firing == true && cooling == false){coolStart = 0;cooling = true;}if(coolTime > coolCfgWait && cooling == true){cooling = false;}//put this, along with all your time reliant functions, at the end of the loop//That is, given you start the timer at the start of the loopif(cooling == true){coolTime += loopMS;}That will prevent you from firing for X milliseconds after you fire. I have a fully working game that features this, if you would like to see the source, I would be glad to show it to you. I calculate the loop time via an SDL function, but it can easily be done though many other libraries.   ;  you really don't need a "class" for this. just add the cooldown timer to anything that needs it. (though you could go full OO and make a class.) But there are often other factors to cooldowns that you want to factor in that make it dificult to make a "cooldown class". expecially ties between weapons, weapon states, other items driven by the cooldown (like effects).;Quote:Original post by VillageIdiotIf you use time based movement (frame rate independent), you almost certainly calculate the number of milliseconds the loop took. What I do is this:Have the following variables*** Source Snippet Removed ***That will prevent you from firing for X milliseconds after you fire. I have a fully working game that features this, if you would like to see the source, I would be glad to show it to you. I calculate the loop time via an SDL function, but it can easily be done though many other libraries.im jsut confused about the fact that without a proper listener listening to incoming cooldown timings  how does the program knows which unit has a cool down .But the program must not focus only on a unit  because there will be alot more.. and i would be more than glad to see the source ^^ so i may get my foggy mind a little clearer with a working code;Quote:Original post by KulSeranyou really don't need a "class" for this. just add the cooldown timer to anything that needs it. (though you could go full OO and make a class.) But there are often other factors to cooldowns that you want to factor in that make it dificult to make a "cooldown class". expecially ties between weapons, weapon states, other items driven by the cooldown (like effects).the reason why i want to do it in a seperate class is for only debugging purposes and a cleaner coding thats all :P   ;  this is how i usually handle cooldown related thingsevery single "game object" type class has an update function such asclass GameObject{ . . . public bool update(float elapsed_seconds); . . . private float cool_down_timer;};then everytime the main game loop executes i have something like..draw_everything();update_everything(elapsed_seconds);..update_everything will call every objects update(elapsed_seconds) function.  it is the responsibility of the function to handle the cooldown...ie to decrement the cooldown_timer by the elapsed_seconds.  When the cooldown_timer reaches 0 then it is eligible to do something again.   ;  Hello,I am also trying to make a delay timer, in this case to get a fixed framerate and to prevent the program from taking 100% cpu load continuously.I tried Sleep(1); in the render loop, however, this caused my framerate to drop significantly. I display the time since the last frame, and it increased from 1 to 16. Does anyone have an idea how I can delay for just 1 ms? to stop 100% load, or how I can make a fixed framerate?;  Sleep(0) should do the trick.   