Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Allow iterating over with EntityRef over the entire World #6843

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/bevy_ecs/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ impl Entities {
}
}

/// Gets the location of an entity without checking the validity of it's index nor it's generation.
james7132 marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Safety
/// `index` must correspond to an allocated entity.
pub(crate) unsafe fn get_unchecked(&self, index: u32) -> EntityLocation {
self.meta.get_unchecked(index as usize).location
}

/// Updates the location of an [`Entity`]. This must be called when moving the components of
/// the entity around in storage.
///
Expand Down
12 changes: 10 additions & 2 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,19 @@ 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<Item = Entity> + '_ {
pub fn iter_entities(&self) -> impl Iterator<Item = EntityRef<'_>> + '_ {
self.archetypes
.iter()
.flat_map(|archetype| archetype.entities().iter())
.map(|archetype_entity| archetype_entity.entity())
.map(|archetype_entity| {
// SAFETY: The entity is already located in a Archetype, which means the entity
// must be properly allocated
let location = unsafe {
self.entities
.get_unchecked(archetype_entity.entity().index())
};
EntityRef::new(self, archetype_entity.entity(), location)
})
}

/// Retrieves an [`EntityMut`] that exposes read and write operations for the given `entity`.
Expand Down
18 changes: 12 additions & 6 deletions crates/bevy_scene/src/dynamic_scene_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{DynamicEntity, DynamicScene};
use bevy_app::AppTypeRegistry;
use bevy_ecs::{prelude::Entity, reflect::ReflectComponent, world::World};
use bevy_ecs::{
reflect::ReflectComponent,
world::{EntityRef, World},
};
use bevy_utils::default;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -67,7 +70,7 @@ impl<'w> DynamicSceneBuilder<'w> {
/// Extract one entity from the builder's [`World`].
///
/// Re-extracting an entity that was already extracted will have no effect.
pub fn extract_entity(&mut self, entity: Entity) -> &mut Self {
pub fn extract_entity<'a>(&mut self, entity: EntityRef<'a>) -> &mut Self {
self.extract_entities(std::iter::once(entity))
}

Expand Down Expand Up @@ -96,11 +99,14 @@ impl<'w> DynamicSceneBuilder<'w> {
/// builder.extract_entities(query.iter(&world));
/// let scene = builder.build();
/// ```
pub fn extract_entities(&mut self, entities: impl Iterator<Item = Entity>) -> &mut Self {
pub fn extract_entities<'a>(
&mut self,
entities: impl Iterator<Item = EntityRef<'a>>,
) -> &mut Self {
let type_registry = self.type_registry.read();

for entity in entities {
let index = entity.index();
let index = entity.id().index();

if self.entities.contains_key(&index) {
continue;
Expand All @@ -111,7 +117,7 @@ impl<'w> DynamicSceneBuilder<'w> {
components: Vec::new(),
};

for component_id in self.world.entity(entity).archetype().components() {
for component_id in entity.archetype().components() {
let reflect_component = self
.world
.components()
Expand All @@ -120,7 +126,7 @@ impl<'w> DynamicSceneBuilder<'w> {
.and_then(|registration| registration.data::<ReflectComponent>());

if let Some(reflect_component) = reflect_component {
if let Some(component) = reflect_component.reflect(self.world, entity) {
if let Some(component) = reflect_component.reflect(self.world, entity.id()) {
entry.components.push(component.clone_value());
}
}
Expand Down