Skip to content

Commit 5b7eb1e

Browse files
committed
bevy_ecs: flush entities after running observers and hooks in despawn
1 parent b884f96 commit 5b7eb1e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

crates/bevy_ecs/src/observer/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,4 +1082,22 @@ mod tests {
10821082
world.flush();
10831083
assert_eq!(vec!["event", "event"], world.resource::<Order>().0);
10841084
}
1085+
1086+
#[test]
1087+
fn observer_on_remove_during_despawn_spawn_empty() {
1088+
let mut world = World::new();
1089+
1090+
let ent = world
1091+
.spawn(A)
1092+
.observe(|trigger: Trigger<OnRemove, A>, mut cmd: Commands| {
1093+
cmd.spawn_empty();
1094+
})
1095+
.id();
1096+
1097+
world.flush();
1098+
1099+
world.entity_mut(ent).despawn();
1100+
1101+
world.flush();
1102+
}
10851103
}

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,10 @@ impl<'w> EntityWorldMut<'w> {
13191319
}
13201320
}
13211321

1322+
// Observers and on_remove hooks may reserve new entities, which
1323+
// requires a flush before Entities::free may be called.
1324+
world.flush_entities();
1325+
13221326
for component_id in archetype.components() {
13231327
world.removed_components.send(component_id, self.entity);
13241328
}

0 commit comments

Comments
 (0)