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(ecs): asking for a resource is not the same as having one

esengineff25bdeeab5e
diff --git a/sdk/src/ecs/resource.ts b/sdk/src/ecs/resource.ts
index edb69face..11999c697 100644
--- a/sdk/src/ecs/resource.ts
+++ b/sdk/src/ecs/resource.ts
@@ -93,9 +93,17 @@ export class ResourceStorage {
private ticks_ = new Map<symbol, number>();
private globalTick_ = 0;
private nameRegistry_ = new Map<string, ResourceDef<unknown>>();
+ /**
+ * Slots holding a materialised default rather than a value somebody put
+ * there. `has` is what every self-gating subsystem asks before installing
+ * itself, so a read must not answer it: a resource defaulting to `null`
+ * would report present the moment any system took it as a parameter.
+ */
+ private defaulted_ = new Set<symbol>();
insert<T>(resource: ResourceDef<T>, value: T): void {
this.resources_.set(resource._id, value);
+ this.defaulted_.delete(resource._id);
this.ticks_.set(resource._id, ++this.globalTick_);
if (resource._name && !resource._name.startsWith('Resource_')) {
this.nameRegistry_.set(resource._name, resource as ResourceDef<unknown>);
@@ -107,21 +115,24 @@ export class ResourceStorage {
// Clone so each storage owns its default; mutating it never leaks
// into the shared ResourceDef or a sibling world.
this.resources_.set(resource._id, deepClone(resource._default));
+ this.defaulted_.add(resource._id);
}
return this.resources_.get(resource._id) as T;
}
set<T>(resource: ResourceDef<T>, value: T): void {
this.resources_.set(resource._id, value);
+ this.defaulted_.delete(resource._id);
this.ticks_.set(resource._id, ++this.globalTick_);
}
has<T>(resource: ResourceDef<T>): boolean {
- return this.resources_.has(resource._id);
+ return this.resources_.has(resource._id) && !this.defaulted_.has(resource._id);
}
remove<T>(resource: ResourceDef<T>): void {
this.resources_.delete(resource._id);
+ this.defaulted_.delete(resource._id);
this.resMutPool_.delete(resource._id);
this.ticks_.delete(resource._id);
this.nameRegistry_.delete(resource._name);
diff --git a/sdk/tests/resource.test.ts b/sdk/tests/resource.test.ts
index 75fe20421..1bc992790 100644
--- a/sdk/tests/resource.test.ts
+++ b/sdk/tests/resource.test.ts
@@ -186,6 +186,29 @@ describe('ResourceStorage', () => {
storage.insert(Counter, { count: 0 });
expect(storage.has(Counter)).toBe(true);
});
+
+ // Self-gating subsystems install on `has` being false, so materialising
+ // a default must not answer it — otherwise one system taking the
+ // resource as a parameter convinces the gate its plugin is installed.
+ it('stays false after a read materialises the default', () => {
+ expect(storage.get(Counter)).toEqual({ count: 0 });
+ expect(storage.has(Counter)).toBe(false);
+ storage.insert(Counter, { count: 7 });
+ expect(storage.has(Counter)).toBe(true);
+ });
+
+ it('goes back to false after remove, even once read again', () => {
+ storage.insert(Counter, { count: 3 });
+ storage.remove(Counter);
+ storage.get(Counter);
+ expect(storage.has(Counter)).toBe(false);
+ });
+
+ it('is true after set, which is a value somebody put there', () => {
+ storage.get(Counter);
+ storage.set(Counter, { count: 5 });
+ expect(storage.has(Counter)).toBe(true);
+ });
});
describe('remove', () => {