Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

IJobProcessComponentData with more than 3 type parameters?

Discussion in 'Entity Component System' started by hellowill89, Jun 13, 2018.

  1. hellowill89

    hellowill89

    Joined:
    Jan 8, 2013
    Posts:
    45
    I noticed IJobProcessComponentData only goes up to 3 parameters. What if I want to get 4 components in the job? I saw that IJobProcessComponentData is more performant that IJobParallelFor, so I don't want to switch away from it.
     
  2. zulfajuniadi

    zulfajuniadi

    Joined:
    Nov 18, 2017
    Posts:
    117
    If either one of the four components are just "tag" components you can add the attribute:

    [RequireComponentTag(typeof(SomeTag), typeof(SomeTag2), typeof(SomeTag3))]
     
    recursive, hellowill89 and DrSpritz like this.
  3. hellowill89

    hellowill89

    Joined:
    Jan 8, 2013
    Posts:
    45
    I'm a little unclear by what you mean as "tag" components. How are they different than normal components?
     
  4. zulfajuniadi

    zulfajuniadi

    Joined:
    Nov 18, 2017
    Posts:
    117
    At this moment they're exactly the same as normal component data just that you don't need to access the value in this particular job.
     
  5. hellowill89

    hellowill89

    Joined:
    Jan 8, 2013
    Posts:
    45
    Why reference them if you don't need to access them? So that jobs are executed in proper order?
     
  6. zulfajuniadi

    zulfajuniadi

    Joined:
    Nov 18, 2017
    Posts:
    117
    As the name suggests, it is to differentiate between two different groups of entities. As an example let's say you are building a game witches vs wizards and you have a WatarDamageSystem that only applies damage to the witches units, so you create a ComponentData tag Witches and use it as a tag so that the system only runs with entities that has component.
     
  7. hellowill89

    hellowill89

    Joined:
    Jan 8, 2013
    Posts:
    45
    That makes sense. Thanks!
     
  8. hellowill89

    hellowill89

    Joined:
    Jan 8, 2013
    Posts:
    45
    In my case, I do have 4 components which I want to reference, so the tag isn't the answer.
     
  9. Afonso-Lage

    Afonso-Lage

    Joined:
    Jul 8, 2012
    Posts:
    70
    So just use injection and create your IJob or IJobParallelFor

    Code (CSharp):
    1. public class MySystem : JobComponentData
    2. {
    3.     struct MyData
    4.     {
    5.         public int Length;
    6.         public ComponentDataArray<MyComponent1> Components1;
    7.         public ComponentDataArray<MyComponent2> Components2;
    8.         public ComponentDataArray<MyComponent3> Components3;
    9.         public ComponentDataArray<MyComponent4> Components4;
    10.     }
    11.     [Inject] private MyData m_Data;
    12.    
    13.     struct MyJob : IJobPrallelFor
    14.     {
    15.         //Remember to mark as read only or write only depending on what you gonna do.
    16.         [ReadOnly] public ComponentDataArray<MyComponent1> Components1;
    17.         [ReadOnly] public ComponentDataArray<MyComponent2> Components2;
    18.         [ReadOnly] public ComponentDataArray<MyComponent3> Components3;
    19.         [WriteOnly] public ComponentDataArray<MyComponent4> Components4;
    20.         public override void Execute(int index)
    21.         {
    22.             var c1 = Components1[index];
    23.             var c2 = Components2[index];
    24.             var c3 = Components3[index];
    25.             Components4[index] = DoSomething(c1, c2, c3);
    26.         }
    27.     }
    28.    
    29.     public override JobHandle Update(JobHandle inputDeps)
    30.     {
    31.         return new MyJob()
    32.         {
    33.             Components1 = m_Data.Components1,
    34.             Components2 = m_Data.Components2,
    35.             Components3 = m_Data.Components3,
    36.             Components4 = m_Data.Components4,
    37.         }.Schedule(m_Data.Length, 1, inputDeps);
    38.     }
    39. }
    Please note this is a just pseudo code, didn't tested it, just wrote it here on forum.
     
    Dan4x8 and LazyMonkey like this.