Cooldown
Overview
This component provides a robust mechanism for entities to manage timed cooldowns for their abilities or actions. It tracks a "charged" state, initiates timed delays, cancels active cooldowns, and can persist its state across game saves. It also supports custom callback functions for when a cooldown starts and when it finishes.
Dependencies & Tags
None identified.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
charged | boolean | false | Indicates whether the cooldown period has finished and the ability is ready. |
cooldown_duration | number | nil | The default duration (in seconds) for the cooldown period. |
startchargingfn | function | nil | A callback function to be executed when the cooldown period begins. |
onchargedfn | function | nil | A callback function to be executed when the cooldown period ends and the component becomes charged. |
task | task_handle | nil | An internal handle to the active DoTaskInTime task managing the cooldown timer. |
cooldown_deadline | number | nil | The global game time (from GetTime()) at which the cooldown is scheduled to finish. nil if not currently charging. |
Main Functions
OnRemoveFromEntity()
- Description: Cleans up any active cooldown tasks when the component is removed from its entity, preventing memory leaks or unexpected behavior.
- Parameters: None.
StartCharging(time)
- Description: Initiates a cooldown period for the specified duration. If
timeis not provided, it defaults toself.cooldown_duration. This will setself.chargedtofalseand schedule a task to mark it as charged after the given time. - Parameters:
time: (number, optional) The duration in seconds for the cooldown. Ifnil,self.cooldown_durationis used.
FinishCharging()
- Description: Immediately ends the current cooldown period, setting
self.chargedtotrueand invoking theonchargedfncallback if it exists. - Parameters: None.
GetTimeToCharged()
- Description: Returns the remaining time until the cooldown period ends.
- Parameters: None.
- Returns: (
number) The remaining time in seconds, or0if already charged or not currently charging.
IsCharged()
- Description: Checks if the component is currently in a charged state (i.e., the cooldown has finished).
- Parameters: None.
- Returns: (
boolean)trueif charged,falseotherwise.
IsCharging()
- Description: Checks if the component is currently undergoing a cooldown period.
- Parameters: None.
- Returns: (
boolean)trueif actively charging,falseotherwise.
OnSave()
- Description: Gathers the necessary data to persist the cooldown component's state, allowing it to be correctly restored after a game save and load.
- Parameters: None.
- Returns: (
table) A table containingcharged(boolean) andtime_to_charge(number) if applicable.
GetDebugString()
- Description: Provides a human-readable string representing the current state of the cooldown, useful for debugging.
- Parameters: None.
- Returns: (
string) "CHARGED!" if charged, or the remaining time formatted as a string if charging.
LongUpdate(dt)
- Description: Handles updates to the cooldown deadline, especially important for synchronizing state after potential game pauses or when time manipulation occurs (
dtrepresents elapsed time). If thecooldown_deadlinefalls below the currentGetTime(), it triggers thedonecharginglogic. - Parameters:
dt: (number) The delta time since the last update.
OnLoad(data)
- Description: Restores the cooldown component's state from saved game data. It will either mark the component as charged or restart the cooldown with the saved remaining time.
- Parameters:
data: (table) The table of saved data, typically fromOnSave().