Mutate two components of a query at the same time #8075
-
I am new to Rust and Bevy, and I am trying to make a falling sand simulator and need to swap cell positions at the same time. I have achieved the swap in C# but Rust's borrow checker is making swapping harder as I can not access two items in a query at the same time. This is a simplified version of the movement loop
The
The compile error, "cannot borrow 'element_query' as immutable because it is also borrowed as mutable," occurs in the I understand the error, but I can not figure out a way around it. How can I access both elements in the query at the same time without the compile error? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You need to store your position differently. I suggest one of the following:
Then, in your system, get the pair let Ok([falling_element, other_element]) = query.get_many_mut([falling_entity, other_entity]); And you can feed that to your |
Beta Was this translation helpful? Give feedback.
You need to store your position differently. I suggest one of the following:
position
is a field ofElement
, while really it should be its own component. This way you can figure out neighbours without requiring access toElement
, which prevents mutating it. I suggest aQuery<(Entity, &Position), With<MovableSolid>>
position -> Entity
map or grid of entities in aRes
.Then, in your system, get the pair
(Entity, Entity)
for each falling element and potential target, and use theQuery::get_many_mut
method. Then you can do: