Skip to content

spawn_batch creates incorrect relationships when RelationshipTarget is initially empty #19275

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

Open
venhelhardt opened this issue May 18, 2025 · 0 comments · May be fixed by #19276
Open

spawn_batch creates incorrect relationships when RelationshipTarget is initially empty #19275

venhelhardt opened this issue May 18, 2025 · 0 comments · May be fixed by #19276
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Comments

@venhelhardt
Copy link
Contributor

Bevy version

The release number or commit hash of the version you're using: 45ba5b9f0347710d0b84979631fae6f690434d62

What you did

This test is failing:

#[test]
fn spawn_batch() {
    #[derive(Component)]
    #[relationship(relationship_target = LikedBy)]
    struct Likes(pub Entity);

    #[derive(Component)]
    #[relationship_target(relationship = Likes)]
    struct LikedBy(Vec<Entity>);

    let mut world = World::new();
    let a = world.spawn_empty().id();
    let b = world
        .spawn_batch((0..10).map(|_| Likes(a)))
        .collect::<Vec<_>>();

    assert_eq!(world.entity(a).get::<LikedBy>().unwrap().0, b);
}

What went wrong

When using World::spawn_batch with an initially empty RelationshipTarget, relationships between entities are not established.

  • what were you expecting? Each entity in the batch should establish a proper relationship component, just like when using World::spawn in a loop.
  • what actually happened? When using World::spawn_batch to spawn multiple entities referencing the same target entity, and that target has no RelationshipTarget, each subsequent command overwrites the RelationshipTarget created by the previous one. This happens because World::spawn_batch defers commands execution, and hooks that update the RelationshipTarget run before previous commands are flushed. As a result, every iteration sees an empty RelationshipTarget on the target and reinitializes it.

Additional information

  • workarounds that you used:
    • Spawn entities individually in a loop instead of using spawn_batch
    • Spawn one entity first to initialize the target's relationship, then use spawn_batch for the rest within the same target group
@venhelhardt venhelhardt added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels May 18, 2025
@greeble-dev greeble-dev added A-ECS Entities, components, systems, and events S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Needs-Triage This issue needs to be labelled labels May 19, 2025
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-Bug An unexpected or incorrect behavior S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
2 participants