Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How to Jobfy this ComponentSystem-based code?

Discussion in 'Entity Component System and C# Job system' started by nzhangaudio, Aug 13, 2018.

  1. nzhangaudio

    nzhangaudio

    Joined:
    May 26, 2015
    Posts:
    7
    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:
    189
    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:
    7
    The problem was I can't put
    [WriteOnly] ComponentDataFromEntity<Position>
    inside a job.
     
  4. nzhangaudio

    nzhangaudio

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

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,149
    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.