Skip to content

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

Open
@CorvusPrudens

Description

@CorvusPrudens

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.

What solution would you like?

Ideally, the Disabled component would prevent an Observer in the same entity from running.

Additional context

Here's a quick demonstration of the current state:

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<OnAdd, Transform>| {
        info!("normal observer: added transform to {:?}", trigger.target());
    }));

    commands.spawn((
        Observer::new(|trigger: Trigger<OnAdd, Transform>| {
            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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorD-ComplexQuite challenging from either a design or technical perspective. Ask for help!S-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    Status

    Observer overhaul

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions