Skip to content

Spawn batch with relationship #19519

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

gwafotapa
Copy link

Objective

Fixes #19356
Issue: Spawning a batch of entities in relationship with the same target adds the relationship between the target and only the last entity of the batch. spawn_batch flushes only after having spawned all entities. This means each spawned entity will have run the on_insert hook of its Relationship component. Here is the relevant part of that hook:

            if let Some(mut relationship_target) =
                target_entity_mut.get_mut::<Self::RelationshipTarget>()
            {
                relationship_target.collection_mut_risky().add(entity);
            } else {
                let mut target = <Self::RelationshipTarget as RelationshipTarget>::with_capacity(1);
                target.collection_mut_risky().add(entity);
                world.commands().entity(target_entity).insert(target);
            }

Given the above snippet and since there's no flush between spawns, each entity finds the target without a RelationshipTarget component and defers the insertion of that component with the entity's id as the sole member of its collection. When the commands are finally flushed, each insertion after the first replaces the one before and in the process triggers the on_replace hook of RelationshipTarget which removes the Relationship component from the corresponding entity. That's how we end up in the invalid state.

Solution

I see two possible solutions

  1. Flush after every spawn
  2. Defer the whole code snippet above

I don't know enough about bevy as a whole but 2. seems much more efficient to me. This is what I'm proposing here. I have a doubt though because I've started to look at #19348 that 1. would fix as well.

Testing

I added a test for the issue. I've put it in relationship/mod.rs but I could see it in world/spawn_batch.rs or lib.rs because the test is as much about spawn_batch as it is about relationships.

Copy link
Contributor

github-actions bot commented Jun 6, 2025

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@alice-i-cecile alice-i-cecile added this to the 0.16.2 milestone Jun 6, 2025
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 6, 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
Development

Successfully merging this pull request may close these issues.

Children not updated correctly when spawning ChildOf components using World::spawn_batch
2 participants