API Reference
createTimerService
Section titled “createTimerService”function createTimerService(config?: TimerServiceConfig): ITimerServiceConfiguration:
| Property | Type | Default | Description |
|---|---|---|---|
maxTimers | number | 0 | Maximum timer count (0 = unlimited) |
maxCooldowns | number | 0 | Maximum cooldown count (0 = unlimited) |
Timer API
Section titled “Timer API”schedule
Section titled “schedule”Schedule a one-time timer:
const handle = timerService.schedule('explosion', 2000, () => { createExplosion();});
// Cancel earlyhandle.cancel();scheduleRepeating
Section titled “scheduleRepeating”Schedule a repeating timer:
// Execute every secondtimerService.scheduleRepeating('regen', 1000, () => { player.hp += 5;});
// Execute immediately once, then repeat every secondtimerService.scheduleRepeating('tick', 1000, () => { console.log('Tick');}, true); // immediate = truecancel / cancelById
Section titled “cancel / cancelById”Cancel timers:
// Cancel by handlehandle.cancel();// ortimerService.cancel(handle);
// Cancel by IDtimerService.cancelById('regen');hasTimer
Section titled “hasTimer”Check if timer exists:
if (timerService.hasTimer('explosion')) { console.log('Explosion is pending');}getTimerInfo
Section titled “getTimerInfo”Get timer information:
const info = timerService.getTimerInfo('explosion');if (info) { console.log(`Remaining: ${info.remaining}ms`); console.log(`Repeating: ${info.repeating}`);}Cooldown API
Section titled “Cooldown API”startCooldown
Section titled “startCooldown”Start a cooldown:
timerService.startCooldown('skill_fireball', 5000);isCooldownReady / isOnCooldown
Section titled “isCooldownReady / isOnCooldown”Check cooldown status:
if (timerService.isCooldownReady('skill_fireball')) { castFireball(); timerService.startCooldown('skill_fireball', 5000);} else { console.log('Skill still on cooldown');}
// or use isOnCooldownif (timerService.isOnCooldown('skill_fireball')) { console.log('On cooldown...');}getCooldownProgress / getCooldownRemaining
Section titled “getCooldownProgress / getCooldownRemaining”Get cooldown progress:
// Progress 0-1 (0=started, 1=complete)const progress = timerService.getCooldownProgress('skill_fireball');console.log(`Progress: ${(progress * 100).toFixed(0)}%`);
// Remaining time (ms)const remaining = timerService.getCooldownRemaining('skill_fireball');console.log(`Remaining: ${(remaining / 1000).toFixed(1)}s`);getCooldownInfo
Section titled “getCooldownInfo”Get complete cooldown info:
const info = timerService.getCooldownInfo('skill_fireball');if (info) { console.log(`Duration: ${info.duration}ms`); console.log(`Remaining: ${info.remaining}ms`); console.log(`Progress: ${info.progress}`); console.log(`Ready: ${info.isReady}`);}resetCooldown / clearAllCooldowns
Section titled “resetCooldown / clearAllCooldowns”Reset cooldowns:
// Reset single cooldowntimerService.resetCooldown('skill_fireball');
// Clear all cooldowns (e.g., on respawn)timerService.clearAllCooldowns();Lifecycle
Section titled “Lifecycle”update
Section titled “update”Update timer service (call every frame):
function gameLoop(deltaTime: number) { timerService.update(deltaTime); // deltaTime in ms}Clear all timers and cooldowns:
timerService.clear();Debug Properties
Section titled “Debug Properties”// Get active timer countconsole.log(timerService.activeTimerCount);
// Get active cooldown countconsole.log(timerService.activeCooldownCount);
// Get all active timer IDsconst timerIds = timerService.getActiveTimerIds();
// Get all active cooldown IDsconst cooldownIds = timerService.getActiveCooldownIds();Blueprint Nodes
Section titled “Blueprint Nodes”Cooldown Nodes
Section titled “Cooldown Nodes”StartCooldown- Start cooldownIsCooldownReady- Check if cooldown is readyGetCooldownProgress- Get cooldown progressGetCooldownInfo- Get cooldown infoResetCooldown- Reset cooldown
Timer Nodes
Section titled “Timer Nodes”HasTimer- Check if timer existsCancelTimer- Cancel timerGetTimerRemaining- Get timer remaining time
Service Token
Section titled “Service Token”For dependency injection:
import { TimerServiceToken, createTimerService } from '@esengine/timer';
// Register serviceservices.register(TimerServiceToken, createTimerService());
// Get serviceconst timerService = services.get(TimerServiceToken);