DavidPeicho / ecstra

Fast & Flexible EntityComponentSystem (ECS) for JavaScript & TypeScript
MIT License
51 stars 2 forks source link

Events / Signals on entities and queries #21

Closed delaneyj closed 3 years ago

delaneyj commented 3 years ago

When interacting with WebGL there is init/destroying of external buffers. Having events fire when a query changes inclusion of an entity is must for decoupled systems.

DavidPeicho commented 3 years ago

Yes, that's one thing I have been wondering about how I should implement that. Ecsy has those added / removed queries. I guess I will either need something similar, or maybe simply check that based on time stamps.

For instance:

query.execute((e) => {
  if (e.justAdded()) {
    // Create WebGL object.
  }
})

For destroying you raise a good question. At first, I wanted to simply defer destroying using command buffers (#18), but now you raise a valid point: if you delete a component outisde of an execution (outside of a system), then the cleaning will never happen. I guess I will need to expose command buffers outside of systems as well

delaneyj commented 3 years ago

I personally don't see the need to support removal outside of a system, but there are times were there is external steps necessary for cleanup. Killing an orc maybe handled by DamageSystem and it deletes the entity. Well the RenderSystem needs to know if the VBO/UBO related to that orc is now free or maybe level complete means an unload of meshes for the RenderSystem. Both are systems but the have work to do de/construction.

In the past I've done such signalling with rxjs but my guess is you don't want to take on outside libs as a dependency.

DavidPeicho commented 3 years ago

Yeah I wouldn't want to add an external dependency. But OK I understand clearly that's an issue and I will provide a mechanism for that. I actually think removal outside of system is still useful, and using command buffers should be relatively easy I guess

DavidPeicho commented 3 years ago

This has been added in #25

Right now, it's synchronous and you can't really control the order of those events, so don't assume a query event will be triggered before another. I believe this feature with CommandBuffers should be enough for all (or almost) purposes. If we need thse events to run in the system order, I can make a modification to the code (that may use a bit more space though....)