removal_detection/removal_detection.rs
1//! This example shows how you can know when a [`Component`] has been removed, so you can react to it.
2//!
3//! When a [`Component`] is removed from an [`Entity`], all [`Observer`] with an [`Remove`] trigger for
4//! that [`Component`] will be notified. These observers will be called immediately after the
5//! [`Component`] is removed. For more info on observers, see the
6//! [observers example](https://github.com/bevyengine/bevy/blob/main/examples/ecs/observers.rs).
7//!
8//! Advanced users may also consider using a lifecycle hook
9//! instead of an observer, as it incurs less overhead for a case like this.
10//! See the [component hooks example](https://github.com/bevyengine/bevy/blob/main/examples/ecs/component_hooks.rs).
11use bevy::prelude::*;
12
13fn main() {
14 App::new()
15 .add_plugins(DefaultPlugins)
16 .add_systems(Startup, setup)
17 // This system will remove a component after two seconds.
18 .add_systems(Update, remove_component)
19 // This observer will react to the removal of the component.
20 .add_observer(react_on_removal)
21 .run();
22}
23
24/// This `struct` is just used for convenience in this example. This is the [`Component`] we'll be
25/// giving to the `Entity` so we have a [`Component`] to remove in `remove_component()`.
26#[derive(Component)]
27struct MyComponent;
28
29fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
30 commands.spawn(Camera2d);
31 commands.spawn((
32 Sprite::from_image(asset_server.load("branding/icon.png")),
33 // Add the `Component`.
34 MyComponent,
35 ));
36}
37
38fn remove_component(
39 time: Res<Time>,
40 mut commands: Commands,
41 query: Query<Entity, With<MyComponent>>,
42) {
43 // After two seconds have passed the `Component` is removed.
44 if time.elapsed_secs() > 2.0
45 && let Some(entity) = query.iter().next()
46 {
47 commands.entity(entity).remove::<MyComponent>();
48 }
49}
50
51fn react_on_removal(remove: On<Remove, MyComponent>, mut query: Query<&mut Sprite>) {
52 // The `Remove` event was automatically triggered for the `Entity` that had its `MyComponent` removed.
53 if let Ok(mut sprite) = query.get_mut(remove.entity) {
54 sprite.color = Color::srgb(0.5, 1., 1.);
55 }
56}