Timer class lets you measure and control the passage of time inside a script. You start a timer with a duration in seconds, optionally attach finish and tick callbacks, and then check its state each frame. Timers are ideal for enforcing cooldowns, delaying actions, and scheduling periodic events without blocking script execution. A single Timer instance can be started, stopped, and restarted as many times as you need, making it easy to reuse the same object across multiple cooldown cycles.
Constructors
Timer is instantiated with the global new helper:
start to actually begin counting.
Instance Properties
| Property | Type | Read-only | Description |
|---|---|---|---|
isRunning | boolean | Yes | true while the timer is actively counting down. |
isStopped | boolean | Yes | true when the timer is not running. |
maxTime | number | Yes | The total duration the timer was started with, in seconds. |
remainingTime | number | Yes | How many seconds remain before the timer finishes. |
Instance Methods
start
Starts the timer. You can provide optional callbacks and an optional initial elapsed time.
| Parameter | Type | Description |
|---|---|---|
time | number | Duration in seconds. |
onFinish | fun(): void | Called once when the countdown reaches zero. |
onTick | fun(): void | Called every game tick while the timer is running. |
initialTime | number | Seconds to treat as already elapsed when starting. |
stop
Stops the timer. Optionally fires the onFinish callback even though the timer did not reach zero naturally.
addTime
Adds seconds to the remaining time. Optionally expands maxTime if the addition would push remaining time above the current maximum.