Skip to content

Commit

Permalink
Expose set_changed() on ResMut and Mut (#2208)
Browse files Browse the repository at this point in the history
This new api stems from this [discord conversation](https://discord.com/channels/691052431525675048/742569353878437978/844057268172357663).

This exposes a public facing `set_changed` method on `ResMut` and `Mut`.

As a side note: `ResMut` and `Mut` have a lot of duplicated code, I have a PR I may put up later that refactors these commonalities into a trait.

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
  • Loading branch information
NathanSWard and cart committed May 18, 2021
1 parent 93cc721 commit 9eb1aee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ impl<'w, T: Component> ResMut<'w, T> {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}

/// Manually flags this resource as having been changed. This normally isn't
/// required because accessing this pointer mutably automatically flags this
/// resource as "changed".
///
/// **Note**: This operation is irreversible.
#[inline]
pub fn set_changed(&mut self) {
self.ticks.set_changed(self.change_tick);
}
}

impl<'w, T: Component> Deref for ResMut<'w, T> {
Expand All @@ -381,7 +391,7 @@ impl<'w, T: Component> Deref for ResMut<'w, T> {

impl<'w, T: Component> DerefMut for ResMut<'w, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.ticks.set_changed(self.change_tick);
self.set_changed();
self.value
}
}
Expand Down
12 changes: 11 additions & 1 deletion crates/bevy_ecs/src/world/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ impl<'a, T> Mut<'a, T> {
self.component_ticks.set_changed(self.change_tick);
self.value
}

/// Manually flags this value as having been changed. This normally isn't
/// required because accessing this pointer mutably automatically flags this
/// value as "changed".
///
/// **Note**: This operation is irreversible.
#[inline]
pub fn set_changed(&mut self) {
self.component_ticks.set_changed(self.change_tick);
}
}

impl<'a, T> Deref for Mut<'a, T> {
Expand All @@ -28,7 +38,7 @@ impl<'a, T> Deref for Mut<'a, T> {
impl<'a, T> DerefMut for Mut<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.component_ticks.set_changed(self.change_tick);
self.set_changed();
self.value
}
}
Expand Down

0 comments on commit 9eb1aee

Please sign in to comment.