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(sdk): accept Query(Added/Changed(...)) as a defineSystem param (#52)

yhhfd934143e734
diff --git a/sdk/src/query.ts b/sdk/src/query.ts
index b657e4a3c..441f6d8d0 100644
--- a/sdk/src/query.ts
+++ b/sdk/src/query.ts
@@ -59,7 +59,12 @@ export function isChangedWrapper(value: unknown): value is ChangedWrapper<AnyCom
return typeof value === 'object' && value !== null && '_filterType' in value && value._filterType === 'changed';
}
-type QueryArg = AnyComponentDef | MutWrapper<AnyComponentDef> | AddedWrapper<AnyComponentDef> | ChangedWrapper<AnyComponentDef>;
+/**
+ * Every value accepted as a positional `Query(...)` argument: a bare component,
+ * or a component wrapped for mutation / change detection. Exported so `system.ts`
+ * can constrain `SystemParam` against the exact same set — the two must never drift.
+ */
+export type QueryArg = AnyComponentDef | MutWrapper<AnyComponentDef> | AddedWrapper<AnyComponentDef> | ChangedWrapper<AnyComponentDef>;
// =============================================================================
// Filter Expression Tree
diff --git a/sdk/src/system.ts b/sdk/src/system.ts
index 76353197a..31ac63b9c 100644
--- a/sdk/src/system.ts
+++ b/sdk/src/system.ts
@@ -7,7 +7,7 @@
import { AnyComponentDef } from './component';
import { getDefaultContext } from './context';
-import { QueryDescriptor, QueryInstance, MutWrapper, RemovedQueryDescriptor, RemovedQueryInstance } from './query';
+import { QueryDescriptor, QueryInstance, QueryArg, RemovedQueryDescriptor, RemovedQueryInstance } from './query';
import { ResDescriptor, ResMutDescriptor, ResMutInstance, ResourceStorage } from './resource';
import { CommandsDescriptor, CommandsInstance } from './commands';
import {
@@ -49,8 +49,6 @@ export function GetWorld(): GetWorldDescriptor {
// System Parameter Types
// =============================================================================
-type QueryArg = AnyComponentDef | MutWrapper<AnyComponentDef>;
-
export type SystemParam =
| QueryDescriptor<readonly QueryArg[]>
| ResDescriptor<unknown>
diff --git a/sdk/tests/system.test.ts b/sdk/tests/system.test.ts
index e136abe55..196906e28 100644
--- a/sdk/tests/system.test.ts
+++ b/sdk/tests/system.test.ts
@@ -11,7 +11,7 @@ import {
SystemRunner,
} from '../src/system';
import { getDefaultContext } from '../src/context';
-import { Query, Mut, QueryInstance, Removed, RemovedQueryInstance } from '../src/query';
+import { Query, Mut, Added, Changed, QueryInstance, Removed, RemovedQueryInstance } from '../src/query';
import { Res, ResMut, ResMutInstance, defineResource, ResourceStorage } from '../src/resource';
import { Commands, CommandsInstance } from '../src/commands';
import { EventWriter, EventReader, EventWriterInstance, EventReaderInstance, defineEvent, EventRegistry } from '../src/event';
@@ -211,6 +211,32 @@ describe('SystemRunner', () => {
expect(received).toBeInstanceOf(QueryInstance);
});
+ it('should accept a Query(Added(...)) change-detection filter (issue #52)', () => {
+ let received: unknown = null;
+ // Must type-check: Query(Added(...)) is a valid SystemParam, and the
+ // callback element is inferred as the unwrapped component data.
+ const sys = defineSystem([Query(Added(Position))], (q) => {
+ received = q;
+ for (const [, pos] of q) {
+ void pos.x;
+ }
+ });
+ runner.run(sys);
+ expect(received).toBeInstanceOf(QueryInstance);
+ });
+
+ it('should accept a Query(Changed(...)) change-detection filter (issue #52)', () => {
+ let received: unknown = null;
+ const sys = defineSystem([Query(Changed(Velocity))], (q) => {
+ received = q;
+ for (const [, vel] of q) {
+ void vel.dx;
+ }
+ });
+ runner.run(sys);
+ expect(received).toBeInstanceOf(QueryInstance);
+ });
+
it('should resolve Res parameter', () => {
resources.insert(GameState, { score: 42, level: 3 });
let received: unknown = null;