-
Notifications
You must be signed in to change notification settings - Fork 37
Relationships
genar edited this page May 30, 2023
·
3 revisions
Sometimes you want or need to reference other entities... but what is the best way to do that? You could store your own lists in components, but that is unfortunately always a lot of effort.
That's why there are "Relationships".
// Tag, can also store data.
public struct ParentOf{}
var parent = world.Create();
var childOne = world.Create();
var childTwo = world.Create();
// Adds relations between entities.
parent.AddRelationship<ParentOf>(childOne);
parent.AddRelationship<ParentOf>(childTwo, new ParentOf(...));
// Has, get and set relation
var hasRelationship = parent.HasRelationship<ParentOf>();
var relationToChildOne = parent.GetRelationship<ParentOf>(childOne);
parent.SetRelationship<ParentOf>(childOne, new ParentOf());
// Iterate all relations
ref var parentOfRelation = ref parent.GetRelationships<ParentOf>();
foreach(var child in parentOfRelation){
...
}
// Remove relations.
parent.RemoveRelationship<ParentOf>(childOne);
parent.RemoveRelationship<ParentOf>(childTwo);
Adding a relationship for the first time acts as a structural change for both the source and the target entity