Should I add a owner component on every entity that has parent?

74 Views Asked by At

Should I add a owner component on every entity that has parent? if yes, what is the right term for that component. Currently I am using AttachmentComponent that consist of owner Entity, and use it like in the below code.

AttachmentComponent ...
ItemComponent ...

entity.add(attachment);
entity.add(item);
1

There are 1 best solutions below

0
On

If your entities can be hierarchical in nature, why introduce a component to represent the parent entity but instead represent it as an attribute on the entity itself?

public class Entity {
  private Set<Component> components;
  private Entity owner;

  public final boolean hasOwner() {
    return owner != null;
  }

  public void setOwner(Entity owner) {
    this.owner = owner;
  }
}

You could also maintain a list on each Entity with all its relevant children should you need to traverse the entity hierarchy from top-down rather than bottom-up.