From 335dcf96a20f31e8145e7b8fe83a103a3817d4fa Mon Sep 17 00:00:00 2001 From: James O'Brien Date: Mon, 17 Jun 2024 08:15:30 -0700 Subject: [PATCH] Update observer archetype flags for sparse components (#13886) # Objective - Fixes #13885 ## Solution - Update the flags correctly on archetype creation ## Testing - Added `observer_order_insert_remove_sparse` to catch regressions. --- crates/bevy_ecs/src/archetype.rs | 1 + crates/bevy_ecs/src/observer/mod.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/crates/bevy_ecs/src/archetype.rs b/crates/bevy_ecs/src/archetype.rs index 6e6450a98d..be502459d5 100644 --- a/crates/bevy_ecs/src/archetype.rs +++ b/crates/bevy_ecs/src/archetype.rs @@ -370,6 +370,7 @@ impl Archetype { // SAFETY: We are creating an archetype that includes this component so it must exist let info = unsafe { components.get_info_unchecked(component_id) }; info.update_archetype_flags(&mut flags); + observers.update_archetype_flags(component_id, &mut flags); archetype_components.insert( component_id, ArchetypeComponentInfo { diff --git a/crates/bevy_ecs/src/observer/mod.rs b/crates/bevy_ecs/src/observer/mod.rs index 08e87854c4..b1876afddd 100644 --- a/crates/bevy_ecs/src/observer/mod.rs +++ b/crates/bevy_ecs/src/observer/mod.rs @@ -400,6 +400,10 @@ mod tests { #[derive(Component)] struct C; + #[derive(Component)] + #[component(storage = "SparseSet")] + struct S; + #[derive(Event)] struct EventA; @@ -444,6 +448,22 @@ mod tests { assert_eq!(3, world.resource::().0); } + #[test] + fn observer_order_insert_remove_sparse() { + let mut world = World::new(); + world.init_resource::(); + + world.observe(|_: Trigger, mut res: ResMut| res.assert_order(0)); + world.observe(|_: Trigger, mut res: ResMut| res.assert_order(1)); + world.observe(|_: Trigger, mut res: ResMut| res.assert_order(2)); + + let mut entity = world.spawn_empty(); + entity.insert(S); + entity.remove::(); + entity.flush(); + assert_eq!(3, world.resource::().0); + } + #[test] fn observer_order_recursive() { let mut world = World::new();