specs icon indicating copy to clipboard operation
specs copied to clipboard

Question: Is it possible to do nested for loops?

Open dmitsuki opened this issue 5 years ago • 4 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.

dmitsuki avatar Aug 06 '20 06:08 dmitsuki

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

salpalvv avatar Aug 06 '20 19:08 salpalvv

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?

dmitsuki avatar Aug 06 '20 19:08 dmitsuki

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.

salpalvv avatar Aug 06 '20 20:08 salpalvv

Note that you can use Storage::get_mut to mutably get certain component for an entity.

WaDelma avatar Aug 09 '20 09:08 WaDelma