Skip to content

Core.Utilities.TimerOn

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

TimerOn (TON)

The TimerOn class, located in the Meatcorps.Engine.Core.Utilities namespace, implements a standard On-Delay Timer. This utility is designed to trigger an action only after a specific condition has remained true for a continuous period of time. If the input condition becomes false at any point before the timer reaches the delay threshold, the timer resets completely.

Class Overview

Properties

Property Type Description
Output bool Becomes true only when the input has been true for the full duration of the delay.
Elapsed float The current time (in milliseconds) that has passed while the timer is active.
TotalTime float The target delay duration (in milliseconds).
TimeRemaining float How much time is left (in milliseconds) before the Output triggers.
NormalizedElapsed float A value between 0.0 and 1.0 representing the progress of the timer.

Core Logic: The Update Method

The Update method is the heart of the class. It should be called every frame within your game loop.

public void Update(bool input, float deltaTime)
{
    if (input)
    {
        _elapsed += deltaTime * 1000; // Convert seconds to milliseconds
        if (_elapsed >= _delay)
            Output = true;
    }
    else
    {
        _elapsed = 0; // Reset if the condition is lost
        Output = false;
    }
}
  • When input is true: The timer accumulates time. Once it hits the _delay, Output becomes true.
  • When input is false: The timer immediately resets _elapsed to 0 and Output to false.

Use Cases

  1. Input Buffering / Confirmation: Requiring a player to hold a "Quit" button for 2 seconds before returning to the main menu.
  2. Charged Attacks: An ability that only fires if the "Attack" button is held down for a specific duration.
  3. Environment Mechanics: A pressure plate that triggers a door, but only if an object stays on it for 1 second (to prevent accidental triggers from debris).
  4. AI Detection: An enemy that "spots" the player only after the player has been in their line of sight for a continuous duration.
  5. State Transitions: Waiting for a "settling" period before transitioning from one animation state to another.

Usage Example

The following example demonstrates how to use TimerOn to implement a "Hold to Interact" mechanic.

using Meatcorps.Engine.Core.Utilities;

public class InteractionHandler
{
    private TimerOn _holdTimer;
    private const float HoldDurationMs = 1500f; // 1.5 seconds

    public InteractionHandler()
    {
        _holdTimer = new TimerOn(HoldDurationMs);
    }

    public void Update(float deltaTime)
    {
        // Check if the interact key (e.g., 'E') is currently pressed
        bool isPressingInteract = Input.IsKeyDown(Keys.E);

        // Update the timer
        _holdTimer.Update(isPressingInteract, deltaTime);

        if (_holdTimer.Output)
        {
            PerformAction();
            _holdTimer.Reset(); // Manually reset if you want to prevent repeating
        }

        // You can use NormalizedElapsed for UI progress bars
        UpdateProgressBar(_holdTimer.NormalizedElapsed);
    }

    private void PerformAction()
    {
        Console.WriteLine("Action Performed!");
    }
    
    private void UpdateProgressBar(float progress)
    {
        // Logic to update a UI radial or bar
    }
}

Related Utilities

  • EdgeDetector: Often used alongside TimerOn to detect the exact frame the Output transitions from false to true.
  • FixedTimer: Similar to TimerOn, but typically used for recurring pulses or loops rather than a conditional delay.

Clone this wiki locally