Skip to content

Commit bd1e44b

Browse files
Demonstrate pattern in example
1 parent 37b3a51 commit bd1e44b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

examples/ecs/component_change_detection.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() {
1313
.run();
1414
}
1515

16-
#[derive(Component, Debug)]
16+
#[derive(Component, PartialEq, Debug)]
1717
struct MyComponent(f64);
1818

1919
fn setup(mut commands: Commands) {
@@ -25,7 +25,12 @@ fn change_component(time: Res<Time>, mut query: Query<(Entity, &mut MyComponent)
2525
for (entity, mut component) in &mut query {
2626
if rand::thread_rng().gen_bool(0.1) {
2727
info!("changing component {:?}", entity);
28-
component.0 = time.seconds_since_startup();
28+
let new_component = MyComponent(time.seconds_since_startup().round());
29+
// Change detection occurs on mutable derefence,
30+
// and does not consider whether or not a value is actually equal.
31+
// To avoid triggering change detection when nothing has actually changed,
32+
// you can use the `set_if_neq` method on any component or resource that implements PartialEq
33+
component.set_if_neq(new_component);
2934
}
3035
}
3136
}

0 commit comments

Comments
 (0)