Skip to content

[proposal] Dispatch events upon adding and removing components #30

Description

@player-03

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions