Skip to content

Don't ignore default query filters for EntityRef or EntityMut #20163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions crates/bevy_ecs/src/entity_disabling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ mod tests {

use super::*;
use crate::{
prelude::World,
prelude::{EntityMut, EntityRef, World},
query::{Has, With},
};
use alloc::{vec, vec::Vec};
Expand Down Expand Up @@ -278,30 +278,42 @@ mod tests {
let mut world = World::new();
world.register_disabling_component::<CustomDisabled>();

// Use powers of two so we can uniquely identify the set of matching archetypes from the count.
world.spawn_empty();
world.spawn(Disabled);
world.spawn(CustomDisabled);
world.spawn((Disabled, CustomDisabled));
world.spawn_batch((0..2).map(|_| Disabled));
world.spawn_batch((0..4).map(|_| CustomDisabled));
world.spawn_batch((0..8).map(|_| (Disabled, CustomDisabled)));

let mut query = world.query::<()>();
assert_eq!(1, query.iter(&world).count());

let mut query = world.query_filtered::<(), With<Disabled>>();
let mut query = world.query::<EntityRef>();
assert_eq!(1, query.iter(&world).count());

let mut query = world.query::<Has<Disabled>>();
let mut query = world.query::<EntityMut>();
assert_eq!(1, query.iter(&world).count());

let mut query = world.query_filtered::<(), With<Disabled>>();
assert_eq!(2, query.iter(&world).count());

let mut query = world.query::<Has<Disabled>>();
assert_eq!(3, query.iter(&world).count());

let mut query = world.query_filtered::<(), With<CustomDisabled>>();
assert_eq!(1, query.iter(&world).count());
assert_eq!(4, query.iter(&world).count());

let mut query = world.query::<Has<CustomDisabled>>();
assert_eq!(2, query.iter(&world).count());
assert_eq!(5, query.iter(&world).count());

let mut query = world.query_filtered::<(), (With<Disabled>, With<CustomDisabled>)>();
assert_eq!(1, query.iter(&world).count());
assert_eq!(8, query.iter(&world).count());

let mut query = world.query::<(Has<Disabled>, Has<CustomDisabled>)>();
assert_eq!(4, query.iter(&world).count());
assert_eq!(15, query.iter(&world).count());

// This seems like it ought to count as a mention of `Disabled`, but it does not.
// We don't consider read access, since that would count `EntityRef` as a mention of *all* components.
let mut query = world.query::<Option<&Disabled>>();
assert_eq!(1, query.iter(&world).count());
}
}
7 changes: 4 additions & 3 deletions crates/bevy_ecs/src/query/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,10 +1225,11 @@ impl<T: SparseSetIndex> FilteredAccess<T> {
.flat_map(|f| f.without.ones().map(T::get_sparse_set_index))
}

/// Returns true if the index is used by this `FilteredAccess` in any way
/// Returns true if the index is used by this `FilteredAccess` in filters or archetypal access.
/// This includes most ways to access a component, but notably excludes `EntityRef` and `EntityMut`
/// along with anything inside `Option<T>`.
pub fn contains(&self, index: T) -> bool {
self.access().has_component_read(index.clone())
|| self.access().has_archetypal(index.clone())
self.access().has_archetypal(index.clone())
|| self.filter_sets.iter().any(|f| {
f.with.contains(index.sparse_set_index())
|| f.without.contains(index.sparse_set_index())
Expand Down
Loading