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

Exception when adding an entity in EntityListener.entityRemoved #81

Closed
wingman87 opened this issue Oct 12, 2014 · 1 comment
Closed
Labels

Comments

@wingman87
Copy link

Hi all, this time I've created a JUnit test to be more helpful:

package wingman.tests;

import org.junit.Test;

import com.badlogic.ashley.core.Component;
import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntityListener;
import com.badlogic.ashley.core.Family;

public class AshleyTests {

    @Test
    public void addEntityListener_Family_Remove() {
        final Engine engine = new Engine();

        Entity e = new Entity();
        e.add(new PositionComponent());
        engine.addEntity(e);

        @SuppressWarnings("unchecked")
        Family family = Family.getFor(PositionComponent.class);
        engine.addEntityListener(family, new EntityListener() {

            public void entityRemoved(Entity entity) {
                engine.addEntity(new Entity());
            }

            public void entityAdded(Entity entity) {

            }
        });

        engine.removeEntity(e);
    }

    @Test
    public void addEntityListener_Family_Add() {
        final Engine engine = new Engine();

        Entity e = new Entity();
        e.add(new PositionComponent());

        @SuppressWarnings("unchecked")
        Family family = Family.getFor(PositionComponent.class);
        engine.addEntityListener(family, new EntityListener() {

            public void entityRemoved(Entity entity) {

            }

            public void entityAdded(Entity entity) {
                engine.addEntity(new Entity());
            }
        });

        engine.addEntity(e);
    }

    @Test
    public void addEntityListener_NoFamily_Remove() {
        final Engine engine = new Engine();

        Entity e = new Entity();
        e.add(new PositionComponent());
        engine.addEntity(e);

        @SuppressWarnings("unchecked")
        final Family family = Family.getFor(PositionComponent.class);
        engine.addEntityListener(new EntityListener() {

            public void entityRemoved(Entity entity) {
                if(family.matches(entity))
                    engine.addEntity(new Entity());
            }

            public void entityAdded(Entity entity) {

            }
        });

        engine.removeEntity(e);
    }

    @Test
    public void addEntityListener_NoFamily_Add() {
        final Engine engine = new Engine();

        Entity e = new Entity();
        e.add(new PositionComponent());

        @SuppressWarnings("unchecked")
        final Family family = Family.getFor(PositionComponent.class);
        engine.addEntityListener(new EntityListener() {

            public void entityRemoved(Entity entity) {

            }

            public void entityAdded(Entity entity) {
                if(family.matches(entity))
                    engine.addEntity(new Entity());
            }
        });

        engine.addEntity(e);
    }

    public class PositionComponent extends Component { }

}

If you run this tests, the addEntityListener_Family_Remove (and _Add) will fail with the following exception:

com.badlogic.gdx.utils.GdxRuntimeException: #iterator() cannot be used nested.
    at com.badlogic.gdx.utils.ObjectMap$Entries.hasNext(ObjectMap.java:690)
    at com.badlogic.ashley.core.Engine.removeEntityInternal(Engine.java:285)
    at com.badlogic.ashley.core.Engine.removeEntity(Engine.java:130)
    at wingman.tests.AshleyTests.addEntityListener_Family(AshleyTests.java:34)

As you can see the addEntityListener_NoFamily_Remove (and _Add) will not fail so I think the exception must be related to how the families are iterated when handling the entity removal event.

@dsaltares dsaltares added the bug label Oct 12, 2014
@dsaltares
Copy link
Member

Thanks for reporting and the JUnit test. I could reproduce the issue on my end and will look into a fix.

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

No branches or pull requests

2 participants