Events are an important part of any game, and it's only natural to want them for ECS programming. There are plenty of situations where you'll want to know about new components being added and old ones being removed. foreachEntity is good, but can't do everything.
My suggestion is to add two new functions to EntityGroup: onEntityMatched (calls a function when an entity begins to match an archetype) and onEntityUnmatched (calls a function when an entity stops matching an archetype). Here's an example of how they might look in action:
entities.onEntityMatched( function (entity:{ display:DisplayObject }) {
stage.addChild(entity.display);
} );
entities.onEntityUnmatched( function (entity:{ display:DisplayObject }) {
stage.removeChild(entity.display);
} );
var entity = {};
entities.createEntity(entity);
entity.display = Images.get("BeachBall.png"); //The image is automatically added to the stage.
entity.display = null; //The image is automatically removed from the stage.
Note that both systems need to know the value of display. Thus onEntityMatched has to run after setting the new value, but onEntityUnmatched has to run before.
Events are an important part of any game, and it's only natural to want them for ECS programming. There are plenty of situations where you'll want to know about new components being added and old ones being removed.
foreachEntityis good, but can't do everything.My suggestion is to add two new functions to
EntityGroup:onEntityMatched(calls a function when an entity begins to match an archetype) andonEntityUnmatched(calls a function when an entity stops matching an archetype). Here's an example of how they might look in action:Note that both systems need to know the value of
display. ThusonEntityMatchedhas to run after setting the new value, butonEntityUnmatchedhas to run before.