Skip to content

Commit ae4144b

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 ae4144b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

crates/bevy_ecs/src/core/filter.rs

+40
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,52 @@ 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::prelude::*;
43+
///
44+
/// fn print_moving_objects_system(query: Query<(Name,), (Mutated<Transform>,)>) {
45+
/// for (name, ) in query.iter() {
46+
/// println!("Entity Moved: {}", name)
47+
/// }
48+
/// }
49+
/// ```
3450
pub struct Mutated<T>(NonNull<ComponentFlags>, PhantomData<T>);
3551

3652
/// Query transformer that retrieves components of type `T` that have been added since the start of the frame.
53+
/// This filter is useful as a performance optimization as it means that a system can be made to run only when
54+
/// it needs to rather than every frame.
55+
///
56+
/// Because the ordering of systems can change and this filter is only effective on changes before the query executes
57+
/// you need to use explicit dependency ordering or ordered stages for these query filters to be useful.
58+
///
59+
///
60+
/// Example:
61+
/// ```
62+
/// use bevy::prelude::*;
63+
///
64+
/// fn print_moving_objects_system(query: Query<(Name,), (Added<Name>,)>) {
65+
/// for (name, ) in query.iter() {
66+
/// println!("Named entity created: {}", name)
67+
/// }
68+
/// }
69+
/// ```
3770
pub struct Added<T>(NonNull<ComponentFlags>, PhantomData<T>);
3871

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

4282
impl QueryFilter for () {

0 commit comments

Comments
 (0)