Skip to content

Relationship Ordering not maintained #19687

Open
@Trashtalk217

Description

@Trashtalk217

Bevy version

main (2078137)

What you did

I was messing around with some internal tests, and I found the following problem in relationship_source_collection.rs

#[test]
fn entity_index_map() {
    #[derive(Component)]
    #[relationship(relationship_target = RelTarget)]
    struct Rel(Entity);

    #[derive(Component)]
    #[relationship_target(relationship = Rel, linked_spawn)]
    struct RelTarget(EntityHashSet);

    let mut world = World::new();
    let _ = world.spawn_empty(); // <-- THIS WAS ADDED
    let a = world.spawn_empty().id();
    let b = world.spawn_empty().id();
    let c = world.spawn_empty().id();

    let d = world.spawn_empty().id();

    world.entity_mut(a).add_related::<Rel>(&[b, c, d]);

    let rel_target = world.get::<RelTarget>(a).unwrap();
    let collection = rel_target.collection();

    // Insertions should maintain ordering
    assert!(collection.iter().eq(&[d, c, b])); // <-- THIS FAILS

    world.entity_mut(c).despawn();

    let rel_target = world.get::<RelTarget>(a).unwrap();
    let collection = rel_target.collection();

    // Removals should maintain ordering
    assert!(collection.iter().eq(&[d, b]));
}

What went wrong

The assertion fails, and after some very crude debugging, I found that collection.iter() returns

3v0#4294967292, 2v0#4294967293, 4v0#4294967291

instead of

4v0#4294967291, 3v0#4294967292, 2v0#4294967293

This leads me to believe that relationship ordering is not working correctly, if offsetting the entities by 1 can cause this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorD-TrivialNice and easy! A great choice to get started with BevyS-Needs-TriageThis issue needs to be labelled

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions