Skip to content

How to despawn entities safely

  1. Despawn immediately when not iterating:
1
world.despawn(e);
  1. Despawn during a query/system via cmd():
1
2
3
4
5
6
7
8
world.addSystem((w: any) => {
  for (const { e, c1: pos } of w.query(Position)) {
    if (pos.x > 10) w.cmd().despawn(e);
  }
});

// apply despawns
world.flush();
  1. Or rely on end-of-frame flush:
1
world.update(dt); // runs systems, then flushes