ecsyjs / ecsy

Entity Component System for javascript
https://ecsyjs.github.io/ecsy/
MIT License
1.11k stars 115 forks source link

Reverse query results iteration by design #184

Open bosconian-dynamics opened 4 years ago

bosconian-dynamics commented 4 years ago

As per #175, mutating the query results array while iterating upon that array (e.g. via entitiy.removeComponent(), or entity.addComponent() in a Not() query) can result in unexpected behaviors, and at the moment it's easy to stumble into those mutations accidentally.

An alternative to documenting the work-around as suggested in #163, a custom object could be used for query results to work around the behavior by substituting iteration methods which iterate in reverse order by default, perform a .slice() prior to iteration, or some other means.

As @fernandojsg mentioned on Discord, any such solution should be benchmarked (pending #178) as using custom objects in place of arrays might come at a high cost.

cfortuner commented 4 years ago

Hey! Just wanted to bump this. I've been working in ecsy for a few months now and I've run into this multiple times. It's really easy to forget and I think many new users will get tripped up by this.

netpro2k commented 4 years ago

IMO reversing iteration is just trying to hide the inherent complexity that queries are tied to entity mutations. There are all sorts of other issues that can come up as well, so we just need better ways of being able to think about that complexity rather than trying to hide it. Unity largely does this by making these mutations happen at more explicit points in the frame using EntityCommandBuffers, I think a similar abstraction could make sense and could also get rid of some of the weirdness and confusion around deferred entity removal.

bosconian-dynamics commented 4 years ago

After thinking on it more over the last few weeks, I've come to agree with @netpro2k's assessment - virtually any implementation adds unnecessary obfuscation.