🕒 Timer System (Timer)

The engine includes a high-precision timer system based on real game time (dt), allowing you to execute functions automatically after a specified number of seconds, with the option to repeat indefinitely. It is ideal for scheduling events, animations, explosions, shots, spawns, or any time-based logic.

💡 Unlike the browser's setTimeout() or setInterval(), the Timer system is fully integrated into the engine and synchronized with the game loop. It works the same in the editor, Play mode, or Web/PWA builds.

📘 1. Timer(seconds, functionName, loop, target)

Creates a new timer that will execute a function after a given number of seconds. It can repeat automatically if the loop parameter is enabled.

📋 Syntax

Timer(seconds, functionName, loop = false, target = null);

📥 Parameters

📤 Returns

Does not return a value. The timer is handled automatically.

⚙️ Basic example

// Script: bomb.js
function Start(obj) {
  console.log("💣 Bomb activated...");
  Timer(3, "Explode", false, obj); // Calls Explode(obj) after 3 seconds
}

function Explode(obj) {
  console.log("💥 BOOM!", obj.name);
  destroyObject(obj);
}

🔁 Loop example

// Generate enemies every 2 seconds
function Start(obj) {
  Timer(2, "SpawnEnemy", true, obj);
}

function SpawnEnemy(obj) {
  console.log("👾 New enemy spawned by:", obj.name);
}
⚠️ Important:
- All timers execute their functions using CallFunction(target, functionName).
- Therefore, the target parameter must be a valid engine object.
- If not specified, the timer cannot call the function and a warning will appear in the console.

📙 3. clearTimer(functionName)

Cancels a specific timer or all active timers.

📋 Syntax

clearTimer(functionName = null);

📥 Parameters

⚙️ Examples

// Cancel a specific timer
clearTimer("SpawnEnemy");

// Cancel all active timers
clearTimer();

📒 4. pauseTimers(pause)

Pauses or resumes all active timers simultaneously.

📋 Syntax

pauseTimers(pause = true);

📥 Parameters

⚙️ Example

// Pause the game and all timers
pauseTimers(true);

// Resume the game and timers
pauseTimers(false);
💡 Useful when pausing the game (menus, dialogs, loading screens, etc.).

💡 Full example

// Script: turret.js
function Start(obj) {
  console.log("🔫 Turret activated");
  Timer(1, "Shoot", true, obj); // shoots every second
}

function Shoot(obj) {
  console.log("💥 Shot from", obj.name);
  spawnBullet(obj);
}

function spawnBullet(obj) {
  const bullet = instantiatePrefab("Bullet");
  bullet.x = obj.x + obj.w / 2;
  bullet.y = obj.y;
}

⚡ Technical details

Conclusion:
The engine's timer system is precise, safe, and very flexible. It allows natural scheduling of delayed or periodic behaviors within the game flow. Ideal for any type of 2D game: platformers, shooters, RPGs, tower defense, or simulations.