mayakwd/tick-knock

Cannot create Query

Closed this issue · 3 comments

Hey,

Trying to implement a Query as described in the manual using TypeScript:

const displayListQuery = new Query((entity: Entity) => {
  return entity.hasAll(View, Position, Rotation) && !entity.has(HIDDEN);
});

displayListQuery.onEntityAdded = ({current}: EntitySnapshot) => {
  console.log("We've got a rookie here!");
  container.addChild(current.get(View)!.view);
}
displayListQuery.onEntityRemoved = ({previous}: EntitySnapshot) => {
  container.removeChild(previous.get(View)!.view);
  console.log("Good bye, friend!");
}

But I get the following errors from the compiler:

TS2740: Type '({ current }: EntitySnapshot) => void' is missing the following properties from type 'Signal(snapshot: EntitySnapshot) => void>': handlers, hasHandlers, handlersAmount, connect, and 3 more.

TS2740: Type '({ previous }: EntitySnapshot) => void' is missing the following properties from type 'Signal(snapshot: EntitySnapshot) => void>': handlers, hasHandlers, handlersAmount, connect, and 3 more.

Here's how my own use case looks:

this.engine = new Engine()
this.engine.addSystem(new PhysicsSystem())
const entity = new Entity()
    .add(new Name("SpriteEntity1"))
    .add(new Position(0,0))
    .add(PIXI.Sprite.from("texture.png"))
this.engine.addEntity(entity)

const spriteListQuery = new Query((entity: Entity) => {
    return entity.hasAll(PIXI.Sprite);
});

spriteListQuery.onEntityAdded = ({current}: EntitySnapshot) => {
    console.log("We've got a rookie here!");
    const sprite = current.get(PIXI.Sprite)!
    this.pixiInstance.stage.addChild(sprite)
}

spriteListQuery.onEntityRemoved = ({previous}: EntitySnapshot) => {
    const sprite = previous.get(PIXI.Sprite)!
    this.pixiInstance.stage.removeChild(sprite)
}

engine.addQuery(spriteListQuery);

Next up I tried to test the code in Queries and Systems section. However, that code has this line

for (const entity of this.query.entities) {

even though there is no this.query in class ViewSystem extends System class. What's up with that?

Hi! Thank you for the report, I'm afraid there is a mistake in the documentation.
You need to connect your listener to the query instead of reassigning the signals.

spriteListQuery.onEntityAdded.connect(({current}: EntitySnapshot) => {
    console.log("We've got a rookie here!");
    const sprite = current.get(PIXI.Sprite)!
    this.pixiInstance.stage.addChild(sprite)
});
spriteListQuery.onEntityRemoved.connect(({previous}: EntitySnapshot) => {
    const sprite = previous.get(PIXI.Sprite)!
    this.pixiInstance.stage.removeChild(sprite)
});

@kaphula, I'll fix the documentation in the next few days, thank you for the report!

Fixed in 6ee5b61