bevy/examples/ecs
Zoey 4820917af6 Add set_if_neq method to DetectChanges trait (Rebased) (#6853)
# Objective

Change detection can be spuriously triggered by setting a field to the same value as before. As a result, a common pattern is to write:

```rust
if *foo != value {
  *foo = value;
}
```

This is confusing to read, and heavy on boilerplate.

Adopted from #5373, but untangled and rebased to current `bevy/main`.

## Solution

    1. Add a method to the `DetectChanges` trait that implements this boilerplate when the appropriate trait bounds are met.

    2. Document this minor footgun, and point users to it.


## Changelog

    * added the `set_if_neq` method to avoid triggering change detection when the new and previous values are equal. This will work on both components and resources.


## Migration Guide

If you are manually checking if a component or resource's value is equal to its new value before setting it to avoid triggering change detection, migrate to the clearer and more convenient `set_if_neq` method.
## Context

Related to #2363 as it avoids triggering change detection, but not a complete solution (as it still requires triggering it when real changes are made).



Co-authored-by: Zoey <Dessix@Dessix.net>
2022-12-11 19:24:19 +00:00
..
component_change_detection.rs Add set_if_neq method to DetectChanges trait (Rebased) (#6853) 2022-12-11 19:24:19 +00:00
custom_query_param.rs fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
ecs_guide.rs fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
event.rs Replace the bool argument of Timer with TimerMode (#6247) 2022-10-17 13:47:01 +00:00
fixed_timestep.rs Add global time scaling (#5752) 2022-10-22 18:52:29 +00:00
generic_system.rs Replace the bool argument of Timer with TimerMode (#6247) 2022-10-17 13:47:01 +00:00
hierarchy.rs Add global time scaling (#5752) 2022-10-22 18:52:29 +00:00
iter_combinations.rs use Mul<f32> to double the value of Vec3 (#6607) 2022-11-14 23:08:30 +00:00
parallel_query.rs Spawn now takes a Bundle (#6054) 2022-09-23 19:55:54 +00:00
removal_detection.rs Add global time scaling (#5752) 2022-10-22 18:52:29 +00:00
startup_system.rs Doc/module style doc blocks for examples (#4438) 2022-05-16 13:53:20 +00:00
state.rs Remove auto-margin properties from the examples (#6535) 2022-11-21 14:38:35 +00:00
system_closure.rs fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
system_param.rs Spawn now takes a Bundle (#6054) 2022-09-23 19:55:54 +00:00
system_piping.rs [Fixes #6224] Add logging variants of system piping (#6751) 2022-12-11 18:10:03 +00:00
system_sets.rs Add global time scaling (#5752) 2022-10-22 18:52:29 +00:00
timers.rs Replace the bool argument of Timer with TimerMode (#6247) 2022-10-17 13:47:01 +00:00