Skip to content

Commit aa2a590

Browse files
james7132ItsDoot
authored andcommitted
Allow iterating over with EntityRef over the entire World (bevyengine#6843)
# Objective Partially addresses bevyengine#5504. Allow users to get an `Iterator<Item = EntityRef<'a>>` over all entities in the `World`. ## Solution Change `World::iter_entities` to return an iterator of `EntityRef` instead of `Entity`. Not sure how to tackle making an `Iterator<Item = EntityMut<'_>>` without being horribly unsound. Might need to wait for `LendingIterator` to stabilize so we can ensure only one of them is valid at a given time. --- ## Changelog Changed: `World::iter_entities` now returns an iterator of `EntityRef` instead of `Entity`.
1 parent e177ae4 commit aa2a590

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

crates/bevy_ecs/src/world/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
component::{
1515
Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, TickCells,
1616
},
17-
entity::{AllocAtWithoutReplacement, Entities, Entity},
17+
entity::{AllocAtWithoutReplacement, Entities, Entity, EntityLocation},
1818
query::{QueryState, ReadOnlyWorldQuery, WorldQuery},
1919
storage::{ResourceData, SparseSet, Storages},
2020
system::Resource,
@@ -323,11 +323,20 @@ impl World {
323323
///
324324
/// This is useful in contexts where you only have read-only access to the [`World`].
325325
#[inline]
326-
pub fn iter_entities(&self) -> impl Iterator<Item = Entity> + '_ {
327-
self.archetypes
328-
.iter()
329-
.flat_map(|archetype| archetype.entities().iter())
330-
.map(|archetype_entity| archetype_entity.entity())
326+
pub fn iter_entities(&self) -> impl Iterator<Item = EntityRef<'_>> + '_ {
327+
self.archetypes.iter().flat_map(|archetype| {
328+
archetype
329+
.entities()
330+
.iter()
331+
.enumerate()
332+
.map(|(index, archetype_entity)| {
333+
let location = EntityLocation {
334+
archetype_id: archetype.id(),
335+
index,
336+
};
337+
EntityRef::new(self, archetype_entity.entity(), location)
338+
})
339+
})
331340
}
332341

333342
/// Retrieves an [`EntityMut`] that exposes read and write operations for the given `entity`.
@@ -1870,7 +1879,7 @@ mod tests {
18701879
let iterate_and_count_entities = |world: &World, entity_counters: &mut HashMap<_, _>| {
18711880
entity_counters.clear();
18721881
for entity in world.iter_entities() {
1873-
let counter = entity_counters.entry(entity).or_insert(0);
1882+
let counter = entity_counters.entry(entity.id()).or_insert(0);
18741883
*counter += 1;
18751884
}
18761885
};

crates/bevy_scene/src/dynamic_scene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl DynamicScene {
4646
let mut builder =
4747
DynamicSceneBuilder::from_world_with_type_registry(world, type_registry.clone());
4848

49-
builder.extract_entities(world.iter_entities());
49+
builder.extract_entities(world.iter_entities().map(|entity| entity.id()));
5050

5151
builder.build()
5252
}

0 commit comments

Comments
 (0)