Question: Is it possible to do nested for loops?
dmitsuki opened this issue · comments
Is it possible to do nested for loops simply within system iterations? If I call join on a &mut component then try to iterate inside that component using join again, the borrow checker complains. What is the intended way to have every entity operate on every entity as an example, mutably.
You can't mutably borrow twice. What are you trying to do? Perhaps it can be rewritten because this is, as you said, a borrow checker issue with your implementation and not specs-specific
I just want to do a n^2 component operation on some component. Would I have to iterate over entities instead and use get_mut then to make this work?
There are many ways to do it and it's been answered a lot on stackoverflow depending on your situation, which you won't give details on. You can split the iterator, copy it and walk that, use interior mutability, or pass a message to another system and have that other system mutate it. Or just rewrite it to not have a nested loop. If it's for collision, you can use ncollide and just query the collision world.
Note that you can use Storage::get_mut
to mutably get certain component for an entity.