Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inform system when entity is added or removed #54

Closed
chrisapril opened this issue Sep 2, 2014 · 3 comments
Closed

Inform system when entity is added or removed #54

chrisapril opened this issue Sep 2, 2014 · 3 comments

Comments

@chrisapril
Copy link

Hi, is it possible to inform a system when an entity ist removed or added to the system?

Im currently using artemis and i have systems which are building there own index of entites, for example to receive all entities in an spatial area. Those systems needs to update their index when an entity is added or removed.

Thx,
Christian

@dsaltares
Copy link
Member

You can always subscribe your EntitySystem to entity events with the Engine by implementing the EntityListener interface. However, the handler will be called every time any entity enters or leaves the engine.

You can always check whether the added/deleted entity is of interest by doing:

ImmutableArray<Entity> entities = engine.getEntitiesFor(Family.getFor(...));
entities.contains(entity);

Would that be enough?

The EntitySystem class doesn't have an associated family. We could, maybe, add listeners to the engine on a per family basis.

@chrisapril
Copy link
Author

Hi,

that wouldnt be enough. Another problem is that when the listener gets informed, i dont have access to the component anymore (when i have understood the ashley code right).

For example i am doing something like this very often:

BehaviourSystem .....

    @Override
    protected void removed(Entity e) {
        super.removed(e);

        Behaviour b = e.getComponent(BehaviourComponent.class).getBehaviour();
        Task task = b.getCurrentTask();
        if(task != null){
            task.removeApplicant(e);
        }
    }

or i a SteeringForceSystem:

@Override
    protected void removed(Entity e) {
        super.removed(e);

        if (spatialMapper.has(e)) {
            Spatial spatial = spatialMapper.get(e);
            spatial.vel.x = 0;
            spatial.vel.y = 0;
        }

        if (steeringForceMapper.has(e)) {
            SteeringForce sf = steeringForceMapper.get(e);
            sf.steeringForce.set(0, 0);
        }
    }

chrisapril added a commit to chrisapril/ashley that referenced this issue Sep 6, 2014
@dsaltares
Copy link
Member

Added in f90d129

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

No branches or pull requests

2 participants