Search Unity

Access SharedComponentData in IJobProcessComponentData possible?

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

  1. The5

    The5

    Joined:
    May 26, 2017
    Posts:
    19
    I cant see anything in the unit tests that suggests it is possible.

    I suppose I need to go with IJobChunk in these cases, huh?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    You can’t use SCD directly in job because it’s not part of chunk actually, chunks store only SCD value index (not yet but as Joachim says, Unity doing it for future releases) you only can manipulate SCD index inside job.
     
    Last edited: Jan 18, 2019
  3. The5

    The5

    Joined:
    May 26, 2017
    Posts:
    19
    Huh? It should generally be possible to [ReadOnly] access SCD from inside a job, right?

    MeshInstanceRenderer does it too with ArchetypeChunkSharedComponentType.

    Code (CSharp):
    1.         struct MapChunkRenderers : IJobParallelFor
    2.         {
    3.             [ReadOnly] public NativeArray<ArchetypeChunk> Chunks;
    4.             [ReadOnly] public ArchetypeChunkSharedComponentType<MeshInstanceRenderer> MeshInstanceRendererType;
    5.             public NativeMultiHashMap<int, int>.Concurrent ChunkRendererMap;
    6.  
    7.             public void Execute(int index)
    8.             {
    9.                 var chunk = Chunks[index];
    10.                 var rendererSharedComponentIndex = chunk.GetSharedComponentIndex(MeshInstanceRendererType);
    11.                 ChunkRendererMap.Add(rendererSharedComponentIndex, index);
    12.             }
    13.         };
     
  4. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    ArchetypeChunkSharedComponentType doesn't work (for me at least) unless you call complete on the job that uses it before returning. Not doing so causes a "The previously scheduled job reads from the NativeArray(system.sharedcomponenttypename)..." error. Just a warning if you encounter that.

    You can use a copy of the shared component in a job though, if you happen to know it beforehand, because you can access the shared component data in the main thread(via EntityManager.GetAllUniqueSharedComponentData). In my case I had some entities used as spawn events, each entity has a sharedcomponent that is copied to the spawned entity. Because each unique shared component is it's own chunk I can use that to generate "per-chunk" traits(as several nativelists, one has copies of the shared component) that I want the spawned entities to have, prior to starting the jobs. Then inside an IJobChunk, iterate over the spawner entities and use the chunk/job index to access the arrays I made.
     
    Last edited: Jan 18, 2019
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    That's not accessing the ISharedComponentData, just the index.
    You can't use the index to access the ISharedComponentData till outside of the job.
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
     
  7. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    There's a new ArchetypeChunk.GetSharedComponentData in preview 23 so maybe that's possible now? I haven't looked at it though.
     
  8. The5

    The5

    Joined:
    May 26, 2017
    Posts:
    19
    @eizenhorn & @tertle:
    Duh! Sorry for that oversight, I get it now. Thanks for clearing it up!

    @RecursiveEclipse
    Passing a copy to the job was my immediate reaction to this as well.
    Thanks for sharing your implementation approach!

    @jooleanlogic
    Oh nice! I'll have a look at this.