Tainted \\ Coders

Iterate over all entities in a world

Last updated:

We can use an EntityRef which is a readonly reference to an Entity and its components.

// A read-only reference to a particular [`Entity`] and all of its components
#[derive(Copy, Clone)]
pub struct EntityRef<'w> {
    world: &'w World,
    entity: Entity,
    location: EntityLocation,
}

We can access EntityRef by using an exclusive system and reading directly from the world.

fn do_crazy_things(world: &mut World) {
    // we can do anything with any data in the Bevy ECS here!
    for entity_ref in world.iter_entities() {
        // ...
    }
}

This type of system cannot run in parallel and should run at the end of all other systems. Its performance will be limited.