#Ref<T> for change detection, but mutable

8 messages · Page 1 of 1 (latest)

dreamy epoch
#

Hi đź‘‹

I have a query like

Query<&mut MyComponent>

and I want to detect if the component has changed by using the new my_component.is_changed() function of Ref

But if I switch my query to Query<Ref<MyComponent>> then I lose the mutability, and I need it

I read in the documentation about Ref (https://docs.rs/bevy/latest/bevy/prelude/struct.Ref.html) I quote: Similar to Mut but is immutable and so doesn’t require unique access., but it's misleading because I can't just replace it by Query<Mut<MyComponent>>

If I do that, I get an error like this:

error: the trait bound `bevy::prelude::Mut<'_, my_crate::MyComponent>: WorldQuery` is not satisfied
label: the trait `WorldQuery` is not implemented for `bevy::prelude::Mut<'_, my_crate::MyComponent>`

Am I doing something wrong, or I did missunderstand the docs? 🤔

river karma
fresh salmon
#

&mut MyComponent will give you a Mut<MyComponent>

#

And you’ll be able to use all the normal methods from the DetectChanges trait

#

But yeah you could also consider using Changed like Nocta suggested (which I believe should be more efficient if you just need to filter down values)

dreamy epoch
#

oh, you're right! I didn't notice 🤦‍♂️ thx!

#

and yep, in fact I changed to Changed<MyComponent as a workaround, but still wanted to understand how is_changed works

#

btw, Changed<MyComponent> detects both changes and new additions, right? I don't need to do Or<(Changed<MyComponent>, Added<MyComponent>)>