Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Should buffers be removed when empty

Discussion in 'Entity Component System and C# Job system' started by LukePammant, Dec 5, 2018.

  1. LukePammant

    LukePammant

    Joined:
    Mar 10, 2015
    Posts:
    28
    Title says it all - should I call
    EntityManager.RemoveComponent<DamageBuffer>()
    when it is empty? This would prevent all damage related systems from running on an entity when it's buffer is empty (because it doesn't exist).

    If we should remove the buffer when it is empty - is there an easy way to determine in a job if a buffer exists or not? The only way I can think of to do this is to have two jobs with two different entity filtering criteria:

    JobA - Is given entities that dont have a
    DamageBuffer
    , adds the buffer to the entity, then adds an element to it
    JobB - A job that does the same thing as JobA but just appends an element
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,442
    Personally I wouldn't be touching buffer component, if you planning using and reusing it.
    I think this my imply some overhead, specially when you want to add it again.
    And data primary is stored in chunks if fits.

    What I would do instead, use tag component, without data.
    DamageTag. and add / remove it instead.
    Should be much "cheaper" to do so.
    I think this was even recommended method, for filtering entities.
     
  3. LukePammant

    LukePammant

    Joined:
    Mar 10, 2015
    Posts:
    28
    Thanks - I should have prefaced that I have a buffer for Damage so that I can add multiple damage's to an entity. If an Entity gets hit by a shotgun blast it would need multiple Damage Components added to it. This way the DamageViewSystem and ApplyDamageSystems can apply/show the correct amounts/sources of damage.

    With that being said - perhaps I can add a different DamageDealtThisFrameTag or something that is only added to the component if the DamageBuffer had something added to it this frame? This could potentially bring me back to one of my above questions - how can I tell if the tag/component exists on the entity already from within a job? If I have multiple systems that can add damage to an entity I'd need a way of making sure a different system didn't already add the Tag.
     
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    785
    To determine existence of component in a job you have 2 choices : bring ComponentDataFromEntity<T> and use cdfe.Exist(entity), or if using chunk iteration bring ArchetypeChunkComponentType<T> then when you get to the chunk (ArchetypeChunk) use ac.Has(acct)

    Removing DamageBuffer when it is empty is beneficial when you need something to do to those with/without damages as a whole, because removing component change its chunk making it easier to query later. If you can make this data movement cost (and a sync point) worthwhile then it might be great. (For example what if you want to heal all damaged units by a certain amount, you could be iterating through just those with damages if you add/remove the component)
     
    Last edited: Dec 5, 2018
    LukePammant and Antypodish like this.
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,442
    Following Single Responsibility Principle, you should have only one system, which adds the tag. Same, or other system should be responsible for removing tag. Hence you guarantee, you will never have situation, where two, or more systems adding same tag.
     
    LukePammant likes this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,442
    Yep. Probably depends how often query and how often removing / adding. But you are right otherwise.
     
    LukePammant likes this.