endel / ecs

(NOT READY TO USE) Experimenting with @colyseus/schema + ECSY
MIT License
17 stars 4 forks source link

Re-export Attributes from Ecsy #4

Closed will-hart closed 4 years ago

will-hart commented 4 years ago

This PR allows custom systems to be built in packages importing @colyseus/ecs with properly defined types.

For instance it allows the following to be written without TS errors or falling back to any/unknown types:

import { Attributes, System, World } from '@colyseus/ecs'

export class MySystem extends System {
  constructor(private injectedDependency: MyInjectedDependency, world: World, attributes?: Attributes) {
    super(...)
  }
}
Elyx0 commented 4 years ago

I don't understand where the injectedDependency comes from I cannot see it in master ecsy nor master ecs

will-hart commented 4 years ago

In the example injectedDependency is just any injected dependency - i.e. not part of the ECS.

will-hart commented 4 years ago

Oh hold up, I've messed up - registerSystem takes a SystemConstructor not an instance of the system.

I'll close and submit a new PR if I come up with a better way to manage System dependencies.

will-hart commented 4 years ago

For the record I think the official way to do this is to register the system like this:

world.registerSystem(MySystem, { myDependency })

with a system that looks like this:

class MySystem extends System {
    static queries = { ... }
    private dep!: MyDependency

    init(attrs?: { myDependency: MyDependency }) {
        // use ! or throw if attrs?.myDependency is falsy
        this.dep = attrs?.myDependency!
    }

    execute() {
        const result = this.dep.doSomething()
        // ...
    }
}

I couldn't see it in the ecsy docs but the constructor passes the attributes to init() of the system.