Search Unity

adding and removing components vs containing everything in a single component

Discussion in 'Entity Component System' started by Ashkan_gc, Jan 23, 2019.

  1. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    I'm trying to wrap my head around ECS and a part of it is trying to think how to implement each specific case in ECS.
    My current example is a character's states,
    Is it a better idea to have components for states [A,B,C] and systems for those and add/remove them when the entity changes state or we should have a state component which contains the fact that which state our entity is in and a StateSsstem which processes that.

    I guess it boils down to how big the component will be with all of the states data and how heavy adding/removing components is, does it invalidate most chunks when we add a component to an entity, will it cause the chunk for systems which their filters is affected by the modification to be invalidated? what will happen under the head.

    The answer to these seem to be true but since the docs don't warn about them like shared component data and tag components will be used instead of callbacks, I was wondering how is it working?
     
  2. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    As discussed in some threads here (with benchmarks...)

    It depends on how hoften you change you data. If this happens quite often than you should use a "all in one solution".

    Don't change (add/remove components) to often. Each change will create a new archetype and therefore a memcopy in a different chunk of that entity. This will also result in some fragmentation issues...

    So best practice would be to create archetypes and don't change them!
     
  3. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    Thanks
    Yes that makes sense but then why Unity advices us to use adding/removing components or system state components instead of using callbacks?
     
  4. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    Because in most scenarios the performance gain filtering by archetype overcame the overhead of changing the entity archetype. Now adding multiple components will change the archetype multiple* times but with some batch optimizations in the future the overhead will be lessened. So adding and removing components looks like a better approach to start and change only you really know that's needed, with days to confirm. To finish, remember if you the need the same filter in more than one system, filtering by value will be heavier.

    *EntityManager has a new add method that support multiple components but I'm not sure if it's already optimized since other batchable methods, like the AddComponent using ComponentGroup, aren't.

    []'s