fixed potential bug

This commit is contained in:
Elliott Pierce 2025-05-31 18:19:04 -04:00
parent 763877f786
commit 9baf15a932

View File

@ -2376,14 +2376,16 @@ impl<'w> EntityWorldMut<'w> {
/// Note that this still increases the generation to differentiate different constructions of the same row. /// Note that this still increases the generation to differentiate different constructions of the same row.
#[track_caller] #[track_caller]
pub fn destruct(&mut self) -> &mut Self { 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 // setup
let Some(location) = self.location else { let Some(location) = self.location else {
// If there is no location, we are already destructed // If there is no location, we are already destructed
return self; return false;
}; };
let archetype = &self.world.archetypes[location.archetype_id]; 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.entity = unsafe { self.world.entities.mark_free(self.entity.row(), 1) };
self.world.flush(); self.world.flush();
self.update_location(); // In case some command re-constructs this entity. self.update_location(); // In case some command re-constructs this entity.
self true
} }
/// Despawns the current entity. /// Despawns the current entity.
@ -2539,8 +2541,9 @@ impl<'w> EntityWorldMut<'w> {
} }
pub(crate) fn despawn_with_caller(mut self, caller: MaybeLocation) { pub(crate) fn despawn_with_caller(mut self, caller: MaybeLocation) {
self.destruct_with_caller(caller); if self.destruct_with_caller(caller) {
self.world.allocator.free(self.entity); self.world.allocator.free(self.entity);
}
} }
/// Ensures any commands triggered by the actions of Self are applied, equivalent to [`World::flush`] /// Ensures any commands triggered by the actions of Self are applied, equivalent to [`World::flush`]