Skip to content

Core.Utilities.FixedTimer

Dennis Steffen edited this page Jan 4, 2026 · 1 revision

The is a utility designed to handle repetitive intervals or "pulses" based on a fixed duration. It tracks elapsed time, provides a normalized (0 to 1) progress value, and triggers a boolean "pulse" whenever the duration is reached. FixedTimer

Key Components of FixedTimer

  • Cyclic Behavior: Unlike a standard stopwatch, this timer resets itself automatically. When reaches , the becomes for one frame, and the internal counter resets to zero. _elapsed``_pulseDuration``Output``true
  • Normalized Progress: The property is particularly useful. It returns a value from 0.0 (start) to 1.0 (end of the interval), which is perfect for driving animations or interpolation. NormalizedElapsed
  • Millisecond Based: The internal logic converts (usually in seconds) to milliseconds (), allowing you to define durations in a human-readable format (e.g., 500ms for half a second). deltaTime``deltaTime * 1000

This utility is very powerful in combination with tween utilities, as the can be passed directly into easing functions to create smooth, looping visual effects or transitions. NormalizedElapsed

Use Cases and Examples

1. Fixed Interval Actions (e.g., Shooting)

If you want an entity to perform an action every 500 milliseconds, you can check the property. Output

// Initialize with 500ms duration
FixedTimer _shootTimer = new FixedTimer(500f);

public void Update(float deltaTime)
{
    _shootTimer.Update(deltaTime);

    if (_shootTimer.Output)
    {
        ShootProjectile(); // This triggers every 0.5 seconds
    }
}

2. Looping Animations with Tweens

You can use to drive a "pulse" or "bobbing" effect. Since it goes from 0 to 1 and then snaps back to 0, it creates a perfect loop for things like UI scales or hover effects. NormalizedElapsed

FixedTimer _hoverTimer = new FixedTimer(1000f);

public void Update(float deltaTime)
{
    _hoverTimer.Update(deltaTime);
    
    // Using NormalizedElapsed with a Tweening/Easing library
    // This creates a smooth back-and-forth scaling effect
    float scale = Tween.NormalToUpDown(_hoverTimer.NormalizedElapsed); // Values move 0 -> 1 -> 0
    
    transform.Scale = Vector2.One * (1f + scale * 0.2f);
}

3. Dynamic Gameplay Pacing

You can change the speed of the timer on the fly, which is useful for things like a heartbeat sound that speeds up as a player's health drops.

FixedTimer _heartbeatTimer = new FixedTimer(1000f);

public void OnHealthChanged(float healthPercent)
{
    // Speed up the timer as health decreases (from 1000ms down to 200ms)
    float newDuration = Math.Max(200f, 1000f * healthPercent);
    _heartbeatTimer.ChangeSpeed(newDuration);
}

public void Update(float deltaTime)
{
    _heartbeatTimer.Update(deltaTime);
    if (_heartbeatTimer.Output)
    {
        PlayHeartbeatSound();
    }
}

Summary of Properties

  • Output: only on the frame the timer finishes; otherwise. true``false
  • NormalizedElapsed: A float from 0.0 to 1.0 representing the current cycle progress.
  • ChangeSpeed(float): Allows updating the frequency without creating a new object.

Clone this wiki locally