Skip to content

Conversation

Trashtalk217
Copy link
Contributor

@Trashtalk217 Trashtalk217 commented Jun 22, 2025

Objective

There is a lot of world.entities().len(), especially in tests. In tests, usually, the assumption is made that empty worlds do not contain any entities. This is about to change (#19711), and as such all of these tests are failing for that PR.

Solution

num_entities is a convenience method that returns the number of entities inside a world. It can later be adapted to exclude 'unexpected' entities, associated with internal data structures such as Resources, Queries, Systems. In general I argue for a separation of concepts where World ignores internal entities in methods such as iter_entities() and clear_entities(), that discussion is, however, separate from this PR.

Testing

I replaced most occurrences of world.entities().len() with world.num_entities() and the tests passed.

@Trashtalk217 Trashtalk217 added D-Trivial Nice and easy! A great choice to get started with Bevy A-ECS Entities, components, systems, and events S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 22, 2025
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left an extension to the docs, but I agree with this direction.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 22, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jun 22, 2025
Merged via the queue into bevyengine:main with commit 6dbe360 Jun 22, 2025
32 checks passed
@mgi388
Copy link
Contributor

mgi388 commented Jun 23, 2025

@alice-i-cecile IMO we arguably should prefer naming things foo_count rather than num_foos. This choice probably stems from the fact that "num foos" is sometimes shortened to "no foos" (as in "# foos"), and this can then manifest as no_foos and it reads as "there are no foos???". Hence foo_count.

There's only a handful for functions starting with num_:

image

But more end with _count:

image

@alice-i-cecile
Copy link
Member

Yeah, I'm down with that. If you submit a rename PR I'll approve it :)

github-merge-queue bot pushed a commit that referenced this pull request Jun 23, 2025
Trashtalk217 added a commit to Trashtalk217/bevy that referenced this pull request Jul 10, 2025
There is a lot of `world.entities().len()`, especially in tests. In
tests, usually, the assumption is made that empty worlds do not contain
any entities. This is about to change (bevyengine#19711), and as such all of these
tests are failing for that PR.

`num_entities` is a convenience method that returns the number of
entities inside a world. It can later be adapted to exclude 'unexpected'
entities, associated with internal data structures such as Resources,
Queries, Systems. In general I argue for a separation of concepts where
`World` ignores internal entities in methods such as `iter_entities()`
and `clear_entities()`, that discussion is, however, separate from this
PR.

I replaced most occurrences of `world.entities().len()` with
`world.num_entities()` and the tests passed.

---------

Co-authored-by: Alice Cecile <[email protected]>
Trashtalk217 pushed a commit to Trashtalk217/bevy that referenced this pull request Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events D-Trivial Nice and easy! A great choice to get started with Bevy S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants