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

Engine.getEntitiesFor(Family) returning non-matching entities #214

Open
Barryrowe opened this issue Mar 28, 2016 · 6 comments
Open

Engine.getEntitiesFor(Family) returning non-matching entities #214

Barryrowe opened this issue Mar 28, 2016 · 6 comments
Labels

Comments

@Barryrowe
Copy link
Contributor

I've been working with Ashley 1.7.0 for a while and it's been running smooth. I just upgraded to 1.7.2 and now I am getting a NullPointer in the code below. When I get the FollowerComponent from an entity in the list of engine.getEntitiesFor(Family.all(FollowerComponent.class).get()), the FollowerCompnent is null.

This shouldn't be possible correct? All of the entities in the returned array should be guaranteed to have the component's required by the Family.

 EntityListener el;
    @Override
    public void addedToEngine(Engine engine) {
        super.addedToEngine(engine);

        final Engine eg = engine;
        if(el == null){
            el = new EntityListener() {
                private ComponentMapper<FollowerComponent> fm = ComponentMapper.getFor(FollowerComponent.class);

                public void entityAdded(Entity entity) {

                }

                @Override
                public void entityRemoved(Entity entity) {
                    for (Entity follower : eg.getEntitiesFor(Family.all(FollowerComponent.class).get())) {
                        FollowerComponent fc = fm.get(follower);
                       if (fc.target == entity) {  ///<<<<------ error occurs here
                            fc.target = null;
                            follower.removeAll();
                            eg.removeEntity(follower);
                        }
                    }

                }
            };
        }

        engine.addEntityListener(Family.all(EnemyComponent.class).get(), el);
    }
@dsaltares
Copy link
Member

Could you please provide a jUnit test that highlights the issue? That would make it a lot easier to reproduce and fix. Thanks a lot!

@Barryrowe
Copy link
Contributor Author

I'll work on a repro for this soon. I wish I hadn't found it in the midst of "Jam Code" so it was easier to extract :)

@dsaltares
Copy link
Member

If you don't mind and having limited time these days, I will wait for the test case for this potential bug. Thanks for reporting it though!

@Barryrowe
Copy link
Contributor Author

No problem. I'll try to get a unit test or at least a small example project that presents the issue this weekend.

The issue exists and is reproducible in this project, but there's quite a bit of code in there, and as the name shows it was started for libgdxjam, so a lot of it is probably a mess:
https://github.com/RoaringCatGames/libgdxjam-2015/tree/laxversion

The System where I encountered the bug is:
https://github.com/RoaringCatGames/libgdxjam-2015/blob/laxversion/core/src/com/roaringcatgames/libgdxjam/systems/FollowerSystem.java

Line 52 is where I had to null check to protect against this issue after the upgrade.

Barryrowe added a commit to Barryrowe/ashley that referenced this issue Apr 10, 2016
Adding tests that should recreate the issue. Original issue must be
somewhere else in the project original found in.
Barryrowe added a commit to Barryrowe/ashley that referenced this issue Apr 11, 2016
Issue number libgdx#214 is recreated in this new unit test if the commented line
is uncommented. Line 518 in PooledEngineTests.java
@Barryrowe
Copy link
Contributor Author

I was able to create a unit test to highlight this. PR #217

If I call Entity.removeAll(), and then engine.remove(entity) in that block, I get instances where the engine.getEntitiesFor(Family) returns a component that shouldn't match the family any longer.

I don't know if this counts as a bug now, or just bad usage on my part. It seems Entity.removeAll is redundant if you are removing the entity anyway, but not knowing for certain, I was trying to be safe.

If I comment out the line that removes the components, I don't get any bad hits. The unit test I've created will have the line commented out so that all tests pass for anyone else that might get this while it's being worked.

@dsaltares
Copy link
Member

I think this is related, although maybe not the same as #161, which has been around for a while, removeAll() is not atomic and can cause issues if combined with other entity operations before the next time pending operations are processed.

No one has come up with a nice, clean solution so far. I'm happy to hear new proposals though, as I'd like Ashley to be as robust as possible.

@dsaltares dsaltares added the bug label Jul 19, 2016
0XDE57 added a commit to 0XDE57/SpaceProject that referenced this issue Nov 23, 2017
Family is not picking up entities when recreating engine to swithc
contexts. Possibly related to issue 214:
libgdx/ashley#214

-ignore local properties
@libgdx libgdx deleted a comment from steelx Jul 9, 2022
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