Skip to content

Commit 64b2961

Browse files
committed
Added documentation on the query filters (#1553)
This documents both the non-obvious interaction with non-explicit system ordering and adds examples for Changed and Added. This likely closes #1551
1 parent 87399c3 commit 64b2961

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

crates/bevy_ecs/src/query/filter.rs

+53
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,29 @@ macro_rules! impl_flag_filter {
574574

575575
impl_flag_filter!(
576576
/// Filter that retrieves components of type `T` that have been added since the start of the frame
577+
///
578+
/// This filter is useful as a performance optimization as it means that the query contains fewer items
579+
/// for a system to iterate over.
580+
///
581+
/// Because the ordering of systems can change and this filter is only effective on changes before the query executes
582+
/// you need to use explicit dependency ordering or ordered stages for these query filters to be useful.
583+
///
584+
///
585+
/// Example:
586+
/// ```
587+
/// # use bevy_ecs::system::Query;
588+
/// # use bevy_ecs::query::Added;
589+
/// #
590+
/// # #[derive(Debug)]
591+
/// # struct Name {};
592+
/// # struct Transform {};
593+
/// #
594+
/// fn print_add_name_component(query: Query<&Name, Added<Name>>) {
595+
/// for name in query.iter() {
596+
/// println!("Named entity created: {:?}", name)
597+
/// }
598+
/// }
599+
/// ```
577600
Added,
578601
AddedState,
579602
AddedFetch,
@@ -583,6 +606,28 @@ impl_flag_filter!(
583606
impl_flag_filter!(
584607
/// Filter that retrieves components of type `T` that have been mutated since the start of the frame.
585608
/// Added components do not count as mutated.
609+
///
610+
/// This filter is useful as a performance optimization as it means that the query contains fewer items
611+
/// for a system to iterate over.
612+
///
613+
/// Because the ordering of systems can change and this filter is only effective on changes before the query executes
614+
/// you need to use explicit dependency ordering or ordered stages for these query filters to be useful.
615+
///
616+
/// Example:
617+
/// ```
618+
/// # use bevy_ecs::system::Query;
619+
/// # use bevy_ecs::query::Mutated;
620+
/// #
621+
/// # #[derive(Debug)]
622+
/// # struct Name {};
623+
/// # struct Transform {};
624+
/// #
625+
/// fn print_moving_objects_system(query: Query<&Name, Mutated<Transform>>) {
626+
/// for name in query.iter() {
627+
/// println!("Entity Moved: {:?}", name)
628+
/// }
629+
/// }
630+
/// ```
586631
Mutated,
587632
MutatedState,
588633
MutatedFetch,
@@ -591,6 +636,14 @@ impl_flag_filter!(
591636

592637
impl_flag_filter!(
593638
/// Filter that retrieves components of type `T` that have been added or mutated since the start of the frame
639+
///
640+
/// This filter is useful as a performance optimization as it means that the query contains fewer items
641+
/// for a system to iterate over.
642+
///
643+
/// Because the ordering of systems can change and this filter is only effective on changes before the query executes
644+
/// you need to use explicit dependency ordering or ordered stages for these query filters to be useful.
645+
///
646+
/// Also see the documentation for [`Mutated<T>`] and [`Added`] as this filter is a logical OR of them.
594647
Changed,
595648
ChangedState,
596649
ChangedFetch,

0 commit comments

Comments
 (0)