Skip to content

Commit 6fc5c49

Browse files
committed
uses an AddChild command
1 parent e57781a commit 6fc5c49

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

crates/bevy_scene/src/scene_spawner.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use bevy_asset::{AssetEvent, Assets, Handle};
44
use bevy_ecs::{
55
entity::{Entity, EntityMap},
66
reflect::{ReflectComponent, ReflectMapEntities},
7+
system::Command,
78
world::{Mut, World},
89
};
910
use bevy_reflect::TypeRegistryArc;
10-
use bevy_transform::{components::Children, prelude::Parent};
11+
use bevy_transform::{hierarchy::AddChild, prelude::Parent};
1112
use bevy_utils::{tracing::error, HashMap};
1213
use thiserror::Error;
1314
use uuid::Uuid;
@@ -268,23 +269,22 @@ impl SceneSpawner {
268269
for (instance_id, parent) in scenes_with_parent {
269270
if let Some(instance) = self.spawned_instances.get(&instance_id) {
270271
for entity in instance.entity_map.values() {
271-
// Add the `Parent` component to the scene root
272-
if let Some(mut entity_mut) = world.get_entity_mut(entity) {
272+
// Add the `Parent` component to the scene root, and update the `Children` component of
273+
// the scene parent
274+
if !world
275+
.get_entity(entity)
273276
// This will filter only the scene root entity, as all other from the
274277
// scene have a parent
275-
if !entity_mut.contains::<Parent>() {
276-
entity_mut.insert(Parent(parent));
277-
if let Some(mut parent_entity) = world.get_entity_mut(parent) {
278-
if let Some(children) = parent_entity.get_mut::<Children>() {
279-
let children = &**children;
280-
let mut children = children.to_vec();
281-
children.push(entity);
282-
parent_entity.insert(Children::with(&children));
283-
} else {
284-
parent_entity.insert(Children::with(&[entity]));
285-
}
286-
}
278+
.map(|entity| entity.contains::<Parent>())
279+
// Default is true so that it won't run on an entity that wouldn't exist anymore
280+
// this case shouldn't happen anyway
281+
.unwrap_or(true)
282+
{
283+
AddChild {
284+
parent,
285+
child: entity,
287286
}
287+
.write(world);
288288
}
289289
}
290290
} else {

crates/bevy_transform/src/hierarchy/child_builder.rs

+22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ use bevy_ecs::{
77
};
88
use smallvec::SmallVec;
99

10+
#[derive(Debug)]
11+
pub struct AddChild {
12+
pub parent: Entity,
13+
pub child: Entity,
14+
}
15+
16+
impl Command for AddChild {
17+
fn write(self, world: &mut World) {
18+
world
19+
.entity_mut(self.child)
20+
// FIXME: don't erase the previous parent (see #1545)
21+
.insert_bundle((Parent(self.parent), PreviousParent(self.parent)));
22+
if let Some(mut children) = world.get_mut::<Children>(self.parent) {
23+
children.0.push(self.child);
24+
} else {
25+
world
26+
.entity_mut(self.parent)
27+
.insert(Children(smallvec::smallvec![self.child]));
28+
}
29+
}
30+
}
31+
1032
#[derive(Debug)]
1133
pub struct InsertChildren {
1234
parent: Entity,

0 commit comments

Comments
 (0)