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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(sdk): Removed fires on despawn, and duplicate names don't corrupt the index
esengineab3cd4b0dbd9
diff --git a/sdk/src/ecs/NameIndex.ts b/sdk/src/ecs/NameIndex.tsindex 4277a1cb5..218750ff3 100644--- a/sdk/src/ecs/NameIndex.ts+++ b/sdk/src/ecs/NameIndex.ts@@ -8,16 +8,19 @@import { Entity } from '../types';export class NameIndex {- private nameToEntity_ = new Map<string, Entity>();+ // Names are NOT unique (games routinely have many "Coin"/"Enemy"), so a name+ // maps to a SET of entities. A 1:1 map corrupted on the first collision:+ // removing/renaming one "Enemy" dropped the shared key and orphaned the other.+ private nameToEntities_ = new Map<string, Set<Entity>>();private entityToName_ = new Map<Entity, string>();update(entity: Entity, name: string): void {const oldName = this.entityToName_.get(entity);- if (oldName !== undefined) {- this.nameToEntity_.delete(oldName);- }+ if (oldName !== undefined) this.unlink_(oldName, entity);if (name) {- this.nameToEntity_.set(name, entity);+ let set = this.nameToEntities_.get(name);+ if (!set) this.nameToEntities_.set(name, (set = new Set()));+ set.add(entity);this.entityToName_.set(entity, name);} else {this.entityToName_.delete(entity);@@ -27,12 +30,25 @@ export class NameIndex {remove(entity: Entity): void {const oldName = this.entityToName_.get(entity);if (oldName !== undefined) {- this.nameToEntity_.delete(oldName);+ this.unlink_(oldName, entity);this.entityToName_.delete(entity);}}findByName(name: string): Entity | null {- return this.nameToEntity_.get(name) ?? null;+ const set = this.nameToEntities_.get(name);+ if (!set) return null;+ // "Last wins" for duplicates (Set keeps insertion order → last added is the+ // last iterated); after one duplicate leaves, the survivor is still found.+ let last: Entity | null = null;+ for (const e of set) last = e;+ return last;+ }++ private unlink_(name: string, entity: Entity): void {+ const set = this.nameToEntities_.get(name);+ if (!set) return;+ set.delete(entity);+ if (set.size === 0) this.nameToEntities_.delete(name);}}diff --git a/sdk/src/world.ts b/sdk/src/world.tsindex 18cbd07c8..0ed420e38 100644--- a/sdk/src/world.ts+++ b/sdk/src/world.ts@@ -258,7 +258,13 @@ export class World {// valid (the query cache no longer honors the global structural version).for (const cppName of this.builtin_.deleteFromEntitySets(entity)) {const def = getComponent(cppName);- if (def) this.queries_.markComponentDirty(def._id);+ if (def) {+ // Record the removal (no-op unless a Removed query tracks it) so+ // Removed(BuiltinComp) fires on despawn, not only on explicit+ // remove() — matching the script-component path below.+ this.changes_.recordRemoved(def, entity);+ this.queries_.markComponentDirty(def._id);+ }}this.queries_.markComponentDirty(Parent._id);this.queries_.markComponentDirty(Children._id);diff --git a/sdk/tests/world.test.ts b/sdk/tests/world.test.tsindex 669b85245..f877e0991 100644--- a/sdk/tests/world.test.ts+++ b/sdk/tests/world.test.ts@@ -79,6 +79,23 @@ describe('World', () => {expect(world.findEntityByName('Same')).toBe(e2);});+ it('keeps a duplicate-named survivor findable when the other leaves', () => {+ const e1 = world.spawn('Enemy');+ const e2 = world.spawn('Enemy');+ world.despawn(e2); // removing the last-registered duplicate must not orphan e1+ expect(world.findEntityByName('Enemy')).toBe(e1);+ world.despawn(e1);+ expect(world.findEntityByName('Enemy')).toBeNull();+ });++ it('renaming one duplicate leaves the other resolvable', () => {+ const e1 = world.spawn('Enemy');+ const e2 = world.spawn('Enemy');+ world.set(e1, Name, { value: 'Boss' }); // must not drop e2's "Enemy" mapping+ expect(world.findEntityByName('Enemy')).toBe(e2);+ expect(world.findEntityByName('Boss')).toBe(e1);+ });+it('should spawn multiple entities', () => {const e1 = world.spawn();const e2 = world.spawn();