Closed will-hart closed 4 years ago
I don't understand where the injectedDependency
comes from I cannot see it in master ecsy nor master ecs
In the example injectedDependency
is just any injected dependency - i.e. not part of the ECS.
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.
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.
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: