DeltaTimer
Coding Style wiki
DeltaTimer is a timer driven by accumulating externally fed deltaTime (game time). The owner (a MonoBehaviour Update, etc.) must keep calling UpdateTimer(dt) to drive it — the timer freezes as soon as feeding stops, and feeding Time.deltaTime makes it naturally follow Time.timeScale and game pause. It supports the Timer (one-shot), Tick (repeating), and Mark (time anchor) modes plus time-speed adjustment.
| Namespace | OxGKit.TimeSystem |
| Type | public class DeltaTimer |
| Source | DeltaTimer.cs |
using OxGKit.TimeSystem;
Quick Start
using OxGKit.TimeSystem;
using UnityEngine;
public class SkillCooldown : MonoBehaviour
{
private DeltaTimer _cooldown;
private void Start()
{
// Create and auto play (autoPlay: true)
this._cooldown = new DeltaTimer(true);
// Set a 3-second cooldown
this._cooldown.SetTimer(3f);
}
private void Update()
{
// Must keep feeding deltaTime to drive the timer
this._cooldown.UpdateTimer(Time.deltaTime);
if (this._cooldown.IsTimerTimeout())
{
// Cooldown finished (time's up)
}
}
}
General Rules
DeltaTimer vs RealTimer
Attention A DeltaTimer only advances while UpdateTimer(dt) is being fed; feeding Time.deltaTime makes it follow Time.timeScale and game pause (it freezes). A RealTimer counts by the system clock and keeps running even while the game is paused — pick deliberately (see Picking the Right Tool).
Playback State
Important A timer is stopped by default after construction — call Play (or use the autoPlay: true constructor) to start counting.
Reminder Stop clears all timing data (keeps the time speed); Reset also resets the time speed back to its default; Pause only pauses and can be resumed with Play.
Constructors
public DeltaTimer()
public DeltaTimer(bool autoPlay)
Creates the timer (internally runs Reset first); with autoPlay set to true, it starts counting immediately after creation.
Members
| Member | Description |
|---|---|
public float deltaTime { get; } | The last deltaTime fed through UpdateTimer. |
Driving and Playback Control
Method Overview
| Method | Description |
|---|---|
| UpdateTimer | Fed deltaTime by the owner to drive the timer. |
| Play | Starts or resumes counting. |
| Pause | Pauses counting (resumable). |
| Stop | Stops and clears timing data. |
| Reset | Resets all state (including time speed). |
| IsPlaying | Whether the timer is counting. |
| IsPause | Whether the timer is paused (stopped). |
| GetTime | Gets the timer's current time (seconds). |
| SetTimeSpeed | Sets the time speed. |
| GetTimeSpeed | Gets the time speed. |
UpdateTimer
public void UpdateTimer(float dt)
Must be fed deltaTime continuously through a MonoBehaviour Update to drive the timer (accumulates internally).
private void Update()
{
this._deltaTimer.UpdateTimer(Time.deltaTime);
}
Important No feeding = frozen timer — none of the Timer / Tick / Mark checks will advance.
Play
public void Play()
Starts counting, or resumes from a paused state (the paused span is deducted — the time does not jump).
Pause
public void Pause()
Pauses counting and records the pause point; resumable with Play.
Stop
public void Stop()
Stops counting and clears all timing data (Timer / Tick / Mark settings are zeroed), but keeps the time speed (timeSpeed).
Reset
public void Reset()
Resets all state to defaults (including resetting the time speed to 1) and returns to the stopped state.
IsPlaying
public bool IsPlaying()
Returns whether the timer is currently counting.
IsPause
public bool IsPause()
Returns whether the timer is paused (not playing).
GetTime
public float GetTime()
Gets the timer's current accumulated time (seconds); while counting it is affected by SetTimeSpeed, and while paused it returns the time at the pause point.
SetTimeSpeed
public void SetTimeSpeed(float timeSpeed)
Sets the time speed (default 1), affecting the flow of GetTime (and therefore the Timer / Tick / Mark checks).
GetTimeSpeed
public float GetTimeSpeed()
Gets the current time speed.
Timer (One-Shot)
A one-shot countdown based on the configured seconds.
Method Overview
| Method | Description |
|---|---|
| SetTimer | Sets the seconds to count. |
| TimerCountdown | Gets the remaining seconds (0 when reached). |
| IsTimerTimeout | Returns whether the timer has reached its time. |
| GetTimerCountdownRatio | Gets the countdown ratio (1 decreasing to 0). |
SetTimer
public void SetTimer(float timeSeconds)
Sets the seconds to count (current time + seconds becomes the trigger point).
this._deltaTimer.SetTimer(3f);
this._deltaTimer.Play();
TimerCountdown
public float TimerCountdown()
Calculates the remaining seconds until the trigger point; returns 0 directly once the trigger time has been exceeded.
IsTimerTimeout
public bool IsTimerTimeout()
Returns whether the timer has reached its time (true once past the trigger point).
Reminder After the time is up, IsTimerTimeout() keeps returning true — for one-shot behavior, call Stop or a new SetTimer after handling the trigger.
GetTimerCountdownRatio
public float GetTimerCountdownRatio()
Gets the countdown ratio of the Timer, from 1 decreasing to 0, where 0 = time's up (handy for progress-bar UI).
Tick (Repeating)
Fires repeatedly at the configured interval (re-arms automatically after each timeout).
Method Overview
| Method | Description |
|---|---|
| SetTick | Sets the Tick interval in seconds. |
| GetTick | Gets the configured Tick interval. |
| TickCountdown | Gets the remaining seconds of the current Tick. |
| IsTickTimeout | Returns whether the Tick is due (auto re-arms). |
| GetTickCountdownRatio | Gets the Tick countdown ratio (1 decreasing to 0). |
SetTick
public void SetTick(float tickSeconds)
Sets the Tick interval in seconds; after IsTickTimeout fires, the Tick keeps cycling.
// Fire every 0.5 seconds
this._deltaTimer.SetTick(0.5f);
this._deltaTimer.Play();
GetTick
public float GetTick()
Gets the configured Tick interval in seconds.
TickCountdown
public float TickCountdown()
Gets the remaining seconds until the current Tick fires; returns 0 directly once the trigger time has been exceeded.
IsTickTimeout
public bool IsTickTimeout()
Returns whether the Tick is due; when it returns true, the next Tick is automatically re-armed from the current time, forming a repeating trigger.
private void Update()
{
this._deltaTimer.UpdateTimer(Time.deltaTime);
if (this._deltaTimer.IsTickTimeout())
{
// Fires once per Tick cycle
}
}
Important When IsTickTimeout() returns true it re-arms automatically, which "consumes" that trigger — check it from exactly one caller per cycle, or the trigger will be eaten by another caller.
GetTickCountdownRatio
public float GetTickCountdownRatio()
Gets the Tick countdown ratio, from 1 decreasing to 0, where 0 = time's up.
Mark (Time Anchor)
Marks a point in time to measure elapsed time against (stopwatch-style).
Method Overview
| Method | Description |
|---|---|
| SetMark | Sets the mark to the current time. |
| GetMark | Gets the marked time. |
| GetElapsedMarkTime | Gets the elapsed seconds since the mark. |
SetMark
public void SetMark()
Sets the mark to the timer's current time.
GetMark
public float GetMark()
Gets the marked time.
GetElapsedMarkTime
public float GetElapsedMarkTime()
Gets the elapsed time (seconds) since the last mark; returns 0 while the current time has not passed the mark.
this._deltaTimer.SetMark();
// ... some time later
float elapsed = this._deltaTimer.GetElapsedMarkTime();