Skip to content

How to run logic conditionally

Option A — Guard inside the system (simple)

  1. Put a condition at the top:
1
2
3
4
5
6
7
8
let paused = false;

world.addSystem((w: any, dt: number) => {
  if (paused) return;
  for (const { c1: pos, c2: vel } of w.query(Position, Velocity)) {
    pos.x += vel.x * dt;
  }
});

Option B — Conditional phases (skip whole groups)

  1. Maintain your phase list dynamically:
1
2
3
4
5
6
7
const base = ["input", "sim", "render"];

function getPhases(paused: boolean) {
  return paused ? ["input", "render"] : base;
}

sched.run(world, dt, getPhases(paused));

Option C — Wrap systems (reuse predicates)

  1. Make a helper:
1
2
3
4
5
6
7
8
const runIf = (pred: () => boolean, fn: (w: any, dt: number) => void) =>
  (w: any, dt: number) => { if (pred()) fn(w, dt); };

world.addSystem(runIf(() => !paused, (w, dt) => {
  for (const { c1: pos, c2: vel } of w.query(Position, Velocity)) {
    pos.x += vel.x * dt;
  }
}));