Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(engine): losing a component counts as changing it

esengine7db6db7eaadc
diff --git a/sdk/src/ecs/ChangeTracker.ts b/sdk/src/ecs/ChangeTracker.ts
index b7b11a2d5..4aaf89940 100644
--- a/sdk/src/ecs/ChangeTracker.ts
+++ b/sdk/src/ecs/ChangeTracker.ts
@@ -100,14 +100,7 @@ export class ChangeTracker {
recordRemoved(component: AnyComponentDef, entity: Entity): void {
if (!this.trackedComponents_.has(component._id)) return;
- let buffer = this.componentRemovedBuffer_.get(component._id);
- if (!buffer) {
- buffer = [];
- this.componentRemovedBuffer_.set(component._id, buffer);
- }
- buffer.push({ entity, tick: this.worldTick_ });
- this.componentAddedTicks_.get(component._id)?.delete(entity);
- this.componentChangedTicks_.get(component._id)?.delete(entity);
+ this.recordRemovedById(component._id, entity);
}
recordRemovedById(componentId: symbol, entity: Entity): void {
@@ -119,5 +112,11 @@ export class ChangeTracker {
buffer.push({ entity, tick: this.worldTick_ });
this.componentAddedTicks_.get(componentId)?.delete(entity);
this.componentChangedTicks_.get(componentId)?.delete(entity);
+ // Losing a component IS a change to it. Without this, `anyChangedSince`
+ // — which reads only this watermark — says nothing happened, and a
+ // consumer that gates work on it never learns the component is gone.
+ // The UI layout did exactly that: removing a FlexContainer left its
+ // padding and justification laying the node out for the rest of the run.
+ this.componentLastChangedTick_.set(componentId, this.worldTick_);
}
}
diff --git a/sdk/tests/query-mut-change-tracking.test.ts b/sdk/tests/query-mut-change-tracking.test.ts
index aa452c092..0ae8aec77 100644
--- a/sdk/tests/query-mut-change-tracking.test.ts
+++ b/sdk/tests/query-mut-change-tracking.test.ts
@@ -101,3 +101,41 @@ describe('Mut() write-back records change tracking for builtin components', () =
expect(changedSince(world, tickBeforeWrite - 1)).toContain(e);
});
});
+
+/**
+ * The other half of "did anything touch this component": losing it.
+ *
+ * `anyChangedSince` is the O(1) gate consumers use to decide whether to do any
+ * work at all, and it reads one per-component watermark. Removal used to record
+ * only into the per-entity maps, so a component being taken off an entity was
+ * invisible to that gate — the UI layout, which gates its whole solve on it,
+ * went on laying nodes out with a FlexContainer that had been removed.
+ */
+describe('removal is a change', () => {
+ it('moves the component watermark, so anyChangedSince sees it', () => {
+ const world = new World();
+ world.enableChangeTracking(Transform);
+ const e = world.spawn();
+ world.insert(e, Transform, defaultTransform());
+
+ world.advanceTick();
+ const quiet = world.getWorldTick();
+ expect(world.anyChangedSince(Transform, quiet)).toBe(false);
+
+ world.advanceTick();
+ world.remove(e, Transform);
+ expect(world.anyChangedSince(Transform, quiet)).toBe(true);
+ });
+
+ it('still lists the entity as removed since that tick', () => {
+ const world = new World();
+ world.enableChangeTracking(Transform);
+ const e = world.spawn();
+ world.insert(e, Transform, defaultTransform());
+
+ const before = world.getWorldTick();
+ world.advanceTick();
+ world.remove(e, Transform);
+ expect(world.getRemovedEntitiesSince(Transform, before)).toContain(e);
+ });
+});