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

Revert Align Scene::write_to_world_with to match DynamicScene::write_to_world_with #13800

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 13 additions & 10 deletions crates/bevy_scene/src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{DynamicScene, SceneSpawnError};
use crate::{DynamicScene, InstanceInfo, SceneSpawnError};
use bevy_asset::Asset;
use bevy_ecs::entity::{Entity, EntityHashMap};
use bevy_ecs::entity::EntityHashMap;
use bevy_ecs::{
reflect::{AppTypeRegistry, ReflectComponent, ReflectMapEntities, ReflectResource},
world::World,
Expand Down Expand Up @@ -43,8 +43,7 @@ impl Scene {
/// provided [`AppTypeRegistry`] or doesn't reflect the [`Component`](bevy_ecs::component::Component) trait.
pub fn clone_with(&self, type_registry: &AppTypeRegistry) -> Result<Scene, SceneSpawnError> {
let mut new_world = World::new();
let mut entity_map = EntityHashMap::default();
self.write_to_world_with(&mut new_world, &mut entity_map, type_registry)?;
self.write_to_world_with(&mut new_world, type_registry)?;
Ok(Self { world: new_world })
}

Expand All @@ -55,9 +54,12 @@ impl Scene {
pub fn write_to_world_with(
&self,
world: &mut World,
entity_map: &mut EntityHashMap<Entity>,
type_registry: &AppTypeRegistry,
) -> Result<(), SceneSpawnError> {
) -> Result<InstanceInfo, SceneSpawnError> {
let mut instance_info = InstanceInfo {
entity_map: EntityHashMap::default(),
};

let type_registry = type_registry.read();

// Resources archetype
Expand Down Expand Up @@ -92,7 +94,8 @@ impl Scene {

for archetype in self.world.archetypes().iter() {
for scene_entity in archetype.entities() {
let entity = entity_map
let entity = *instance_info
.entity_map
.entry(scene_entity.id())
.or_insert_with(|| world.spawn_empty().id());
for component_id in archetype.components() {
Expand All @@ -118,7 +121,7 @@ impl Scene {
&self.world,
world,
scene_entity.id(),
*entity,
entity,
&type_registry,
);
}
Expand All @@ -127,10 +130,10 @@ impl Scene {

for registration in type_registry.iter() {
if let Some(map_entities_reflect) = registration.data::<ReflectMapEntities>() {
map_entities_reflect.map_all_entities(world, entity_map);
map_entities_reflect.map_all_entities(world, &mut instance_info.entity_map);
}
}

Ok(())
Ok(instance_info)
}
}
31 changes: 11 additions & 20 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ impl SceneSpawner {
let scene = scenes
.get(id)
.ok_or(SceneSpawnError::NonExistentScene { id })?;

scene.write_to_world(world, entity_map)
})
}
Expand All @@ -230,33 +229,27 @@ impl SceneSpawner {
pub fn spawn_sync(
&mut self,
world: &mut World,
id: impl Into<AssetId<Scene>>,
id: AssetId<Scene>,
) -> Result<InstanceId, SceneSpawnError> {
let mut entity_map = EntityHashMap::default();
let id = id.into();
Self::spawn_sync_internal(world, id, &mut entity_map)?;
let instance_id = InstanceId::new();
self.spawned_instances
.insert(instance_id, InstanceInfo { entity_map });
Ok(instance_id)
self.spawn_sync_internal(world, id, InstanceId::new())
}

fn spawn_sync_internal(
// &mut self,
&mut self,
world: &mut World,
id: AssetId<Scene>,
entity_map: &mut EntityHashMap<Entity>,
) -> Result<(), SceneSpawnError> {
instance_id: InstanceId,
) -> Result<InstanceId, SceneSpawnError> {
world.resource_scope(|world, scenes: Mut<Assets<Scene>>| {
let scene = scenes
.get(id)
.ok_or(SceneSpawnError::NonExistentRealScene { id })?;

scene.write_to_world_with(
world,
entity_map,
&world.resource::<AppTypeRegistry>().clone(),
)
let instance_info =
scene.write_to_world_with(world, &world.resource::<AppTypeRegistry>().clone())?;

self.spawned_instances.insert(instance_id, instance_info);
Ok(instance_id)
})
}

Expand Down Expand Up @@ -326,9 +319,7 @@ impl SceneSpawner {
let scenes_to_spawn = std::mem::take(&mut self.scenes_to_spawn);

for (scene_handle, instance_id) in scenes_to_spawn {
let mut entity_map = EntityHashMap::default();

match Self::spawn_sync_internal(world, scene_handle.id(), &mut entity_map) {
match self.spawn_sync_internal(world, scene_handle.id(), instance_id) {
Ok(_) => {}
Err(SceneSpawnError::NonExistentRealScene { .. }) => {
self.scenes_to_spawn.push((scene_handle, instance_id));
Expand Down