diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 6c4bab76ade83..a3218f8ff014f 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -14,7 +14,7 @@ use crate::{ component::{ Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, TickCells, }, - entity::{AllocAtWithoutReplacement, Entities, Entity}, + entity::{AllocAtWithoutReplacement, Entities, Entity, EntityLocation}, query::{QueryState, ReadOnlyWorldQuery, WorldQuery}, storage::{ResourceData, SparseSet, Storages}, system::Resource, @@ -323,11 +323,20 @@ impl World { /// /// This is useful in contexts where you only have read-only access to the [`World`]. #[inline] - pub fn iter_entities(&self) -> impl Iterator + '_ { - self.archetypes - .iter() - .flat_map(|archetype| archetype.entities().iter()) - .map(|archetype_entity| archetype_entity.entity()) + pub fn iter_entities(&self) -> impl Iterator> + '_ { + self.archetypes.iter().flat_map(|archetype| { + archetype + .entities() + .iter() + .enumerate() + .map(|(index, archetype_entity)| { + let location = EntityLocation { + archetype_id: archetype.id(), + index, + }; + EntityRef::new(self, archetype_entity.entity(), location) + }) + }) } /// Retrieves an [`EntityMut`] that exposes read and write operations for the given `entity`. @@ -1870,7 +1879,7 @@ mod tests { let iterate_and_count_entities = |world: &World, entity_counters: &mut HashMap<_, _>| { entity_counters.clear(); for entity in world.iter_entities() { - let counter = entity_counters.entry(entity).or_insert(0); + let counter = entity_counters.entry(entity.id()).or_insert(0); *counter += 1; } }; diff --git a/crates/bevy_scene/src/dynamic_scene.rs b/crates/bevy_scene/src/dynamic_scene.rs index 92f8cae312f4e..c0eb067b2ca8b 100644 --- a/crates/bevy_scene/src/dynamic_scene.rs +++ b/crates/bevy_scene/src/dynamic_scene.rs @@ -46,7 +46,7 @@ impl DynamicScene { let mut builder = DynamicSceneBuilder::from_world_with_type_registry(world, type_registry.clone()); - builder.extract_entities(world.iter_entities()); + builder.extract_entities(world.iter_entities().map(|entity| entity.id())); builder.build() }