samedi 21 avril 2018

Entity Component System removing entity

This is my first attempt to implement Entity Component System in my project and I'm not sure how some of its mechanics works. For example do I remove an entity? Since all systems are using entities list throughout whole game loop, every attempt of deleting element of that list is condemned to ConcurrentModificationException. Going by this advice I've tried to setting some kind of "toRemove" flag for entities and look for it every time system iterate through list

public class DrawingSystem extends System {

    public DrawingSystem(List<Entity> entityList) {
        super(entityList);
    }

    public void update(Batch batch) {
        for (Entity entity : entityList) {
            removeIfNeccesarry(entity);
            //code

            }
        }

        public void removeIfNeccesarry(Entity entity){
            if(entity.toRemove){
                entityList.remove(entity);
            }
        }

    }

but that didn't help getting rid of the exception. I'm sure there is a elegant solution to this problem since this design pattern is broadly used but I'm just not aware of it.

Aucun commentaire:

Enregistrer un commentaire