Zay-ES hierarchical entity question


#1

I was wondering what design patterns people are using for the following Zay-es use case:

I have a ShipHull entity and an Engine entity. The ShipHull and Engine have Health components. The ShipHull also has a TotalHealth component. Now, I want to create a System called UpdateTotalHealth. It needs to update the TotalHealth component of each ship by adding the ship’s health component and the engine health component of that ship. Since there is no intrinsic way in Zay-es to let the system know that engine 1 entity belongs to ship 1, how would you solve for this?
Just to complicate things a bit, you can have many engines on one ship hull.


#2

Link component. In ES the child points to parent. I do this for various things which are somehow connected.


#3

Is there a facility built into Zay-es for this or do I have to create a Parent component for entities that contains the parent’s EntityID?


#4

Yep you have to create a component which holds the parent id and of course you also need a specific system which cares for the relationship in your wished way. I guess this will then be your total damage system or whatever you call it.


#5

These sorts of parent components are easy to write and you will quite likely have several different ones or slightly different ones depending on your game. So, no, Zay-ES doesn’t provide it. (CreatedBy is a decent template for one, though.)

Another example might look like:

public class OwnedBy implements EntityComponent, PersistentComponent {
    private EntityId owner;

    protected OwnedBy() {
    }

    public OwnedBy( EntityId owner ) {
        this.owner = owner;
    }

    public EntityId getOwner() {
        return owner;
    }
}

…or InContainer… or Attachment… or Link… or… just very game dependent. And sometimes they will have additional fields.

Sometimes the parent link will be embedded as part of a component that does a different job. For example, a Damage component may point to the entity that is damaged as the Damage component is attached to a damage entity (there may be many attached to a single entity on a given frame). The damage system then watches for all entities with Damage components and then once a frame applies the damage to the target.

Oh, yeah… “target” is another potentially common component that refers to a ‘parent’.

I picked CreatedBy as an example just because it’s kind of general without leading an implementation. Tracking an entity that creates another entity is either something you need or something you don’t. Any other thing felt game or genre specific.