From b7be890e18a9fd41c472e14c2c0ab73cd9c8b4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Tue, 29 Jun 2021 00:52:03 +0200 Subject: [PATCH] fix parenting of scenes --- crates/bevy_scene/src/scene_spawner.rs | 16 +++++++++++++++- .../hierarchy/hierarchy_maintenance_system.rs | 8 +++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index b62015c5095e16..933184cfbd391e 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -7,7 +7,7 @@ use bevy_ecs::{ world::{Mut, World}, }; use bevy_reflect::TypeRegistryArc; -use bevy_transform::prelude::Parent; +use bevy_transform::{components::Children, prelude::Parent}; use bevy_utils::{tracing::error, HashMap}; use thiserror::Error; use uuid::Uuid; @@ -268,11 +268,25 @@ impl SceneSpawner { for (instance_id, parent) in scenes_with_parent { 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 if let Some(mut entity_mut) = world.get_entity_mut(entity) { + // This will filter only the scene root entity, as all other from the + // scene have a parent if !entity_mut.contains::() { entity_mut.insert(Parent(parent)); } } + // Add the scene root to the `Children` component on the parent + if let Some(mut parent_entity) = world.get_entity_mut(parent) { + if let Some(children) = parent_entity.get_mut::() { + let children = &**children; + let mut children = children.to_vec(); + children.push(entity); + parent_entity.insert(Children::with(&children)); + } else { + parent_entity.insert(Children::with(&[entity])); + } + } } } else { self.scenes_with_parent.push((instance_id, parent)); diff --git a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs index 541b10744b0eea..4a96f90c60c261 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs @@ -50,11 +50,9 @@ pub fn parent_update_system( // `children_additions`). if let Ok(mut new_parent_children) = children_query.get_mut(parent.0) { // This is the parent - debug_assert!( - !(*new_parent_children).0.contains(&entity), - "children already added" - ); - (*new_parent_children).0.push(entity); + if !(*new_parent_children).0.contains(&entity) { + (*new_parent_children).0.push(entity); + } } else { // The parent doesn't have a children entity, lets add it children_additions