Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an Entities::iter() method to iterate over all entities when you only have &World #6228

Closed
alice-i-cecile opened this issue Oct 10, 2022 · 2 comments
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@alice-i-cecile
Copy link
Member

What problem does this solve or what need does it fill?

The ordinary approach for getting the list of entities (query for Query<Entity>) doesn't work in contexts where you only have read-only access to the World.

What solution would you like?

Add an Entities::iter() or World::iter_entities() method.

What alternative(s) have you considered?

The following snippet works

        for archetype in world.archetypes().iter() {
            builder.extract_entities(archetype.entities().iter().copied());

Borrowed from @mockersf in https://github.com/bevyengine/bevy/pull/6227/files#diff-717fbf581c75ed1fc65c9755af2bf8a2ddc97c02e89f4044f86efa70a2ea4260L45

Additional context

It's not entirely clear if Entities::iter() can be implemented directly without digging in; I'm not sure if we have all of the information needed there.

@alice-i-cecile alice-i-cecile added D-Trivial Nice and easy! A great choice to get started with Bevy A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Oct 10, 2022
ramirezmike added a commit to ramirezmike/bevy that referenced this issue Oct 12, 2022
ramirezmike added a commit to ramirezmike/bevy that referenced this issue Oct 12, 2022
ramirezmike added a commit to ramirezmike/bevy that referenced this issue Oct 12, 2022
@hymm
Copy link
Contributor

hymm commented Oct 12, 2022

FYI I was able to implement this on Entities, but I prefer #6242 as my method requires filtering out entities with invalid archetypes and creating the Entity from the EntityMeta. Iterating over the archetypes feels more straightforward.

impl Entities {
    /// Returns an iterator over entities in valid archetypes
    pub fn iter(&'_ self) -> impl Iterator<Item = Entity> + '_ {
        self.meta
            .iter()
            .enumerate()
            .filter(|(_, meta)| meta.location.archetype_id != ArchetypeId::INVALID)
            .map(|(index, meta)| Entity {
                generation: meta.generation,
                id: index as u32,
            })
    }
}

ramirezmike added a commit to ramirezmike/bevy that referenced this issue Oct 13, 2022
bors bot pushed a commit that referenced this issue Oct 17, 2022
# Objective

- Add a way to iterate over all entities from &World

## Solution

- Added a function `iter_entities` on World which returns an iterator of `Entity` derived from the entities in the `World`'s `archetypes`

---

## Changelog

- Added a function `iter_entities` on World, allowing iterating over all entities in contexts where you only have read-only access to the World.
@mockersf
Copy link
Member

closed by #6242

james7132 pushed a commit to james7132/bevy that referenced this issue Oct 19, 2022
# Objective

- Add a way to iterate over all entities from &World

## Solution

- Added a function `iter_entities` on World which returns an iterator of `Entity` derived from the entities in the `World`'s `archetypes`

---

## Changelog

- Added a function `iter_entities` on World, allowing iterating over all entities in contexts where you only have read-only access to the World.
james7132 pushed a commit to james7132/bevy that referenced this issue Oct 28, 2022
# Objective

- Add a way to iterate over all entities from &World

## Solution

- Added a function `iter_entities` on World which returns an iterator of `Entity` derived from the entities in the `World`'s `archetypes`

---

## Changelog

- Added a function `iter_entities` on World, allowing iterating over all entities in contexts where you only have read-only access to the World.
Pietrek14 pushed a commit to Pietrek14/bevy that referenced this issue Dec 17, 2022
# Objective

- Add a way to iterate over all entities from &World

## Solution

- Added a function `iter_entities` on World which returns an iterator of `Entity` derived from the entities in the `World`'s `archetypes`

---

## Changelog

- Added a function `iter_entities` on World, allowing iterating over all entities in contexts where you only have read-only access to the World.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
# Objective

- Add a way to iterate over all entities from &World

## Solution

- Added a function `iter_entities` on World which returns an iterator of `Entity` derived from the entities in the `World`'s `archetypes`

---

## Changelog

- Added a function `iter_entities` on World, allowing iterating over all entities in contexts where you only have read-only access to the World.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
None yet
Development

No branches or pull requests

3 participants