Search Unity

Summing up Health data among multiple Entities

Discussion in 'Entity Component System' started by Vacummus, Nov 20, 2018.

  1. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    I have a use case where an Organism is compromised of 8 entities, and each entity has it's own Health { float Amount; } component. But I would like to keep track of the total health of all 8 entities and having trouble figuring out a good way to do that. Any thoughts on this?

    Here is my data layout:
    • Entity1 [ Health, TotalHealth ]
    • Entity2-8 [Health, TotalHealthRef ]
     
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    If you mean a total that automatically recalculated whenever data in an entity changes I think it is difficult, since ECS is based on update turns and nothing updates immediately.

    Since total health is not a part of organism it should belong to a system, like TotalHealthCalculationSystem. Have it sum up the health and store in itself (system's code public fields) or its singleton entity that keeps summation of various things, or in each organism like you do but that's a lot of writes. Then all user of total health must have [UpdateAfter(typeof(TotalHealthCalculationSystem))] and all changes to the health must be before.
     
  3. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Can you give some details on what is the specific complication beyond simple summation?
    Is damage propagated from the head to the children or the other way around or both?

    I have a possibly similar scenario in my game which has to track the total fuel from a bunch of fuel barrels. In my case, changes are propagated in both directions.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (CSharp):
    1. struct TotalHealthRef {  Entity TotalHealth; }
    Then use

    Code (CSharp):
    1.  ... : IJobProcessComponentData<TotalHealthRef>
    2. {
    3.    public ComponentDataFromEntity<TotalHealth> Healths;
    4.     ...
    5. }
    using ScheduleSingle to perform it all on one job.


    If you have many many creatures and you want to perform summing of multiple creates in parallel to each other. Then using DynamicBuffer on TotalHealth root object, pointing to indivdual health is the better approach.
     
  5. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    ^ Thanks Joachim. Going to give that a try.

    The damage is propagated from the children to the head. Though do you need to propagate in both directions? I would think you just need to propagate from one of the two directions to keep data in sync.