Adding / Removing Schema fields during runtime

Hi,

I am working on a opensource ecs game engine and I wanted to replace the current custom network management with Colyseus !

But, ECS means that my game is composed by several Entity that can have 0 to many components. A Component is assigned to an Entity by putting him into a Entity.components object holding them by their names.

I am not very familiar with the Schema aspect, how can I bring this data structure into Colyseus ?

Idea 1 :

Define types during runtime with defineTypes, something like :

const entity = new Entity() // Entity extends empty Schema
entity.addComponent(new MyComponent()) // MyComponent extends Schema with data structure

// The second line will run this :
schema.defineTypes(entity , {
  MyComponent: MyComponent
});

But, well ... Schema defines types in object prototype (https://github.com/colyseus/schema/blob/master/src/annotations.ts#L283) not instance ... so it will not work as each Entity instance may have different Components.

Idea 2 :

Sending maps of Components for each Entity (preferred solution as I already have a Entity.components attributes.

class Entity extends Schema {
    @type({ map: Components})
    components = new MapSchema<Components>();
}

But again I think it will not work because the engine will store different Component data structures. Examples : PositionnableComponent with x, y, angle, HealthComponent with current, maximum, etc ...

Okay I am working on something !

The solution may be having a global hashmap of Component detached from Entity objects. It allows us to group the Component types in the State and send them as group. Not sure it will works but, at least i will try this :)

I will keep updated the work on this thread.

Did it work?