diff --git a/crates/bevy_ecs/hecs/src/borrow.rs b/crates/bevy_ecs/hecs/src/borrow.rs index abb90e6c81..9dc5cdd33a 100644 --- a/crates/bevy_ecs/hecs/src/borrow.rs +++ b/crates/bevy_ecs/hecs/src/borrow.rs @@ -105,6 +105,7 @@ impl<'a, T: Component> Deref for Ref<'a, T> { pub struct RefMut<'a, T: Component> { archetype: &'a Archetype, target: NonNull, + modified: &'a mut bool, } impl<'a, T: Component> RefMut<'a, T> { @@ -118,7 +119,16 @@ impl<'a, T: Component> RefMut<'a, T> { .add(index as usize), ); archetype.borrow_mut::(); - Ok(Self { archetype, target }) + let modified = archetype + .get_modified::() + .unwrap() + .as_ptr() + .add(index as usize); + Ok(Self { + archetype, + target, + modified: &mut *modified, + }) } } @@ -140,6 +150,7 @@ impl<'a, T: Component> Deref for RefMut<'a, T> { impl<'a, T: Component> DerefMut for RefMut<'a, T> { fn deref_mut(&mut self) -> &mut T { + *self.modified = true; unsafe { self.target.as_mut() } } }