Search Unity

Iterating on all entities in jobs that have a specific component w/o receiving an actual component

Discussion in 'Entity Component System' started by xVergilx, Jul 15, 2019.

  1. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    There's IJobForEachWithEntity<T> that receives a component that is required to be iterated upon.
    Is there a way to iterate only entities without fetching an actual component to the job?

    E.g. which method would look like:
    Code (CSharp):
    1. public void Execute(Entity entity, int index) {
    2. }
    instead of
    Code (CSharp):
    1. private struct DisposeTempJob : IJobForEachWithEntity<T> {
    2.             public void Execute(Entity entity, int index, ref T c0) {  
    3.             }
    4. }
    I'm assuming that it has at least some overhead of sending refs vs sending only entity array.

    What I'm trying to achieve is to remove entities at the end of the simulation frame that are "marked" by the specific component.
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    xVergilx likes this.
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Well, I've marked component right now as [ReadOnly].
    Probably IJobChunk is the best candidate for this kind of iteration. But IJobForEachWithEntity is too convienient to use in comparison to IJobChunk.
     
  4. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    The performant way to do that is to query for that specific component and then use a batch command like:
    Code (CSharp):
    1. EntityCommandBuffer.DestroyEntity(EntityQuery);
    2.  
     
    xVergilx and florianhanke like this.
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Oof. That's exactly what I've wanted. Thanks! :)
     
    digitaliliad likes this.
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    S_Darkwell and xVergilx like this.