From a272c9f8eb56db8ffe622a32671bc521892bc9a9 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Sat, 29 Apr 2023 14:21:09 +0100 Subject: [PATCH 1/3] check parent --- crates/bevy_scene/src/scene_spawner.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index 5a46aa14e256c..e84fe11516025 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -267,6 +267,12 @@ impl SceneSpawner { let scenes_with_parent = std::mem::take(&mut self.scenes_with_parent); for (instance_id, parent) in scenes_with_parent { + if world.get_entity(parent).is_none() { + // parent has been deleted, despawn the instance + self.instances_to_despawn.push(instance_id); + continue; + } + if let Some(instance) = self.spawned_instances.get(&instance_id) { for entity in instance.entity_map.values() { // Add the `Parent` component to the scene root, and update the `Children` component of From 23bfdc91c44ffb06235277ff73959d3bddf3e2c7 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Sat, 29 Apr 2023 23:06:45 +0100 Subject: [PATCH 2/3] despawn before loading --- crates/bevy_scene/src/scene_spawner.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index e84fe11516025..0381bae7466b5 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -267,12 +267,6 @@ impl SceneSpawner { let scenes_with_parent = std::mem::take(&mut self.scenes_with_parent); for (instance_id, parent) in scenes_with_parent { - if world.get_entity(parent).is_none() { - // parent has been deleted, despawn the instance - self.instances_to_despawn.push(instance_id); - continue; - } - if let Some(instance) = self.spawned_instances.get(&instance_id) { for entity in instance.entity_map.values() { // Add the `Parent` component to the scene root, and update the `Children` component of @@ -322,6 +316,26 @@ impl SceneSpawner { pub fn scene_spawner_system(world: &mut World) { world.resource_scope(|world, mut scene_spawner: Mut| { + // remove any loading instances where parent is deleted + let mut dead_instances = HashSet::default(); + scene_spawner + .scenes_with_parent + .retain(|(instance, parent)| { + let retain = world.get_entity(*parent).is_some(); + + if !retain { + dead_instances.insert(*instance); + } + + retain + }); + scene_spawner + .dynamic_scenes_to_spawn + .retain(|(_, instance)| !dead_instances.contains(instance)); + scene_spawner + .scenes_to_spawn + .retain(|(_, instance)| !dead_instances.contains(instance)); + let scene_asset_events = world.resource::>>(); let mut updated_spawned_scenes = Vec::new(); From 2c87dc9a6affc4f91b801f79bd31441c53404f80 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Sun, 30 Apr 2023 01:27:31 +0100 Subject: [PATCH 3/3] missed an import somehow --- crates/bevy_scene/src/scene_spawner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index 0381bae7466b5..94abfccc39e15 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -8,7 +8,7 @@ use bevy_ecs::{ world::{Mut, World}, }; use bevy_hierarchy::{AddChild, Parent}; -use bevy_utils::{tracing::error, HashMap}; +use bevy_utils::{tracing::error, HashMap, HashSet}; use thiserror::Error; use uuid::Uuid;