legion icon indicating copy to clipboard operation
legion copied to clipboard

How to iterate over all entries in a world?

Open selvavm opened this issue 3 years ago • 1 comments

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

selvavm avatar Mar 01 '21 12:03 selvavm

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.

Rua avatar Mar 02 '21 08:03 Rua