Skip to content

Commit 9eb1aee

Browse files
NathanSWardcart
andcommitted
Expose set_changed() on ResMut and Mut (#2208)
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 <[email protected]>
1 parent 93cc721 commit 9eb1aee

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

crates/bevy_ecs/src/system/system_param.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ impl<'w, T: Component> ResMut<'w, T> {
369369
self.ticks
370370
.is_changed(self.last_change_tick, self.change_tick)
371371
}
372+
373+
/// Manually flags this resource as having been changed. This normally isn't
374+
/// required because accessing this pointer mutably automatically flags this
375+
/// resource as "changed".
376+
///
377+
/// **Note**: This operation is irreversible.
378+
#[inline]
379+
pub fn set_changed(&mut self) {
380+
self.ticks.set_changed(self.change_tick);
381+
}
372382
}
373383

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

382392
impl<'w, T: Component> DerefMut for ResMut<'w, T> {
383393
fn deref_mut(&mut self) -> &mut Self::Target {
384-
self.ticks.set_changed(self.change_tick);
394+
self.set_changed();
385395
self.value
386396
}
387397
}

crates/bevy_ecs/src/world/pointer.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ impl<'a, T> Mut<'a, T> {
1414
self.component_ticks.set_changed(self.change_tick);
1515
self.value
1616
}
17+
18+
/// Manually flags this value as having been changed. This normally isn't
19+
/// required because accessing this pointer mutably automatically flags this
20+
/// value as "changed".
21+
///
22+
/// **Note**: This operation is irreversible.
23+
#[inline]
24+
pub fn set_changed(&mut self) {
25+
self.component_ticks.set_changed(self.change_tick);
26+
}
1727
}
1828

1929
impl<'a, T> Deref for Mut<'a, T> {
@@ -28,7 +38,7 @@ impl<'a, T> Deref for Mut<'a, T> {
2838
impl<'a, T> DerefMut for Mut<'a, T> {
2939
#[inline]
3040
fn deref_mut(&mut self) -> &mut T {
31-
self.component_ticks.set_changed(self.change_tick);
41+
self.set_changed();
3242
self.value
3343
}
3444
}

0 commit comments

Comments
 (0)