@@ -898,63 +898,39 @@ impl_debug!(Ref<'w, T>,);
898
898
899
899
/// Unique mutable borrow of an entity's component or of a resource.
900
900
///
901
- /// This can be used in queries to opt into change detection on both their mutable and immutable forms , as opposed to
902
- /// `&mut T`, which only provides access to change detection while in its mutable form:
901
+ /// This can be used in queries to access change detection from immutable query methods , as opposed
902
+ /// to `&mut T` which only provides access to change detection from mutable query methods.
903
903
///
904
904
/// ```rust
905
905
/// # use bevy_ecs::prelude::*;
906
906
/// # use bevy_ecs::query::QueryData;
907
907
/// #
908
- /// #[derive(Component, Clone)]
908
+ /// #[derive(Component, Clone, Debug )]
909
909
/// struct Name(String);
910
910
///
911
- /// #[derive(Component, Clone, Copy)]
911
+ /// #[derive(Component, Clone, Copy, Debug )]
912
912
/// struct Health(f32);
913
913
///
914
- /// #[derive(Component, Clone, Copy)]
915
- /// struct Position {
916
- /// x: f32,
917
- /// y: f32,
918
- /// };
919
- ///
920
- /// #[derive(Component, Clone, Copy)]
921
- /// struct Player {
922
- /// id: usize,
923
- /// };
924
- ///
925
- /// #[derive(QueryData)]
926
- /// #[query_data(mutable)]
927
- /// struct PlayerQuery {
928
- /// id: &'static Player,
929
- ///
930
- /// // Reacting to `PlayerName` changes is expensive, so we need to enable change detection when reading it.
931
- /// name: Mut<'static, Name>,
932
- ///
933
- /// health: &'static mut Health,
934
- /// position: &'static mut Position,
935
- /// }
936
- ///
937
- /// fn update_player_avatars(players_query: Query<PlayerQuery>) {
938
- /// // The item returned by the iterator is of type `PlayerQueryReadOnlyItem`.
939
- /// for player in players_query.iter() {
940
- /// if player.name.is_changed() {
941
- /// // Update the player's name. This clones a String, and so is more expensive.
942
- /// update_player_name(player.id, player.name.clone());
943
- /// }
944
- ///
945
- /// // Update the health bar.
946
- /// update_player_health(player.id, *player.health);
914
+ /// fn my_system(mut query: Query<(Mut<Name>, &mut Health)>) {
915
+ /// // Mutable access provides change detection information for both parameters:
916
+ /// // - `name` has type `Mut<Name>`
917
+ /// // - `health` has type `Mut<Health>`
918
+ /// for (name, health) in query.iter_mut() {
919
+ /// println!("Name: {:?} (last changed {:?})", name, name.last_changed());
920
+ /// println!("Health: {:?} (last changed: {:?})", health, health.last_changed());
921
+ /// # println!("{}{}", name.0, health.0); // Silence dead_code warning
922
+ /// }
947
923
///
948
- /// // Update the player's position.
949
- /// update_player_position(player.id, *player.position);
924
+ /// // Immutable access only provides change detection for `Name`:
925
+ /// // - `name` has type `Ref<Name>`
926
+ /// // - `health` has type `&Health`
927
+ /// for (name, health) in query.iter() {
928
+ /// println!("Name: {:?} (last changed {:?})", name, name.last_changed());
929
+ /// println!("Health: {:?}", health);
950
930
/// }
951
931
/// }
952
932
///
953
- /// # bevy_ecs::system::assert_is_system(update_player_avatars);
954
- ///
955
- /// # fn update_player_name(player: &Player, new_name: Name) {}
956
- /// # fn update_player_health(player: &Player, new_health: Health) {}
957
- /// # fn update_player_position(player: &Player, new_position: Position) {}
933
+ /// # bevy_ecs::system::assert_is_system(my_system);
958
934
/// ```
959
935
pub struct Mut < ' w , T : ?Sized > {
960
936
pub ( crate ) value : & ' w mut T ,
0 commit comments