# How can I refactor “attack cooldown” into “attack speed”?

I'm building a game where the player's character can attack bad guys and vice versa. I built this with the concept of an "attack cooldown" because I don't want units to be able to attack each other every single frame. I want there to be some delay between attacks. Here's roughly how it works:

``````if unit.ticksUntilAttack <= 0
unit.attack(target)
unit.ticksUntilAttack = unit.attackCooldown
unit.ticksUntilAttack = unit.ticksUntilAttack - 1
``````

I also have a page where I show the character's stats and list this as "Attack Cooldown". I was showing my friend and he said, "Why don't you rename that to 'Attack Speed'"? I explained that it's not really the speed at which you attack because "smaller is better". We both agreed that "attack speed" is a more standard way to show this concept and that I should change things the way he suggests.

The problem for me is that "attack cooldown" is a much more intuitive way for me to program the solution. I'm wondering if there's some "trick" to refactoring the current code I've got. I'm thinking something like this:

``````if unit.attackSpeedAccumulator >= SUM_NEEDED_TO_ATTACK //a constant that's the same for every unit
unit.attack(target)
unit.attackSpeedAccumulator = 0
unit.attackSpeedAccumulator = unit.attackSpeedAccumulator + unit.attackSpeed
``````

Is there a smarter way to do this?

-
This is entirely a UI issue. There is no reason to change the game logic. – Sean Middleditch Sep 10 '13 at 1:28

You could express it in terms of "attacks per second" or "attack rate". If the per-second numbers are too small to be convenient for the player to compare, you could just scale them by an arbitrary constant - e.g. 0.5 attacks per second (1 attack per 2 seconds) could be multiplied by 10 or 100 and displayed as "5" or "50". The way you'd code it would be something like

``````attackAccumulator += attackRate * frameTime;
if (attackAccumulator >= 1.0)
// allow attack
``````

Or you could keep your current code, and just calculate attacks-per-second as 1 / the cooldown time in seconds.

On the other hand, I think the concept of a cooldown time or charge-up time is pretty common and players will understand it. Many games have that type of mechanic. As long as you display the number as a time value (e.g. "5 seconds" instead of just "5"), I think it would be fine.

-
Actually I think you understood the UX problem better than either of us. It wasn't that it was "cool down" or "speed", it's that it was "per tick" which is meaningless to a player. Whatever value I use, I should present it as "per second" or at least some other time unit that's meaningful to a player. Thanks – Daniel Kaplan Sep 9 '13 at 6:12