Skip to content

Commit

Permalink
Use handles for queued scenes in SceneSpawner (bevyengine#10619)
Browse files Browse the repository at this point in the history
# Objective

Fixes bevyengine#10482

## Solution

Store Handles instead of AssetIds for queued Scenes and DynamicScenes in
SceneSpawner.
  • Loading branch information
cart authored and Ray Redondo committed Jan 9, 2024
1 parent 4d84f79 commit a02cb76
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -63,8 +63,8 @@ pub struct SceneSpawner {
spawned_dynamic_scenes: HashMap<AssetId<DynamicScene>, Vec<InstanceId>>,
spawned_instances: HashMap<InstanceId, InstanceInfo>,
scene_asset_event_reader: ManualEventReader<AssetEvent<DynamicScene>>,
dynamic_scenes_to_spawn: Vec<(AssetId<DynamicScene>, InstanceId)>,
scenes_to_spawn: Vec<(AssetId<Scene>, InstanceId)>,
dynamic_scenes_to_spawn: Vec<(Handle<DynamicScene>, InstanceId)>,
scenes_to_spawn: Vec<(Handle<Scene>, InstanceId)>,
scenes_to_despawn: Vec<AssetId<DynamicScene>>,
instances_to_despawn: Vec<InstanceId>,
scenes_with_parent: Vec<(InstanceId, Entity)>,
Expand Down Expand Up @@ -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<AssetId<DynamicScene>>) -> InstanceId {
pub fn spawn_dynamic(&mut self, id: impl Into<Handle<DynamicScene>>) -> InstanceId {
let instance_id = InstanceId::new();
self.dynamic_scenes_to_spawn.push((id.into(), instance_id));
instance_id
Expand All @@ -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<AssetId<DynamicScene>>,
id: impl Into<Handle<DynamicScene>>,
parent: Entity,
) -> InstanceId {
let instance_id = InstanceId::new();
Expand All @@ -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<AssetId<Scene>>) -> InstanceId {
pub fn spawn(&mut self, id: impl Into<Handle<Scene>>) -> 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<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();
self.scenes_to_spawn.push((id.into(), instance_id));
self.scenes_with_parent.push((instance_id, parent));
Expand Down Expand Up @@ -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),
}
Expand All @@ -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),
}
Expand Down

0 comments on commit a02cb76

Please sign in to comment.