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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions crates/bevy_ecs/src/relationship/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,20 @@ pub trait Relationship: Component + Sized {
world.commands().entity(entity).remove::<Self>();
return;
}
if let Ok(mut target_entity_mut) = world.get_entity_mut(target_entity) {
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);
}
if world.get_entity(target_entity).is_ok() {
world
.commands()
.entity(target_entity)
.entry::<Self::RelationshipTarget>()
.and_modify(move |mut relationship_target| {
relationship_target.collection_mut_risky().add(entity);
})
.or_insert({
let mut target =
<Self::RelationshipTarget as RelationshipTarget>::with_capacity(1);
target.collection_mut_risky().add(entity);
target
});
} else {
warn!(
"{}The {}({target_entity:?}) relationship on entity {entity:?} relates to an entity that does not exist. The invalid {} relationship has been removed.",
Expand Down Expand Up @@ -458,4 +462,31 @@ mod tests {
assert!(world.get_entity(child).is_err());
assert!(!world.entity(parent).contains::<RelTarget>());
}

// Spawn a batch of entities in relationship with a target entity
#[test]
fn spawn_batch_with_relationship() {
use crate::relationship::{Relationship, RelationshipTarget};

#[derive(Component)]
#[relationship(relationship_target = RelTarget)]
struct Rel(Entity);

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

let mut world = World::new();
let target = world.spawn_empty().id();
let rel_entities = world
.spawn_batch((0..10).map(|_| Rel(target)))
.collect::<Vec<_>>();

for &entity in &rel_entities {
assert!(world.get::<Rel>(entity).is_some_and(|r| r.get() == target));
}
assert!(world
.get::<RelTarget>(target)
.is_some_and(|rt| rt.len() == 10));
}
}