Skip to content

Commit

Permalink
fix parenting of scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Jun 28, 2021
1 parent c893b99 commit b7be890
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 15 additions & 1 deletion crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Parent>() {
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::<Children>() {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b7be890

Please sign in to comment.