Skip to content

Bevy does not distinguish between "has a component" access and "read" access, leading to OnRemove + Has system param incompatibility #18964

@tnthung

Description

@tnthung

Bevy version

v0.16.0

What you did

Code:

use bevy::prelude::*;


fn main() {
  App::new()
    .add_plugins(DefaultPlugins)
    .add_observer(foo)
    .add_systems(Startup, setup)
    .add_systems(Update, update)
    .run();
}


#[derive(Component)]
struct A;


fn foo(t: Trigger<OnRemove, A>, q: Query<Has<A>>) {
  println!("foo: {:?}", q.get(t.target()));
}


fn setup(mut commands: Commands) {
  commands.spawn(A);
}


fn update(mut commands: Commands, q: Query<Entity, With<A>>, time: Res<Time>) {
  if time.elapsed_secs() > 2.0 {
    for entity in q.iter() {
      commands.entity(entity).remove::<A>();
    }
  }
}

What went wrong

When the component A is removed from the entity, the observer foo is correctly triggered. However, the Has<A> in query still reports true. This is a MRE, my actual implementation is way complicated, solely relay on OnRemove to tell if the component presents in my case is impossible, unless I totally rewrite that part of the logics (which is probably the only workaround, AFAIK).

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

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions