NateTheGreatt / bitECS

Flexible, minimal, data-oriented ECS library for Typescript
Mozilla Public License 2.0
942 stars 84 forks source link

API request: getAllEntities(world) - and unexpected behavior with empty queries #77

Closed vegeta897 closed 2 years ago

vegeta897 commented 2 years ago

There are some cases where I want to simply iterate over all existing entities in a world.

Lacking a function in the API, my first thought was using an "empty" query.

Since the typings for defineQuery require an array, I passed in an empty one []

I haven't tested this thoroughly, but calling removeEntity while iterating this query has strange behavior. The first run through, it goes fine. After adding some entities back to the world, running the query again will only iterate entities that weren't deleted in the first query call. None of the new entities show up.

When calling defineQuery without the [] argument, it works much better. However, the array returned by this query will be mutated as you delete entities, so you need to clone it first if you want to get every entity (or maybe a while(entities[0]) loop works). This is different than queries with components, which defer the removal.

I'd rather avoid these unexpected behaviors entirely by having a simple function to get all my entities, and have it return an array that will not be mutated by removeEntity.

NateTheGreatt commented 2 years ago

done! 0.3.37