Skip to content

Commit ab440e7

Browse files
committed
bevy_ecs: flush entities after running observers and hooks in despawn
1 parent de3c70a commit ab440e7

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

crates/bevy_ecs/src/observer/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1160,4 +1160,22 @@ mod tests {
11601160
world.flush();
11611161
assert_eq!(vec!["event", "event"], world.resource::<Order>().0);
11621162
}
1163+
1164+
#[test]
1165+
fn observer_on_remove_during_despawn_spawn_empty() {
1166+
let mut world = World::new();
1167+
1168+
let ent = world
1169+
.spawn(A)
1170+
.observe(|trigger: Trigger<OnRemove, A>, mut cmd: Commands| {
1171+
cmd.spawn_empty();
1172+
})
1173+
.id();
1174+
1175+
world.flush();
1176+
1177+
world.entity_mut(ent).despawn();
1178+
1179+
world.flush();
1180+
}
11631181
}

crates/bevy_ecs/src/world/entity_ref.rs

+4
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)