bevyengine / bevyengine/bevy

`Disabled` should disable observers / `World` APIs do not respect default query filters

Open
#19,866 2 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Bug D-Complex S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?

It's occasionally useful to be able to temporarily "disable" an observer. In many cases, this can be trivially achieved by despawning (and then later re-spawning) `Observer` entities. However, this is not a general solution.

If the observer is a capturing closure that may mutate its captured state, despawning it will thrash the state. In generic contexts, this would also require a `Clone` bound on the observer. You _can_ work around this currently, but [it's a little cumbersome](https://discord.com/channels/691052431525675048/742569353878437978/1388978687059300393).

## What solution would you like?

Ideally, the [`Disabled`](https://docs.rs/bevy/latest/bevy/ecs/entity_disabling/struct.Disabled.html) component would prevent an `Observer` in the same entity from running.

## Additional context

Here's a quick demonstration of the current state:

```rs
use bevy::{ecs::entity_disabling::Disabled, prelude::*};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, startup)
.run();
}

fn startup(mut commands: Commands) {
commands.spawn(Observer::new(|trigger: Trigger| {
info!("normal observer: added transform to {:?}", trigger.target());
}));

commands.spawn((
Observer::new(|trigger: Trigger| {
info!(
"disabled observer: added transform to {:?}",
trigger.target()
);
}),
Disabled,
));

commands.spawn(Transform::default());
}
```

As is, this outputs two logs:

```
normal observer: added transform to 7v1#4294967303
disabled observer: added transform to 7v1#4294967303
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.