Description
What problem does this solve or what need does it fill?
Currently after all relationships have been removed for a given RelationshipTarget
component, the component is left on the Entity
in an empty state.
I would like an option on the derive macro to have the RelationshipTarget
component removed upon reaching zero relations.
The primary benefit is it allows more powerful & flexible querying of entities that use the relation.
User could filter entites based on whether they have relationships of a given type or not. Currently just because an entity has a RelationshipTarget
component doesn't mean it actually has any current relationships. The user must query for the RelationshipTarget
and check the collection for whether there is any actual related entities.
This is a strict overhead if the user just wants to take action on entites if they have relationships without needing to reference the related entities themselves.
For example, if Children
had this behaviour, you would be able to guarantee all entities found by Query<Entity, With<Children>>
are not leaf nodes, and Query<Entity, Without<Children>>
are leaf nodes. Today to find all leaf nodes you must query the relation itself (extra data to fetch) Query<(Entity, &Children)>
, and interrogate every entity if !children.is_empty()
.
The secondary benefit is it makes it harder to misuse certain relationships. For example, today a relationship that uses Entity
as it's collection will fall back to Entity:PLACEHOLDER
when the relationship is ended, which is a footgun waiting to happen. It's too easy to forget to null check Entity
.
What solution would you like?
The first thing that comes to mind is an additional configuration option on the relationship_target
macro, eg remove_on_empty
.
#[relationship_target(relationship = HealthTextRel, linked_spawn, remove_on_empty)]