 57c021538e
			
		
	
	
		57c021538e
		
	
	
	
	
		
			
			# Objective Noticed a warning when running tests: ``` > cargo test --workspace warning: output filename collision. The example target `change_detection` in package `bevy_ecs v0.5.0 (/bevy/crates/bevy_ecs)` has the same output filename as the example target `change_detection` in package `bevy v0.5.0 (/bevy)`. Colliding filename is: /bevy/target/debug/examples/change_detection The targets should have unique names. Consider changing their names to be unique or compiling them separately. This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>. warning: output filename collision. The example target `change_detection` in package `bevy_ecs v0.5.0 (/bevy/crates/bevy_ecs)` has the same output filename as the example target `change_detection` in package `bevy v0.5.0 (/bevy)`. Colliding filename is: /bevy/target/debug/examples/change_detection.dSYM The targets should have unique names. Consider changing their names to be unique or compiling them separately. This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>. ``` ## Solution I renamed example `change_detection` to `component_change_detection`
		
			
				
	
	
		
			52 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use bevy::prelude::*;
 | |
| use rand::Rng;
 | |
| 
 | |
| // This example illustrates how to react to component change
 | |
| fn main() {
 | |
|     App::build()
 | |
|         .add_plugins(DefaultPlugins)
 | |
|         .add_startup_system(setup.system())
 | |
|         .add_system(change_component.system())
 | |
|         .add_system(change_detection.system())
 | |
|         .add_system(tracker_monitoring.system())
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| #[derive(Debug)]
 | |
| struct MyComponent(f64);
 | |
| 
 | |
| fn setup(mut commands: Commands) {
 | |
|     commands.spawn().insert(MyComponent(0.));
 | |
|     commands.spawn().insert(Transform::identity());
 | |
| }
 | |
| 
 | |
| fn change_component(time: Res<Time>, mut query: Query<(Entity, &mut MyComponent)>) {
 | |
|     for (entity, mut component) in query.iter_mut() {
 | |
|         if rand::thread_rng().gen_bool(0.1) {
 | |
|             info!("changing component {:?}", entity);
 | |
|             component.0 = time.seconds_since_startup();
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| // There are query filters for `Changed<T>` and `Added<T>`
 | |
| // Only entities matching the filters will be in the query
 | |
| fn change_detection(query: Query<(Entity, &MyComponent), Changed<MyComponent>>) {
 | |
|     for (entity, component) in query.iter() {
 | |
|         info!("{:?} changed: {:?}", entity, component,);
 | |
|     }
 | |
| }
 | |
| 
 | |
| // By looking at trackers, the query is not filtered but the information is available
 | |
| fn tracker_monitoring(
 | |
|     query: Query<(
 | |
|         Entity,
 | |
|         Option<&MyComponent>,
 | |
|         Option<ChangeTrackers<MyComponent>>,
 | |
|     )>,
 | |
| ) {
 | |
|     for (entity, component, trackers) in query.iter() {
 | |
|         info!("{:?}: {:?} -> {:?}", entity, component, trackers);
 | |
|     }
 | |
| }
 |