-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
One-to-One Relationships #18087
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
One-to-One Relationships #18087
Conversation
cb62c3e to
ecbf16d
Compare
| } | ||
|
|
||
| fn remove(&mut self, entity: Entity) { | ||
| if *self == entity { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definitely feels sus. What if we impl'ed this for Option<Entity> instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it that way at first. It felt weird to me that it's expressable for the RelationshipTarget to be None while the Relationship still points to that entity. To be fair - manually changing the RelationshipTarget to None is just as weird as setting the RelationshipTarget to some random entity (we mess up the relationship).
This felt like the more minimal change that preserves symmetry (but I'm open to changing my mind)
… one_to_one_relationships
RelationshipSourceCollection for EntityThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! Though I like the Option idea more personally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. FWIW, I prefer the Entity approach because it better matches how its interacted with; assuming all invariants are upheld, users will always have access to a single valid entity.
|
The #[derive(Component)]
#[relationship(relationship_target = Below)]
pub struct Above(Entity);
#[derive(Component)]
#[relationship_target(relationship = Above)]
pub struct Below(Option<Entity>);@alice-i-cecile Is it possible for this to be considered for 0.16? |
|
Yeah I see what y'all mean. There should always be a 1-1 relationship between the two entities as an invariant. Not something to consider for this PR perhaps, but I wonder if this is a code smell then. It sorta weird to use the word "collection" in the code when really it's just one item. |
|
Yep, I'll make the call today in my merge train :) The 0.16 milestone isn't exhaustive: it just captures "stuff that I would consider blocking the release for". |
This comment convinced me. Merging. |
# Objective One to one relationships (added in #18087) can currently easily be invalidated by having two entities relate to the same target. Alternative to #18817 (removing one-to-one relationships) ## Solution Panic if a RelationshipTarget is already targeted. Thanks @urben1680 for the idea! --------- Co-authored-by: François Mockers <[email protected]>
# Objective One to one relationships (added in #18087) can currently easily be invalidated by having two entities relate to the same target. Alternative to #18817 (removing one-to-one relationships) ## Solution Panic if a RelationshipTarget is already targeted. Thanks @urben1680 for the idea! --------- Co-authored-by: François Mockers <[email protected]>
# Objective One to one relationships (added in bevyengine#18087) can currently easily be invalidated by having two entities relate to the same target. Alternative to bevyengine#18817 (removing one-to-one relationships) ## Solution Panic if a RelationshipTarget is already targeted. Thanks @urben1680 for the idea! --------- Co-authored-by: François Mockers <[email protected]>
Objective
Minimal implementation of directed one-to-one relationships via implementing
RelationshipSourceCollectionforEntity.Now you can do
Future Work
It would be nice if the relationships could be fully symmetrical in the future - in the example above, since
Aboveis the source of truth you can't addBelowto an entity and haveAboveadded automatically.Testing
Wrote unit tests for new relationship sources and and verified adding/removing relationships maintains connection as expected.