Action Timer

A high-performance, async timer utility for s&box. This package bridges the reliable community-standard Timer class with the modern Scene System, providing both a robust C# API and a designer-friendly Component.


Features
  • Static & Component-Based: Use Timer.Create in code or drop an ActionTimer component onto a GameObject.
  • Async & Threaded: Built on Task and CancellationToken for non-blocking delays.
  • State Control: Pause, Resume, Stop, or Adjust active timers via unique string IDs.
  • Visual Scripting: Trigger GameObjectEvents or Actions directly from the Inspector.
Usage

1. The ActionTimer Component

Add the ActionTimer component to any GameObject to trigger logic after a duration.
  • Duration: Time in seconds before the timer fires.
  • Timer Loops: If checked, the timer restarts immediately after firing.
  • Auto Start: If checked, the timer begins as soon as the component starts.
  • Use Threading: Offloads the timer loop to a background thread.
  • On Timer Elapsed: Hook up any method or event to fire when the time is up.
2. Static C# Methods

For pure code implementations, use the static Timer class.

Simple Delay

A "fire and forget" one-off delay.

C#
// Runs after 1 second on the main thread
Timer.Simple( 1.0f, () => Log.Info( "Time is up!" ) );

// Runs on a background thread
Timer.Simple( 1.0f, () => HeavyLogic(), threaded: true );

Managed Timer

Create a timer with an ID to track or manipulate it later.

C#
// Create a timer that repeats 10 times
Timer.Create( "health_regen", 1.0f, 10, () => Player.Heal( 5 ) );

// Check if it exists
if ( Timer.Exists( "health_regen" ) )
{
    Log.Info( $"Next tick in: {Timer.TimeLeft( "health_regen" )}" );
}

// Pause or Stop it
Timer.Pause( "health_regen" );
Timer.Remove( "health_regen" );


API Reference

MethodDescriptionSimple( delay, func, threaded ) | Fire and forget one-time delay.
Create( id, delay, reps, func, threaded ) | Create a tracked timer (reps: 0 = infinite).
Remove( id ) | Stops and disposes of a timer immediately.
Adjust( id, delay, reps, func ) | Change timer parameters on the fly.
Pause( id ) / UnPause( id ) | Suspends or resumes a timer.
TimeLeft( id ) | Returns float seconds remaining in current cycle.
RepsLeft( id ) | Returns remaining repetition count.

Credits

Based on the sbox-timer-lib originally maintained by the sbox.Community. Updated and wrapped for the Scene System by Farawayrobot.


If you would like to support more of my work you can subscribe at my Patreon: https://www.patreon.com/FarawayRobot
Or want to leave me a tip? You can do so at https://ko-fi.com/farawayrobot