| name | game-loop |
| description | Server-side game loop implementation with fixed timestep, physics simulation, and tick rate optimization |
| sasmp_version | 1.3.0 |
| version | 2.0.0 |
| bonded_agent | 05-game-loop-developer |
| bond_type | PRIMARY_BOND |
| parameters | {"required":["tick_rate"],"optional":["physics_enabled","max_tick_catchup"],"validation":{"tick_rate":{"type":"integer","min":1,"max":128},"physics_enabled":{"type":"boolean","default":true},"max_tick_catchup":{"type":"integer","min":1,"max":10,"default":5}}} |
| retry_config | {"max_attempts":1,"fallback":"skip_frame"} |
| observability | {"logging":{"level":"info","fields":["tick_number","tick_time_ms","entity_count"]},"metrics":[{"name":"game_tick_duration_seconds","type":"histogram"},{"name":"game_tick_count","type":"counter"},{"name":"game_entity_count","type":"gauge"}]} |
Server Game Loop
Implement deterministic game loops with fixed timestep for consistent gameplay.
Fixed Timestep Loop
class GameLoop {
constructor(tickRate = 60) {
this.tickRate = tickRate;
this.tickMs = 1000 / tickRate;
this.tick = 0;
this.running = false;
}
start() {
this.running = true;
this.lastTime = process.hrtime.bigint();
this.accumulator = 0n;
this.loop();
}
loop() {
if (!this.running) return;
const now = process.hrtime.bigint();
const deltaNs = now - this.lastTime;
this.lastTime = now;
this.accumulator += deltaNs;
const tickNs = BigInt(.(. * ));
ticksProcessed = ;
(. >= tickNs && ticksProcessed < ) {
.(.);
.++;
. -= tickNs;
ticksProcessed++;
}
(ticksProcessed === ) {
. = ;
.();
}
sleepMs = .(, . - (.) / );
( .(), sleepMs);
}
() {
.();
.(dt);
.(dt);
.();
.();
}
}