legion
legion copied to clipboard
How to iterate over all entries in a world?
Hi, Thanks for this crate.
I want to iterate over each entity in a world and add a new component. I tried below code but have issues with Borrow checker. How can I achieve it?
let mut query = <Entity>::query();
for entity in query.iter(&user_world) {
let entry = user_world.entry(*entity);
}
Update: I could see it is implemented in the pull request #102 but I am not able to find the function
The problem is that you are trying to modify the world while also iterating through it. That would bring all kinds of safety issues if it were allowed. What you can do, though, is use a CommandBuffer
to store the component additions during the iteration, and then flush it in one go once the iteration is over.