Use handles for queued scenes in SceneSpawner (#10619)
Fixes #10482 Store Handles instead of AssetIds for queued Scenes and DynamicScenes in SceneSpawner.
This commit is contained in:
parent
c458093cc3
commit
24b4c517d0
@ -1,5 +1,5 @@
|
|||||||
use crate::{DynamicScene, Scene};
|
use crate::{DynamicScene, Scene};
|
||||||
use bevy_asset::{AssetEvent, AssetId, Assets};
|
use bevy_asset::{AssetEvent, AssetId, Assets, Handle};
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
event::{Event, Events, ManualEventReader},
|
event::{Event, Events, ManualEventReader},
|
||||||
@ -63,8 +63,8 @@ pub struct SceneSpawner {
|
|||||||
spawned_dynamic_scenes: HashMap<AssetId<DynamicScene>, Vec<InstanceId>>,
|
spawned_dynamic_scenes: HashMap<AssetId<DynamicScene>, Vec<InstanceId>>,
|
||||||
spawned_instances: HashMap<InstanceId, InstanceInfo>,
|
spawned_instances: HashMap<InstanceId, InstanceInfo>,
|
||||||
scene_asset_event_reader: ManualEventReader<AssetEvent<DynamicScene>>,
|
scene_asset_event_reader: ManualEventReader<AssetEvent<DynamicScene>>,
|
||||||
dynamic_scenes_to_spawn: Vec<(AssetId<DynamicScene>, InstanceId)>,
|
dynamic_scenes_to_spawn: Vec<(Handle<DynamicScene>, InstanceId)>,
|
||||||
scenes_to_spawn: Vec<(AssetId<Scene>, InstanceId)>,
|
scenes_to_spawn: Vec<(Handle<Scene>, InstanceId)>,
|
||||||
scenes_to_despawn: Vec<AssetId<DynamicScene>>,
|
scenes_to_despawn: Vec<AssetId<DynamicScene>>,
|
||||||
instances_to_despawn: Vec<InstanceId>,
|
instances_to_despawn: Vec<InstanceId>,
|
||||||
scenes_with_parent: Vec<(InstanceId, Entity)>,
|
scenes_with_parent: Vec<(InstanceId, Entity)>,
|
||||||
@ -127,7 +127,7 @@ pub enum SceneSpawnError {
|
|||||||
|
|
||||||
impl SceneSpawner {
|
impl SceneSpawner {
|
||||||
/// Schedule the spawn of a new instance of the provided dynamic scene.
|
/// Schedule the spawn of a new instance of the provided dynamic scene.
|
||||||
pub fn spawn_dynamic(&mut self, id: impl Into<AssetId<DynamicScene>>) -> InstanceId {
|
pub fn spawn_dynamic(&mut self, id: impl Into<Handle<DynamicScene>>) -> InstanceId {
|
||||||
let instance_id = InstanceId::new();
|
let instance_id = InstanceId::new();
|
||||||
self.dynamic_scenes_to_spawn.push((id.into(), instance_id));
|
self.dynamic_scenes_to_spawn.push((id.into(), instance_id));
|
||||||
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`.
|
/// Schedule the spawn of a new instance of the provided dynamic scene as a child of `parent`.
|
||||||
pub fn spawn_dynamic_as_child(
|
pub fn spawn_dynamic_as_child(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: impl Into<AssetId<DynamicScene>>,
|
id: impl Into<Handle<DynamicScene>>,
|
||||||
parent: Entity,
|
parent: Entity,
|
||||||
) -> InstanceId {
|
) -> InstanceId {
|
||||||
let instance_id = InstanceId::new();
|
let instance_id = InstanceId::new();
|
||||||
@ -146,14 +146,14 @@ impl SceneSpawner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Schedule the spawn of a new instance of the provided scene.
|
/// Schedule the spawn of a new instance of the provided scene.
|
||||||
pub fn spawn(&mut self, id: impl Into<AssetId<Scene>>) -> InstanceId {
|
pub fn spawn(&mut self, id: impl Into<Handle<Scene>>) -> InstanceId {
|
||||||
let instance_id = InstanceId::new();
|
let instance_id = InstanceId::new();
|
||||||
self.scenes_to_spawn.push((id.into(), instance_id));
|
self.scenes_to_spawn.push((id.into(), instance_id));
|
||||||
instance_id
|
instance_id
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Schedule the spawn of a new instance of the provided scene as a child of `parent`.
|
/// 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<AssetId<Scene>>, parent: Entity) -> InstanceId {
|
pub fn spawn_as_child(&mut self, id: impl Into<Handle<Scene>>, parent: Entity) -> InstanceId {
|
||||||
let instance_id = InstanceId::new();
|
let instance_id = InstanceId::new();
|
||||||
self.scenes_to_spawn.push((id.into(), instance_id));
|
self.scenes_to_spawn.push((id.into(), instance_id));
|
||||||
self.scenes_with_parent.push((instance_id, parent));
|
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> {
|
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);
|
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 = HashMap::default();
|
let mut entity_map = HashMap::default();
|
||||||
|
|
||||||
match Self::spawn_dynamic_internal(world, id, &mut entity_map) {
|
match Self::spawn_dynamic_internal(world, handle.id(), &mut entity_map) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
self.spawned_instances
|
self.spawned_instances
|
||||||
.insert(instance_id, InstanceInfo { entity_map });
|
.insert(instance_id, InstanceInfo { entity_map });
|
||||||
let spawned = self
|
let spawned = self
|
||||||
.spawned_dynamic_scenes
|
.spawned_dynamic_scenes
|
||||||
.entry(id)
|
.entry(handle.id())
|
||||||
.or_insert_with(Vec::new);
|
.or_insert_with(Vec::new);
|
||||||
spawned.push(instance_id);
|
spawned.push(instance_id);
|
||||||
}
|
}
|
||||||
Err(SceneSpawnError::NonExistentScene { .. }) => {
|
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),
|
Err(err) => return Err(err),
|
||||||
}
|
}
|
||||||
@ -319,10 +319,10 @@ impl SceneSpawner {
|
|||||||
let scenes_to_spawn = std::mem::take(&mut self.scenes_to_spawn);
|
let scenes_to_spawn = std::mem::take(&mut self.scenes_to_spawn);
|
||||||
|
|
||||||
for (scene_handle, instance_id) in 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(_) => {}
|
Ok(_) => {}
|
||||||
Err(SceneSpawnError::NonExistentRealScene { id: handle }) => {
|
Err(SceneSpawnError::NonExistentRealScene { .. }) => {
|
||||||
self.scenes_to_spawn.push((handle, instance_id));
|
self.scenes_to_spawn.push((scene_handle, instance_id));
|
||||||
}
|
}
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user