Search Unity

How to Jobfy this ComponentSystem-based code?

Discussion in 'Entity Component System' started by nzhangaudio, Aug 13, 2018.

  1. nzhangaudio

    nzhangaudio

    Joined:
    May 26, 2015
    Posts:
    20
    Let's say I have a bunch of source positions that need to be updated to some target positions, and each target's entity information is contained by the source positon's companion EntityHolder component. The only way I can think of is using ComponentDataFromEntity<Position> to take that entity info from that "Position(source) & EntityHolder" ComponentGroup and write to it in a for loop. I wonder how this should be implemented in the job system.

    Code (CSharp):
    1. public class PositioningSystem : ComponentSystem
    2. {
    3.     struct SourceGroup
    4.     {
    5.         public readonly int Length;
    6.         [ReadOnly] public ComponentDataArray<Position> Positions;
    7.         [ReadOnly] public ComponentDataArray<EntityHolder> EntityHolder;
    8.     }
    9.     [Inject] SourceGroup sourceGroup;
    10.  
    11.     [Inject] ComponentDataFromEntity<Position> TargetPosition;
    12.  
    13.     protected override void OnUpdate()
    14.     {
    15.         for (int i = 0; i < sourceGroup.Length; i++)
    16.         {
    17.             TargetPosition[sourceGroup.EntityHolder[i].TargetEntity] = sourceGroup.Positions[i];
    18.         }
    19.     }
    20. }
     
    Last edited: Aug 13, 2018
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Just derive from JobComponentSystem and launch a Job. There is nothing in your code making this a problem. All Injected arrays are automatically protected. While your Job is running no other System accessing Position or writing to EntityHolder components is allowed to run.
     
  3. nzhangaudio

    nzhangaudio

    Joined:
    May 26, 2015
    Posts:
    20
    The problem was I can't put
    [WriteOnly] ComponentDataFromEntity<Position>
    inside a job.
     
  4. nzhangaudio

    nzhangaudio

    Joined:
    May 26, 2015
    Posts:
    20
    Can we use
    ComponentDataFromEntity<Position>
    in IJobProcessComponentData?
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    yes. It depends on if it is a parallel for job or not. IJobProcessComponent can be run as parallel for or not. Depends on if you pass batchIterationCount or not.

    Same as was discussed above in regards to thread safety.
     
    nzhangaudio likes this.