Skip to content

Commit 9474248

Browse files
authored
Fix EntityPatch and scene retention bugs in bevy_proto_bsn (#206)
* Ensure orphans exist before trying to despawn * Fix dynamic construction condition for EntityPatch * Fix redundant closure warning
1 parent b0c028c commit 9474248

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

crates/bevy_proto_bsn/src/entity_patch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ where
135135

136136
/// Constructs an [`EntityPatch`], inserts the resulting bundle to the context entity, and recursively spawns children.
137137
fn construct(self, context: &mut ConstructContext) -> Result<(), ConstructError> {
138-
if !self.inherit.root_count() > 0 {
138+
if self.inherit.root_count() > 0 {
139139
// Dynamic scene
140140
let mut dynamic_scene = DynamicScene::default();
141141
self.dynamic_patch(&mut dynamic_scene);

crates/bevy_proto_bsn/src/retain.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum Anchor {
1919
}
2020

2121
/// An explicit identifier for an entity in a retained scene.
22-
#[derive(Eq, PartialEq, Hash, Clone)]
22+
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
2323
pub struct Key(String);
2424

2525
impl<T: Display> From<T> for Key {
@@ -175,9 +175,7 @@ impl RetainScene for DynamicScene {
175175
});
176176

177177
// Retain the children
178-
let anchors = self
179-
.children
180-
.retain_children::<T>(entity, receipt.anchors)?;
178+
let anchors = self.children.retain_children(entity, receipt.anchors)?;
181179

182180
// Place the new receipt onto the entity
183181
entity.insert(Receipt::<T> {
@@ -192,18 +190,18 @@ impl RetainScene for DynamicScene {
192190

193191
/// Trait implemented for collections of scenes that can be retained.
194192
pub trait RetainChildren {
195-
/// Retains the scenes as children of `entity`, updating the [`Receipt`] in the process.
193+
/// Retains the scenes as children of `entity`, returning the new [`Anchor`] map.
196194
///
197195
/// See: [`RetainScene::retain`].
198-
fn retain_children<T: Send + Sync + 'static>(
196+
fn retain_children(
199197
self,
200198
entity: &mut EntityWorldMut,
201199
current_anchors: HashMap<Anchor, Entity>,
202200
) -> Result<HashMap<Anchor, Entity>, ConstructError>;
203201
}
204202

205203
impl RetainChildren for Vec<DynamicScene> {
206-
fn retain_children<T: Send + Sync + 'static>(
204+
fn retain_children(
207205
self,
208206
entity: &mut EntityWorldMut,
209207
mut current_anchors: HashMap<Anchor, Entity>,
@@ -241,7 +239,9 @@ impl RetainChildren for Vec<DynamicScene> {
241239
// first (before deparenting) so that hooks still see the parent when
242240
// they run.
243241
for orphan_id in current_anchors.into_values() {
244-
world.entity_mut(orphan_id).despawn();
242+
if let Ok(entity) = world.get_entity_mut(orphan_id) {
243+
entity.despawn();
244+
}
245245
}
246246
});
247247

@@ -323,7 +323,7 @@ impl RetainSceneExt for EntityWorldMut<'_> {
323323
.into_iter()
324324
.map(DynamicPatch::into_dynamic_scene)
325325
.collect::<Vec<_>>()
326-
.retain_children::<T>(self, receipt.anchors)?;
326+
.retain_children(self, receipt.anchors)?;
327327

328328
// Place the receipt back onto the entity
329329
self.insert(Receipt::<T> {

0 commit comments

Comments
 (0)