From 9baf15a93245246a5a191214741d0f47c7bd6a89 Mon Sep 17 00:00:00 2001 From: Elliott Pierce Date: Sat, 31 May 2025 18:19:04 -0400 Subject: [PATCH] fixed potential bug --- crates/bevy_ecs/src/world/entity_ref.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 6dec064703..7a7756a5a1 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -2376,14 +2376,16 @@ impl<'w> EntityWorldMut<'w> { /// Note that this still increases the generation to differentiate different constructions of the same row. #[track_caller] pub fn destruct(&mut self) -> &mut Self { - self.destruct_with_caller(MaybeLocation::caller()) + self.destruct_with_caller(MaybeLocation::caller()); + self } - pub(crate) fn destruct_with_caller(&mut self, caller: MaybeLocation) -> &mut Self { + /// Returns whether or not it really did need to destruct. + pub(crate) fn destruct_with_caller(&mut self, caller: MaybeLocation) -> bool { // setup let Some(location) = self.location else { // If there is no location, we are already destructed - return self; + return false; }; let archetype = &self.world.archetypes[location.archetype_id]; @@ -2522,7 +2524,7 @@ impl<'w> EntityWorldMut<'w> { self.entity = unsafe { self.world.entities.mark_free(self.entity.row(), 1) }; self.world.flush(); self.update_location(); // In case some command re-constructs this entity. - self + true } /// Despawns the current entity. @@ -2539,8 +2541,9 @@ impl<'w> EntityWorldMut<'w> { } pub(crate) fn despawn_with_caller(mut self, caller: MaybeLocation) { - self.destruct_with_caller(caller); - self.world.allocator.free(self.entity); + if self.destruct_with_caller(caller) { + self.world.allocator.free(self.entity); + } } /// Ensures any commands triggered by the actions of Self are applied, equivalent to [`World::flush`]