How to detect if an entity has been removed?

I have an entity that must be removed when another entity is removed.
Code here:

public class DecayTogetherProcessor extends BaseAppState {

    private EntityData ed;
    private EntitySet entities;

    @Override
    protected void initialize(Application app) {
        ed = getState(EntityDataState.class).getEntityData();
        entities = ed.getEntities(DecayTogether.class);
    }

    @Override
    protected void cleanup(Application app) {
        // Release the entity set we grabbed previously
        entities.release();
        entities = null;
    }

    @Override
    protected void enable() {
    }

    @Override
    protected void disable() {
    }

    @Override
    public void update(float tpf) {
        entities.applyChanges();
        for (Entity e : entities) {
            EntityId parent = e.get(DecayTogether.class).getStickedTo();
            //not possible?
            if (ed.isRemoved(parent)) {
                ed.removeEntity(e.getId());
            }
        }
    }
}

Thanks!

???

if( entities.applyChanges() ) {
// Do something with entities.getRemovedEntities()
}

Edit: EntitySet (zay-es 1.5.0 API)

Edit 2: Oh, I see you are trying to go backwards… then probably you are thinking backwards.

Better explain what you are really trying to do.

I have a hitbox, and a “spatial” (not actually a spatial) that is actually made of multiple particles.

So I have an entity for the hitbox, and an entity for each particle.

I want that when the hitbox is removed, the particles are notified and disappear as well.

Then when the hitbox is removed, iterate over all of the particles that are connected to it and remove them.

I thought the way explained on the first post was the best way

        ed.setComponents(hitbox,
                new Position(pos, Quaternion.ZERO),
                new Velocity(new Vector3f(-500, 0, 0)),
//...
);
        for (int i = 0; i < 50; i++) {
            Vector3f bpos = pos.clone();
            bpos.x += SPRITE_SIZE * (i * .02f - .3f);
            bpos.y += SPRITE_SIZE * ((float) Math.random() * .12f - .0f);
            ed.setComponents(ed.createEntity(),
                    new Position(bpos, Quaternion.ZERO),
                    new Velocity(new Vector3f(-500, 0, 0)),
                    new DecayTogether(hitbox)
            );

Or you mean that I should make a component on the hitbox that keeps track of all the “children”?
Besides, is it an antipattern to query if an entity is removed?

No. Lists are bad.

Detect that the hitbox is removed. Then iterate over all entities with “DecayTogether(hitbox)” and remove them.

It still feels weird to me, though. Like something is backwards.

What is this hitbox for?

It is a projectile. Think space invaders… not really rocket science. Oh wait, maybe it is, but that’s beyond the scope :slight_smile:

I mean… what kind of “thing” is it where as soon as it’s hitbox goes away all of the particles instantly vanish?

And why are those particles separate entities instead of just a visual effect?

Anyway… because I’m heading to bed and I don’t want to leave you hanging… if you really must persist down this path you are on… and it feels very weird and so hopefully there is a non-weird way…

You can remember that an entity is never really “removed”. It just may not have the components you are interested in. For example, your ed.isRemoved(parent) call could just as easily be replaced with a ed.getComponent(parent, Position.class) == null.

That’s the only thing you can know: this entity doesn’t have the component I’m interested in. It’s not “removed”. It’s just an unfortunate convenience that there is a remove() method that will clear all of the known components.

1 Like

Ok thanks.

As you can see from the code, each has a separate random position, so I can’t just slap together a “Model” and a “hitbox” component on the same entity.
Why should I manage these particles without an ES? I see no benefit.

Maybe one day I’ll upgrade the visuals and use a different approach altogether, but for now that’s it.

Are the particles game objects? Do they interact with other entities?

Or are they just visual particles?

For example, other things I would not model with an ES:
-hair animation
-sparks from an anvil
-missile trails

These are purely visual effects.

…so I should use a Control that reads the position of the ES hitbox and manages the particles, instead of managing the position from ES?

Yeah, or even a standard JME particle control or whatever.

I mean, to me, if this is a client-server game then the server doesn’t really care to send status updates for every little visual particle if it’s never going to interact with a game object.