Artemis - how to delete all entities from world

690 views Asked by At

I want to delete all entities from world. And if a tag or a group is registered for the entity, I want to remove them too. Like there was no such entity at all. It is much like World.delete function, but you should loop on all entities. I can't find a way. And can't believe the designers didn't prepare such functionality for clearing the world from entities. Do I miss something?

1

There are 1 answers

0
junkdog On

There's no default way of deleting all entities from the World - typically this is done by disposing and recreating the world - but it can be easily achieved by adding a custom manager:

public final class EntityTracker extends Manager {
    private Bag<Entity> entities = new Bag<Entity>();

    @Override
    public void added(Entity e) {
        entities.add(e);
    }

    @Override
    public void deleted(Entity e) {
        entities.remove(e);
    }

    public void deleteAllEntities() {
        for (Entity e : entities)
            e.deleteFromWorld();
    }
}

In recent versions of artemis-odb it's easier to use the AspectSubscriptionManager:

    IntBag entities = world.getAspectSubscriptionManager()
        .get(Aspect.all())
        .getEntities();

    int[] ids = entities.getData();
    for (int i = 0, s = entities.size(); s > i; i++) {
        world.delete(ids[i]);
    }

edit: The above code assumes artemis-odb; I'm not sure whether vanilla artemis' TagManager and GroupManager automatically removes entities upon deletion.