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

Use handles for queued scenes in SceneSpawner #10619

Merged
merged 1 commit into from
Nov 18, 2023
Merged
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
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