Skip to content

Entities::resolve_from_id of freed entity makes Entities::contains return true #19012

@urben1680

Description

@urben1680

Bevy version

0.16

What you did

I noticed this when I tried to check if an entity by index is currently in use.

I ran this test:

let mut world = World::new();
let entity_mut = world.spawn_empty();
let existing = entity_mut.id();
let entity_index = existing.index();
entity_mut.despawn();
let freed = world
    .entities()
    .resolve_from_id(entity_index)
    .expect("freed entities are still allocated");
assert_eq!(
    world.entities().contains(freed),
    false,
    "before: {existing}, freed: {freed}"
)

What went wrong

The test failed:

assertion `left == right` failed: before: 0v1, freed: 0v2
  left: true
 right: false
stack backtrace:

This is unexpected because resolve_from_id docs say:

Get the Entity with a given id, if it exists in this Entities collection Returns None if this Entity is outside of the range of currently reserved Entities

Note: This method may return Entities which are currently free Note that contains will correctly return false for freed entities, since it checks the generation

I would think contains works as a way of verifying if the entity of this index is freed. However this is only done by get as that method also checks the current archetype.

I am unsure what the issue is here, the docs or the implementation. But I think that contains should never return true for a freed entity no matter if the generation value has been increased by free or I took a good guess when using Entity::from_bits.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions