Search Unity

Suggestion: AddOrSetComponent in EntityCommandBuffer

Discussion in 'Entity Component System' started by e199, Nov 28, 2018.

  1. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    Hey

    In my small code base I already have a lot of places like this

    Code (CSharp):
    1.  
    2. if (hasHeat)
    3. {
    4.     Commands.SetComponent(_threadIndex, entity, heat);
    5. }
    6. else
    7. {
    8.     Commands.AddComponent(_threadIndex, entity, heat);
    9. }
    10.  
    It would probably work fine if SetComponent would implicitly Add it, if it's not exists.
    But to stay clear about what API would actually do - I propose to add something like AddOrSetComponent
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    This has been discussed a few times without any official word on it I believe.

    I used to be all for this and found multiple cases where I needed it. However over time with move towards chunk iteration and a better understanding of how ECS works, I've changed how I design systems and I've come to believe that AddOrSet promotes bad design.

    Adding and removing components is something I believe should be avoided where possible. Constantly (every frame) moving entities between chunks is costly and really limits the potential performance for your systems.

    That's not to say that there aren't use cases for this and that adding it wouldn't be helpful for these, but if you're constantly finding you need it you might want to consider refactoring your design.
     
    Last edited: Nov 28, 2018
    noio and e199 like this.
  3. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    Yep. After thinking about it and rewriting some parts, amount of usages has been reduced. (total amount of dependencies has been dropped)
    At the cost of introducing new barrier...