Skip to content

AspectSubscriptionManager

Adrian Papari edited this page Nov 26, 2019 · 11 revisions

Subscriptions

You can subscribe to entities that match a certain Aspect. You can use this to:

  • Obtain a list of matching entities
  • Be informed of new entities matching the Aspect.
  • Be informed of entities no longer matching the Aspect.

Usage

Retrieve entity list

EntitySubscription subscription = world.getAspectSubscriptionManager()
        .get(Aspect.all(ComponentX.class, ComponentY.class));
IntBag entityIds = subscription.getEntities();

Note that the bag is only valid while the system is being processed. Don't hold on to it! Also don't change the contents.

Subscribe to changes

  1. Implement EntitySubscription.SubscriptionListener.
  2. Fetch subscription and add listener.
EntitySubscription subscription = world.getAspectSubscriptionManager()
        .get(Aspect.all(ComponentY.class).exclude(ComponentZ.class));

subscription.addSubscriptionListener(new EntitySubscription.SubscriptionListener() {
    @Override
    public void inserted(IntBag entities) {}

    @Override
    public void removed(IntBag entities) {}
});

See @DelayedComponentRemoval if you need to components to be readable in listeners after the user removes them.

Clone this wiki locally