Change a component without triggering change detection #8748
-
Hi, I have a peculiar use case where I am synchronizing data across the network, and my intention is to base these synchronizations into ordered events that are not aware of game ticks or any other specific engine detail. All of that is made more complicated on the fact that I need to use reflection because these may be future components implemented in a higher level crate, that is using my sync one as a dependency. This means I am bound to use the reflection apis that are available in bevy. So, is there a way to apply a component change without triggering the change in a system's query that is looking for such trigger? Currently I am using this way of updating components that came across the net: reflect_component.apply_or_insert(&mut world.entity_mut(e_id), component_data.as_reflect()); (ref: https://github.com/raffaeleragni/bevy_sync/blob/master/src/server.rs#L409) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
You want the |
Beta Was this translation helpful? Give feedback.
-
Yes, I noticed that in a PR before, but that's also the reason I am asking this question: I can't find how I am supposed to use it. |
Beta Was this translation helpful? Give feedback.
-
Speaking of which, @alice-i-cecile is there a way to have two systems that would ignore changes made by each other (i.e. share state where changes are tracked)? |
Beta Was this translation helpful? Give feedback.
-
For the moment I can do this: if let Some(mut component) = reflect_component.reflect_mut(entity) {
component.bypass_change_detection().apply(component_data.as_reflect());
} else {
reflect_component.insert(entity, component_data.as_reflect());
} In the else statement tho, I still need to pass through reflection, maybe I just need to figure out how to do that in a different way too, since entities are also involved for that case and not just components (EntityMut::insert is used in side ReflectComponent::insert). |
Beta Was this translation helpful? Give feedback.
Instead of
&mut component
you usecomponent.bypass_change_detection()
.If you put both sides of the sync (bevy to network and network to bevy) into the same system, you won't have any loops. This is my preferred approach at the moment.