Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How often is too often for modifying a SharedComponentData?

Discussion in 'Entity Component System' started by Guedez, Jan 17, 2019.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    How too often is too often? once a frame? Also, is the issue changing which SharedComponentData is attached to a entity, or changing the content of a SharedComponentData?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Ask more specific. Often is dependent on count of entites, count of types in archetype, etc.
    Both - changing SCD type changes archetype where entity locates, changing SCD value splits and moves entities in archetype to different chunk.
     
    Antypodish likes this.
  3. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    So if I have about 10 "ingredient" entities, which all share the same "cooking pot" SharedComponentData, changing the "cooking pot".temperature will cause chunk changes on the entities?
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Every different SCD value is different chunk group
     
  5. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    So, changing a value of a existing SCD will create a whole new chunk, and move all entities to the new chunk?

    It feels like it's better to have an array keeping track which entities are contained in which cooking pot, and iterate through their 'container temperature' component and change that instead of using a SCD, even though all of them will always have the same temperature value if they are in the same cooking pot
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    10, 100, or even 1000 probably wont break the bank.
    But if you can avoid changing Shared Component on many entities that best way to go.
    As you said, either use arrays, or use Component Data (not shared), to mark entities.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Best usage for SCD is use it for filtering, as Unity recommend, it’s helps also do your grouping and use it for chunk iteration.
     
  8. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    If cooking pot is a reference type (inside the struct type ISharedComponentData), and you happen to hold it after adding it to all entities then you change .temperature from that instance in hope for the change to affect all entities, chunk will not change. All entities remembers the SCD via index. There is only one instance of SCD stored, because uniqueness is determined by hashing, hashing of reference type is the address's hash.

    If you clone a new reference type cooking pot with a new temperature then use SetSharedComponentData to one of 10 entities, then that 1 entity will move out to a new chunk because cloned instance has a new address.

    If cooking pot is a struct type then changing the kept instance will not affect the already added SCD. If you change then set again it would cause that entity receiving set to move chunk. But this time hash changed because of value type change.
     
    Antypodish, Abbrew and Guedez like this.
  9. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    So long I can change the SCD values without changing the hash (by changing a value of a reference type like in your example) the entities will not move chunks and therefore cause no performance problems? Seems reasonable, I now also understand why changing a value (and therefore, the hash) would cause the chunk move
     
  10. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    Going off what you both said, if a SCD shared by different entities consists of say a NativeArray, which is a reference type, then changing the elements of the NativeArray (but not assigning it to a new NativeArray) would not cause a performance drop due to moving entities across chunks?
     
  11. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    No, basically if you do not call SetSharedComponentData nothing could magically moves.
     
    Abbrew likes this.