ISource2ModScheduler
Interface for scheduling tasks and managing timers.
Provides functionality for:
- Scheduling callbacks for next frame
- Creating repeating or delayed timers
- Managing timer lifecycle
Interface for scheduling tasks and timers.
Interface id ISource2ModScheduler002 (SOURCE2MOD_SCHEDULER_INTERFACE) · Global g_pSource2ModScheduler · Header source2mod/ISource2ModScheduler.h
Types
TimerCallback
using TimerCallback = Source2ModCallback<void()>;Callback executed by timers A timer's or a next-frame task's callback: a function, an object and a method, or a lambda (Source2ModCallback) -- whose it is, the core reads off it.
Methods
NextFrame
virtual void NextFrame(TimerCallback task) = 0;Executes a task on the next frame.
Runs once on the next game frame
Dropped if the owning plugin unloads before the frame arrives
| Parameter | Type | Description |
|---|---|---|
task | TimerCallback | Callback function; the plugin whose code it is owns it |
AddTimer
virtual Timer* AddTimer(float interval, TimerCallback callback, int flags = 0) = 0;Adds a timer.
Killed automatically when the owning plugin unloads
| Parameter | Type | Default | Description |
|---|---|---|---|
interval | float | Time in seconds between executions | |
callback | TimerCallback | Function to execute | |
flags | int | 0 | Timer behavior flags |
Returns Timer* — Pointer to created Timer
KillTimer
virtual void KillTimer(Timer* timer) = 0;Removes a timer.
| Parameter | Type | Description |
|---|---|---|
timer | Timer* | Timer instance |
Timer
Represents a scheduled timer.
Contains execution data and state for scheduled callbacks.
Fields
| Field | Type | Description |
|---|---|---|
Owner | PluginId | Plugin that created the timer, 0 for Source2Mod itself. The callback holds code inside that plugin's library, so the scheduler has to drop the timer before the library is closed -- destroying the std::function afterwards calls into unmapped memory. |
Interval | float | Interval between executions (seconds) |
ExecTime | double | Next execution time (engine time) |
Callback | TimerCallback | Callback function |
Flags | int | Timer flags (TimerFlags) |
InExec | bool | True while callback is executing |
KillMe | bool | Marks timer for deletion |
Constructors
Timer(PluginId owner, float interval, double execTime, TimerCallback callback, int flags);Constructs a timer.
Enums
TimerFlags
enum TimerFlagsTimer behavior flags.
| Name | Value | Description |
|---|---|---|
TIMER_FLAG_REPEAT | 1 << 0 | Repeats timer indefinitely |
TIMER_FLAG_NO_MAPCHANGE | 1 << 1 | Automatically stops timer on map change |
Macros
| Macro | Expands to |
|---|---|
NEXT_FRAME(task) | g_pSource2ModScheduler->NextFrame(task) |
ADD_TIMER(interval, cb, ...) | g_pSource2ModScheduler->AddTimer(interval, cb, ##__VA_ARGS__) |
KILL_TIMER(timer) | g_pSource2ModScheduler->KillTimer(timer) |