@@ -4,10 +4,11 @@ use bevy_asset::{AssetEvent, Assets, Handle};
4
4
use bevy_ecs:: {
5
5
entity:: { Entity , EntityMap } ,
6
6
reflect:: { ReflectComponent , ReflectMapEntities } ,
7
+ system:: Command ,
7
8
world:: { Mut , World } ,
8
9
} ;
9
10
use bevy_reflect:: TypeRegistryArc ;
10
- use bevy_transform:: { components :: Children , prelude:: Parent } ;
11
+ use bevy_transform:: { hierarchy :: AddChild , prelude:: Parent } ;
11
12
use bevy_utils:: { tracing:: error, HashMap } ;
12
13
use thiserror:: Error ;
13
14
use uuid:: Uuid ;
@@ -268,23 +269,22 @@ impl SceneSpawner {
268
269
for ( instance_id, parent) in scenes_with_parent {
269
270
if let Some ( instance) = self . spawned_instances . get ( & instance_id) {
270
271
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)
273
276
// This will filter only the scene root entity, as all other from the
274
277
// 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,
287
286
}
287
+ . write ( world) ;
288
288
}
289
289
}
290
290
} else {
0 commit comments