-
Notifications
You must be signed in to change notification settings - Fork 144
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
Calling engine.removeEntity() on item in ImmutableArray from Engine.getEntitiesFor mutates array #224
Comments
We are not doing snapshots on these immutable arrays. They are immutable in the sense that they cannot be used to affect the state of the engine. However, their state can, and will change, whenever the engine does. Maybe adding that to the wiki/docs would be useful? |
Ok thanks. I think a note in the docs would be good, as just reading the type name, I wouldn't have expected the behavior that is intended. I wonder if changing the name to something like "ManagedArray" instead of ImmutableArray might make that clearer as well? I'm not sure if there are other use cases for this, and allowing leaking of entity references may be enough to shoot this down, but how would you feel about a |
Document the fact that the Engine may modify the underlying contents of the ImmutableArray that is returned from Engine.getEntities. This addresses issue libgdx#224
I was looking through this, and while I submitted a pull request to add documentation around on the Engine.getEntities method, it seems like in the long run implementing snapshots seems like it would prevent confusion and unexpected behavior. |
I ran into the same problem, when I tried to remove all entities of a family. My workaround is (shamelessly borrowed from ImmutableArray<Entity> entities = engine.getEntitiesFor(family);
while (entities.size() > 0) {
engine.removeEntity(entities.first());
} I suggest to add the method I agree that the name ImmutableArray is misleading. In my opinion something like ReadOnlyArray would be better, since this doesn't indicate that the content never changes. |
@LennartH that sounds good, do you wanna send a PR for that? |
FWIW, java.util.collections has similar wrappers, designated using the term "unmodifiable" |
Use Case:
Code that fails:
The above code runs without error, but only half of of the entities are removed, as it apperas the underlying Array<> is modified immediately on the removeEntity() call, which appears to cause the iterator to adjust positions.
Work Around for now:
This caught me by surprise, as I expected to be able to trust the ImmutableArray to remain unchanged. I have not tested this same code inside of and update of an EntitySystem, I'm currently doing this on an
InputProcessor
keydown event. I'm not sure if this is the expected behavior outside of a systemupdate()
method.The text was updated successfully, but these errors were encountered: