CooldownController - jimdroberts/FishMMO GitHub Wiki

Description

Controls and manages ability cooldowns for a character. Provides methods for adding, removing, updating, and querying cooldowns, as well as network serialization for synchronizing cooldown state in FishMMO.


API Access

Fields

  • private Dictionary<long, CooldownInstance> cooldowns

    Dictionary of active cooldowns, keyed by ability ID.

  • private List keysToRemove

    List of keys to remove after cooldowns expire.

Methods

  • public override void ResetState(bool asServer)

    Resets the cooldown controller state, clearing all cooldowns. asServer: bool - Indicates if the reset is occurring on the server.

  • public void Read(Reader reader)

    Reads cooldown data from a network reader. reader: Reader - The network reader.

  • public void Write(Writer writer)

    Writes cooldown data to a network writer. writer: Writer - The network writer.

  • public void OnTick(float deltaTime)

    Updates all cooldowns by subtracting deltaTime and removes expired cooldowns. deltaTime: float - Time to subtract from each cooldown.

  • public bool IsOnCooldown(long id)

    Checks if an ability is currently on cooldown. id: long - Ability ID. Returns: bool - True if on cooldown, otherwise false.

  • public bool TryGetCooldown(long id, out float cooldown)

    Tries to get the remaining cooldown time for an ability. id: long - Ability ID. cooldown: out float - Remaining cooldown time. Returns: bool - True if found, otherwise false.

  • public void AddCooldown(long id, CooldownInstance cooldown)

    Adds a cooldown for the specified ability. id: long - Ability ID. cooldown: CooldownInstance - Cooldown instance.

  • public void RemoveCooldown(long id)

    Removes the cooldown for the specified ability. id: long - Ability ID.

  • public void Clear()

    Clears all cooldowns.


Basic Usage

Setup

  1. Attach CooldownController to a character or entity that requires ability cooldown management.
  2. Use AddCooldown to start a cooldown for an ability, and RemoveCooldown to clear it when expired.
  3. Call OnTick regularly (e.g., each frame or tick) to update cooldown timers and remove expired cooldowns.
  4. Use Read and Write for network serialization of cooldown state.

Example

// Example 1: Adding and updating cooldowns
cooldownController.AddCooldown(abilityId, new CooldownInstance(totalTime, totalTime));
cooldownController.OnTick(Time.deltaTime);

// Example 2: Checking and removing cooldowns
if (cooldownController.IsOnCooldown(abilityId))
{
    float remaining;
    if (cooldownController.TryGetCooldown(abilityId, out remaining))
    {
        // Use remaining cooldown time
    }
}
// Remove when done
cooldownController.RemoveCooldown(abilityId);

Best Practices

  • Always call OnTick to keep cooldowns updated and remove expired entries.
  • Use TryGetCooldown to safely query remaining cooldown time.
  • Clear all cooldowns on character reset or respawn to avoid stale state.
  • Use network serialization methods (Read/Write) to synchronize cooldowns in multiplayer scenarios.
  • Avoid duplicate cooldowns for the same ability by checking with IsOnCooldown before adding.
⚠️ **GitHub.com Fallback** ⚠️