Search Unity

Is there a way to Execute a System when a chunk had a structural change?

Discussion in 'Entity Component System' started by Opeth001, Feb 8, 2020.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Hello Everyone,
    I'm looking for a way to execute a job only on chunks that had a structural change, like an entity has been removed or a new entity joined the chunk.
    My use case:
    I'm updating a ChunkComponentData which require to calculate a component on all it's entities. This is a heavy calculation and require to happen only when a change of occurred.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    You can use a reactive system to detect adds (add the secondary tag component and then mark the entity's new chunk as dirty in a chunk component). You can also keep count of the entities in the chunk since last update and if there's a mismatch, then also dirty the chunk. With both mechanisms, you can cover the case where an entity is both removed and added to a chunk within a frame.
     
    Opeth001 likes this.
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Thanks @DreamingImLatios !
    i just found out that it's already implemented.

    Detecting changes in data
    Use component change versions to detect when a chunk component needs to be updated for a given chunk. ECS updates the component versions for a chunk whenever the data in a component is accessed as writable or when an entity is added or removed from the chunk.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    The solution I proposed filters out the "component is accessed as writable part". But yes if you want potential value changes as well, the built-in solution works great. Keep in mind that if you use an Entities.ForEach with a ref of that component, no matter whether or not you actually do any writing, that alone will trigger the change detection.
     
    Opeth001 likes this.