Timers

Timers are a first-class part of the language rather than a library. They let you schedule code to run later or repeatedly — useful for respawn delays, recharge ticks, reload animations and similar timed behaviour. All durations are in milliseconds.

after — run once after a delay

after schedules a block to run a single time once the delay has elapsed.

after 1000 {
	std::console.log("This runs once, 1 second later");
}

// The delay can be any expression
after battlecore::weapon.getReloadDelayMs() {
	battlecore::weapon.reload();
}

every — run repeatedly

every runs a block repeatedly at a fixed interval.

every 1000 {
	std::console.log("This runs every second");
}

limit

Add a limit clause to cap how many times the block runs.

every 1000 limit 5 {
	std::console.log("Runs 5 times, then stops");
}

keep

By default a timer is tied to the scope that created it. Add keep to retain the timer when the device changes state, so it keeps firing across state transitions.

every 1000 keep {
	std::console.log("Keeps ticking even after a state change");
}

Controlling a timer

Assign a timer to a variable to get a handle you can control later. The control statementsstop, start, resume and cancel each take that handle (or no argument to act on the current timer context).

int rechargeTimer = every 4500 {
	shieldRecharge();
};

stop rechargeTimer;     // pause the timer
start rechargeTimer;    // start it again
resume rechargeTimer;   // resume a stopped timer
cancel rechargeTimer;   // cancel it entirely

A common pattern is a one-shot repeating timer that stops itself from inside its own block:

int t = every 4500 {
	shieldRecharge();
	// Prevent the timer from firing again
	stop;
};

Scheduling is built into the language — use the keywords on this page to run code later or repeatedly. To read the current device clock (for example to measure elapsed time), use std::time.now().

Related Topics