1

I am struggling with naming 3 variables defining the following concept:

An action, after being performed, is not available for an amount of time and then is available again.

The concept comprises 3 variables: - A boolean (is action available?) - An int (action 'cooldown' duration) - Another int (action 'cooldown' countdown/timer)

As you can imagine, naming them actionCooldown, actionCooldownDuration and actionCooldownCountdown is awkward, as the names are too long.

I can't, however, find a better naming standard for this concept.

I thought about delay and pause but they don't necessarily communicate "period of unavailability after use".

The context is a bit generic, this is in game development but I am using these cooldowns anywhere from input to movement to collisions.

New contributor
RedKnight91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 6
    "as the names are too long." Why are they too long? Also, why do you need actionCooldown; can't you just test if the actionCooldownCountdown is zero or has otherwise expired? – Nicol Bolas Feb 14 at 22:17
  • 3
    Also, shouldn't this "concept" of a cooldown be encapsulated into an object of some sort? if you really are using them "anywhere from input to movement to collisions", you should have some kind of ActionCooldown object that represents this construct. – Nicol Bolas Feb 14 at 22:19
  • Those variable names are not too long. – Eric King Feb 14 at 22:59
3

I'd recommend grouping these together as an object, and making IsAvailable a property/method based on the cooldown timer.

Beyond that - what's the distinction between cooldown duration and cooldown timer?

If cooldown duration is 'the amount of time this action always takes to cool down after use', then it sounds like part of the configuration, where-as the cooldown timer is part of the state. In that case, it doesn't need an instance per-object.

If it's something that's set each time a cooldown is started, then you may not need both - the timer could just be set to the desired duration and count down to zero. If you do need both (for example, to show a progress bar), that's fine, just consider whether that's the case.

So for example (C#):

class Cooldown
{
    public float Total { get; private set; }
    public float Elapsed { get; private set; }
    public bool Available => Elapsed >= Total;

    public void Start(float total)
    {
        Total = total;
        Elapsed = 0f;
    }

    public void ElapseTime(float time)
    {
        Elapsed += time;
    }
}
1

Would refactor.

boolean isActionAvailable(timestamp lastTimeActionPerformed, timestamp currentTime);

With the refactor, just need to keep one variable, lastTimeActionPerformed

The time between actions can be static variable in the isActionAvailable() function (hardcoded) or perhaps something loaded from a resource file at runtime.

1

Since cooldowns are associated with specific actions, it makes sense to store related state in a structure/object corresponding to that action, then you can drop the action prefix on the variables. The "cooldown duration" is typically called the "base cooldown" in games (which might be further reduced by cooldown reduction, if your game supports that concept). You can have a function to check if the action is ready (no need to store a boolean for this) and a function that performs the action and puts it on cooldown.

Assuming you are using an OO language, it might look something like this:

class Action {
  int baseCooldown;
  int remainingCooldown;

  bool isReady() {
    return remainingCooldown == 0;
  }

  void execute() {
    remainingCooldown = baseCooldown;
    // perform the action here
  }

  void tick(int elapsedTime) {
    remainingCooldown = max(0, remainingCooldown - elapsedTime);
  }
}
0

"Cooldown" means "not ready." It's a negative. Consider talking about readiness instead of not-readiness.

actionCooldown = !IsReady

actionCooldownDuration = ReadinessInterval

actionCooldownCountdown = TimeTillReady or if you need to express units SecTillReady or MinutesTillReady

0

It's fine to use terminology from the domain. Everyone who has played a game will know what a cooldown is.

Write any API from the API user's view, for example

interface ICooldown
{
    int? Cooldown {get; }
    void OnTrigger();
}

Your Answer

RedKnight91 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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