Skip to content

Custom 1:1 relationship not inserted when using .with_related() #22959

@TravisRybka

Description

@TravisRybka

Bevy version and features

  • 0.18.0 (repro'd on main at cf35f37)

What you did

Created a custom 1:1 relationship, and spawned entities using EntityCommands::with_related:

#[derive(Component)]
#[relationship(relationship_target = MyFriend)]
struct FriendOf(pub Entity);

#[derive(Component)]
#[relationship_target(relationship = FriendOf)]
struct MyFriend(Entity);

Spawning entities:

fn spawning(mut commands: Commands) {
    let mut top_entity_command = commands.spawn_empty();

    // Bugged Behavior
    top_entity_command.with_related::<FriendOf>(Bugged);

    // Workaround
    let top_entity_id = top_entity_command.id();
    commands.spawn((FriendOf(top_entity_id), NotBugged));
}

// Marker Trait
#[derive(Component)]
struct Bugged;

// Marker Trait
#[derive(Component)]
struct NotBugged;

What went wrong

Using .with_related::<FriendOf>() doesn't insert the FriendOf component on the source entity.

fn verify(
    bugged: Single<Has<FriendOf>, With<Bugged>>,
    not_bugged: Single<Has<FriendOf>, With<NotBugged>>,
) {
    // Prints "false"
    println!("Bugged:\t\t{}", bugged.into_inner());
    // Prints "true"
    println!("Not bugged:\t{}", not_bugged.into_inner());
}

Additional information

This seems to be specific to 1:1 relationships (i.e. impl RelationshipSourceCollection for Entity). If I change the relationship target to be a Vec<Entity>, there is no bug. E.g.

-- struct MyFriend(Entity);
++ struct MyFriend(Vec<Entity>);

With the above diff, then the verify system returns "true" and "true"

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorS-Needs-InvestigationThis issue requires detective work to figure out what's going wrong

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions