Skip to content

Commit 71ad427

Browse files
authored
Make 1-to-1 Relationship clearer in docs (#24116)
# Objective Make the possibility of 1-to-1 `Relationship` clearer in the docs, so that people know it's an option. (There's already a passing mention of it at the top, but that doesn't show that it's supported in code.) ## Solution Added an example to the doc comment to show how it's done, and explained what happens if you try to add another entity anyway. ## Testing Ran `cargo doc` and looked at the new docs to see if they're ok.
1 parent 739b1e5 commit 71ad427

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

  • crates/bevy_ecs/src/relationship

crates/bevy_ecs/src/relationship/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ use log::warn;
6464
/// pub struct Children(Vec<Entity>);
6565
/// ```
6666
///
67+
/// A one-to-one relationship can be created by putting a single [`Entity`] in the [`RelationshipTarget`]'s field.
68+
/// In that case, if another entity is added to the relationship, the original entity is removed.
69+
///
70+
/// ```
71+
/// # use bevy_ecs::component::Component;
72+
/// # use bevy_ecs::entity::Entity;
73+
/// #[derive(Component)]
74+
/// #[relationship(relationship_target = View)]
75+
/// pub struct ViewOf(pub Entity);
76+
///
77+
/// #[derive(Component)]
78+
/// #[relationship_target(relationship = ViewOf)]
79+
/// pub struct View(Entity);
80+
/// ```
81+
///
6782
/// When deriving [`RelationshipTarget`] you can specify the `#[relationship_target(linked_spawn)]` attribute to
6883
/// automatically despawn entities stored in an entity's [`RelationshipTarget`] when that entity is despawned:
6984
///

0 commit comments

Comments
 (0)