Tainted\\Coders

Query for removed components

Bevy version: 0.14Last updated:

To query for all removed components of a type within a World we can use RemovedComponents. It will let us iterate over all entities that had the component removed this frame.

const RED: Color = Color::srgb(1.0, 0.0, 0.0);

fn react_on_removal(
  mut removed: RemovedComponents<Dying>,
  mut query: Query<&mut Sprite>,
) {
  // RemovedComponents returns us the entities
  for entity in removed.read() {
    // Which we use to query for other components on the entity
    if let Ok(mut sprite) = query.get_mut(entity) {
      // And remove red from any sprite components that are
      // not dying
      sprite.color = RED;
    }
  }
}