Skip to content

Commit 6dbe360

Browse files
Add num_entities() to World (#19780)
# 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. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent c6ae964 commit 6dbe360

File tree

6 files changed

+16
-8
lines changed

6 files changed

+16
-8
lines changed

crates/bevy_app/src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ mod tests {
15821582
app.add_systems(EnterMainMenu, (foo, bar));
15831583

15841584
app.world_mut().run_schedule(EnterMainMenu);
1585-
assert_eq!(app.world().entities().len(), 2);
1585+
assert_eq!(app.world().num_entities(), 2);
15861586
}
15871587

15881588
#[test]

crates/bevy_ecs/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ mod tests {
16371637

16381638
assert_eq!(q1.iter(&world).len(), 1);
16391639
assert_eq!(q2.iter(&world).len(), 1);
1640-
assert_eq!(world.entities().len(), 2);
1640+
assert_eq!(world.num_entities(), 2);
16411641

16421642
world.clear_entities();
16431643

@@ -1652,7 +1652,7 @@ mod tests {
16521652
"world should not contain sparse set components"
16531653
);
16541654
assert_eq!(
1655-
world.entities().len(),
1655+
world.num_entities(),
16561656
0,
16571657
"world should not have any entities"
16581658
);

crates/bevy_ecs/src/observer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ mod tests {
13151315
world.spawn(A).flush();
13161316
assert_eq!(vec!["add_2", "add_1"], world.resource::<Order>().0);
13171317
// Our A entity plus our two observers
1318-
assert_eq!(world.entities().len(), 3);
1318+
assert_eq!(world.num_entities(), 3);
13191319
}
13201320

13211321
#[test]

crates/bevy_ecs/src/system/commands/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2342,7 +2342,7 @@ mod tests {
23422342
.spawn((W(1u32), W(2u64)))
23432343
.id();
23442344
command_queue.apply(&mut world);
2345-
assert_eq!(world.entities().len(), 1);
2345+
assert_eq!(world.num_entities(), 1);
23462346
let results = world
23472347
.query::<(&W<u32>, &W<u64>)>()
23482348
.iter(&world)

crates/bevy_ecs/src/world/command_queue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,12 @@ mod test {
423423
let mut world = World::new();
424424
queue.apply(&mut world);
425425

426-
assert_eq!(world.entities().len(), 2);
426+
assert_eq!(world.num_entities(), 2);
427427

428428
// The previous call to `apply` cleared the queue.
429429
// This call should do nothing.
430430
queue.apply(&mut world);
431-
assert_eq!(world.entities().len(), 2);
431+
assert_eq!(world.num_entities(), 2);
432432
}
433433

434434
#[expect(
@@ -462,7 +462,7 @@ mod test {
462462
queue.push(SpawnCommand);
463463
queue.push(SpawnCommand);
464464
queue.apply(&mut world);
465-
assert_eq!(world.entities().len(), 3);
465+
assert_eq!(world.num_entities(), 3);
466466
}
467467

468468
#[test]

crates/bevy_ecs/src/world/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ impl World {
216216
&mut self.entities
217217
}
218218

219+
/// Retrieves the number of [`Entities`] in the world.
220+
///
221+
/// This is helpful as a diagnostic, but it can also be used effectively in tests.
222+
#[inline]
223+
pub fn num_entities(&self) -> u32 {
224+
self.entities.len()
225+
}
226+
219227
/// Retrieves this world's [`Archetypes`] collection.
220228
#[inline]
221229
pub fn archetypes(&self) -> &Archetypes {

0 commit comments

Comments
 (0)