Description
What problem does this solve or what need does it fill?
Bevy does not allow a relationship to point to its own entity. Doing so logs a warning and removes the component. I have not found a clear reason for this limitation.
Semantically, I would argue that an entity having a relationship to itself is perfectly valid in many cases. Likes(self)
, EmployedBy(self)
, TalkingTo(self)
, Healing(self)
, and many more relationships make sense and should be valid.
In my case, in Avian, I have a component called ColliderParent
that attaches a collider to a rigid body. For 0.16.0-rc.1, I changed it to a ColliderOf
relationship, since I think that makes semantic sense, and I wanted to benefit from the various niceties provided by relationships. However, I ran into this problem, as colliders and rigid bodies can be on the same entity (and more often than not, they are), requiring the relationship to point to its own entity, and removing the collider.
I fixed this problem by manually implementing Relationship
with these limitations removed, but the fact that I had to do this is not great, and it is more prone to future breakage.
There may be some potential questions around how relationship queries work for self-relationships, and it may be possible to get infinite recursion if querying for the target entity recursively, but I believe a lot of these same questions apply to cyclical relationships (which are generally allowed afaik) and may be something that people just have to consider in their own code, or something we warn about.
What solution would you like?
Allow a relationship to point to its own entity. This mainly just involves removing the checks and warnings for target_entity == entity
.
Relationship::on_replace
must also be made non-panicking in the event that the entity is despawned. I ran into this in Jondolf/avian#677.
Of course, for some relationships like ChildOf
, it may not make sense to allow pointing to self. We could have a flag such as disallow_self
in the relationship
macro attribute to configure this per-relationship.
What alternative(s) have you considered?
- Keep the current limitations, and disallow self-relationships.
- Use satellite entities. Oftentimes this might not make sense however, or is undesirable for ergonomic reasons or performance.