I'm working on a blender game with a character who can use certain abilities. With some of these abilities, I'd like to know how to add a cool down timer (e.g. 2 seconds) to them. How do I do this?
I suggest a timeout implementation.
Use a timer property to let the BGE count the time for you (timer properties always count upwards).
In this state the operation can be activated at any time.
When the operation gets activated, you switch the state to "Blocked" and set the (negative) timeout value.
In this state the operation should not be available.
All you do is wait until the timeout happens. You do this by measuring if time gets positive. When it does you switch to state "ready".
There are other ways to implement timeout. This one is a pretty efficient one as there is not really much to do rather than waiting on the events to happen. By using states you clearly separate the logic of both states.
I hope it helps
There are a number of approaches that you might use. Here are a couple of big-picture ideas.
1) You might record the time (milliseconds) that the ability was last used, and then if the player attempts to use the ability again you test to see if the difference between the recorded time and the current time is large enough. This has the benefit of only needing to be calculated when needed. This method would make it slightly more difficult to show a live count-down to the player, though. you could record the time in a custom property that's associated with the character, or with the ability.
2) You might set the value of a variable or property to a large number when the ability is used, and then periodically update that value to decrease based on how much time has passed. Then when the counter reaches zero, your ability is available again. This has the benefit of being flexible. You can update more frequently if you need to, or less frequently if granularity isn't very important. You have a rough estimate of how much time is remaining and can easily drive other features with that value. This method is also easy to make based on something other than time. Anytime a particular event occurs (each button press, enemy hit, etc.), you subtract one from the value until it reaches zero. This is useful for other kinds of timers in addition to cooldown.
3) Lastly, you might set a property to "always" update a given value, using either of the first two methods. This will force Blender to calculate it every "tic." This gives the most real-time information, but it's one more calculation that has to be done before a tic can be finished, meaning that each tic will take longer.