From 48d10e6d48d0a19ffa32aa195093bfcf8d3a5df0 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Fri, 17 Nov 2023 17:27:05 -0800 Subject: [PATCH] Use handles for queued scenes in SceneSpawner (#10619) # Objective Fixes #10482 ## Solution Store Handles instead of AssetIds for queued Scenes and DynamicScenes in SceneSpawner. --- crates/bevy_scene/src/scene_spawner.rs | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index 11192337b6..f8908736dc 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -1,5 +1,5 @@ use crate::{DynamicScene, Scene}; -use bevy_asset::{AssetEvent, AssetId, Assets}; +use bevy_asset::{AssetEvent, AssetId, Assets, Handle}; use bevy_ecs::{ entity::Entity, event::{Event, Events, ManualEventReader}, @@ -63,8 +63,8 @@ pub struct SceneSpawner { spawned_dynamic_scenes: HashMap, Vec>, spawned_instances: HashMap, scene_asset_event_reader: ManualEventReader>, - dynamic_scenes_to_spawn: Vec<(AssetId, InstanceId)>, - scenes_to_spawn: Vec<(AssetId, InstanceId)>, + dynamic_scenes_to_spawn: Vec<(Handle, InstanceId)>, + scenes_to_spawn: Vec<(Handle, InstanceId)>, scenes_to_despawn: Vec>, instances_to_despawn: Vec, scenes_with_parent: Vec<(InstanceId, Entity)>, @@ -127,7 +127,7 @@ pub enum SceneSpawnError { impl SceneSpawner { /// Schedule the spawn of a new instance of the provided dynamic scene. - pub fn spawn_dynamic(&mut self, id: impl Into>) -> InstanceId { + pub fn spawn_dynamic(&mut self, id: impl Into>) -> InstanceId { let instance_id = InstanceId::new(); self.dynamic_scenes_to_spawn.push((id.into(), instance_id)); instance_id @@ -136,7 +136,7 @@ impl SceneSpawner { /// Schedule the spawn of a new instance of the provided dynamic scene as a child of `parent`. pub fn spawn_dynamic_as_child( &mut self, - id: impl Into>, + id: impl Into>, parent: Entity, ) -> InstanceId { let instance_id = InstanceId::new(); @@ -146,14 +146,14 @@ impl SceneSpawner { } /// Schedule the spawn of a new instance of the provided scene. - pub fn spawn(&mut self, id: impl Into>) -> InstanceId { + pub fn spawn(&mut self, id: impl Into>) -> InstanceId { let instance_id = InstanceId::new(); self.scenes_to_spawn.push((id.into(), instance_id)); instance_id } /// Schedule the spawn of a new instance of the provided scene as a child of `parent`. - pub fn spawn_as_child(&mut self, id: impl Into>, parent: Entity) -> InstanceId { + pub fn spawn_as_child(&mut self, id: impl Into>, parent: Entity) -> InstanceId { let instance_id = InstanceId::new(); self.scenes_to_spawn.push((id.into(), instance_id)); self.scenes_with_parent.push((instance_id, parent)); @@ -296,21 +296,21 @@ impl SceneSpawner { pub fn spawn_queued_scenes(&mut self, world: &mut World) -> Result<(), SceneSpawnError> { let scenes_to_spawn = std::mem::take(&mut self.dynamic_scenes_to_spawn); - for (id, instance_id) in scenes_to_spawn { + for (handle, instance_id) in scenes_to_spawn { let mut entity_map = EntityHashMap::default(); - match Self::spawn_dynamic_internal(world, id, &mut entity_map) { + match Self::spawn_dynamic_internal(world, handle.id(), &mut entity_map) { Ok(_) => { self.spawned_instances .insert(instance_id, InstanceInfo { entity_map }); let spawned = self .spawned_dynamic_scenes - .entry(id) + .entry(handle.id()) .or_insert_with(Vec::new); spawned.push(instance_id); } Err(SceneSpawnError::NonExistentScene { .. }) => { - self.dynamic_scenes_to_spawn.push((id, instance_id)); + self.dynamic_scenes_to_spawn.push((handle, instance_id)); } Err(err) => return Err(err), } @@ -319,10 +319,10 @@ impl SceneSpawner { let scenes_to_spawn = std::mem::take(&mut self.scenes_to_spawn); for (scene_handle, instance_id) in scenes_to_spawn { - match self.spawn_sync_internal(world, scene_handle, instance_id) { + match self.spawn_sync_internal(world, scene_handle.id(), instance_id) { Ok(_) => {} - Err(SceneSpawnError::NonExistentRealScene { id: handle }) => { - self.scenes_to_spawn.push((handle, instance_id)); + Err(SceneSpawnError::NonExistentRealScene { .. }) => { + self.scenes_to_spawn.push((scene_handle, instance_id)); } Err(err) => return Err(err), }