Skip to content

Commit 210f82a

Browse files
committed
Added documentation on the query filters Mutated<T> Changed<T> and Added<T>
This documents both the non-obvious interaction with non-explicit system ordering and adds examples for Changed and Added. This is in response to #1551
1 parent d9fb61d commit 210f82a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

crates/bevy_ecs/src/core/filter.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,62 @@ pub struct Or<T>(pub T);
3131

3232
/// Query transformer that retrieves components of type `T` that have been mutated since the start of the frame.
3333
/// Added components do not count as mutated.
34+
/// This filter is useful as a performance optimization as it means that a system can be made to run only when
35+
/// it needs to rather than every frame.
36+
///
37+
/// Because the ordering of systems can change and this filter is only effective on changes before the query executes
38+
/// you need to use explicit dependency ordering or ordered stages for these query filters to be useful.
39+
///
40+
/// Example:
41+
/// ```
42+
/// # use bevy_ecs::Query;
43+
/// # use bevy_ecs::Mutated;
44+
///
45+
/// # #[derive(Debug)]
46+
/// # struct Name {};
47+
/// # struct Transform {};
48+
///
49+
/// fn print_moving_objects_system(query: Query<(&Name,), (Mutated<Transform>,)>) {
50+
/// for (name, ) in query.iter() {
51+
/// println!("Entity Moved: {:?}", name)
52+
/// }
53+
/// }
54+
/// ```
3455
pub struct Mutated<T>(NonNull<ComponentFlags>, PhantomData<T>);
3556

3657
/// Query transformer that retrieves components of type `T` that have been added since the start of the frame.
58+
/// This filter is useful as a performance optimization as it means that a system can be made to run only when
59+
/// it needs to rather than every frame.
60+
///
61+
/// Because the ordering of systems can change and this filter is only effective on changes before the query executes
62+
/// you need to use explicit dependency ordering or ordered stages for these query filters to be useful.
63+
///
64+
///
65+
/// Example:
66+
/// ```
67+
/// # use bevy_ecs::Query;
68+
/// # use bevy_ecs::Added;
69+
///
70+
/// # #[derive(Debug)]
71+
/// # struct Name {};
72+
/// # struct Transform {};
73+
///
74+
/// fn print_add_name_component(query: Query<(&Name,), (Added<Name>,)>) {
75+
/// for (name, ) in query.iter() {
76+
/// println!("Named entity created: {:?}", name)
77+
/// }
78+
/// }
79+
/// ```
3780
pub struct Added<T>(NonNull<ComponentFlags>, PhantomData<T>);
3881

3982
/// Query transformer that retrieves components of type `T` that have either been mutated or added since the start of the frame.
83+
/// This filter is useful as a performance optimization as it means that a system can be made to run only when
84+
/// it needs to rather than every frame.
85+
///
86+
/// Because the ordering of systems can change and this filter is only effective on changes before the query executes
87+
/// you need to use explicit dependency ordering or ordered stages for these query filters to be useful.
88+
///
89+
/// Also see the documentation for [`Mutated<T>`] and [`Added`] as this filter is a logical OR of them.
4090
pub struct Changed<T>(NonNull<ComponentFlags>, PhantomData<T>);
4191

4292
impl QueryFilter for () {

0 commit comments

Comments
 (0)