Search Unity

Old IJobParallelFor Script doesn't work on Unity 2019.3.4

Discussion in 'Entity Component System' started by GamerLordMat, Mar 11, 2020.

  1. GamerLordMat

    GamerLordMat

    Joined:
    Oct 10, 2019
    Posts:
    185
    Hello guys,
    I have ported my old gravitation script to a new project.
    I just want to pass two readonly arrays to a job. This worked in Unity 2019.2, but ported to Unity 2019.03.4 I get this error: Assets\Systems\ForEachGrav.cs(14,12): error CS0453: The type 'ForceData' must be a non-nullable value type in order to use it as parameter 'T0' in the generic type or method 'IJobForEach_CCC'
    What does that mean?
    I know UNity changed the Syntax [Readonly] to in, but changing this also didn't worked out.

    Code (CSharp):
    1.       using Unity.Burst;
    2.          using Unity.Collections;
    3.          using Unity.Entities;
    4.          using Unity.Jobs;
    5.     using Unity.Mathematics;
    6.     using Unity.Transforms;
    7.     using UnityEngine;
    8.     public class ForEachGrav : JobComponentSystem
    9.     {
    10.          struct ForEachGravJob : IJobForEach<ForceData, MyMassData, Translation>
    11.          {
    12.              [DeallocateOnJobCompletion] [ReadOnly] public NativeArray<MyMassData> masses;
    13.              [DeallocateOnJobCompletion] [ReadOnly] public NativeArray<Translation> translations;
    14.              public void Execute( ref ForceData f, [ReadOnly] ref MyMassData mass, [ReadOnly] ref Translation t)
    15.              {
    16.                  float maxVel = 0.5f;
    17.                  float3 newForce = new float3(0, 0, 0);
    18.                  var mass1 = mass.mass;
    19.                  float3 posi1 = t.Value;
    20.                  var force1 = f.Value;
    21.                  for (int j = 0; j < masses.Length; j++)
    22.                  {
    23.                      var mass2 = masses[j];
    24.                      var posi2 = translations[j];
    25.                      float3 p1 = posi1;
    26.                      float3 p2 = posi2.Value;
    27.                      float d = mass2.mass * mass1 * 6.67f;
    28.                      p1 = p2 - p1;
    29.                      float magnitude = (math.length(p1));
    30.                      if (magnitude != 0 && magnitude > 1f)
    31.                      {
    32.                          p1 = (p1 / math.pow(magnitude, 3)) * d; //force
    33.                          newForce += p1;
    34.                      }
    35.                      f.Value = newForce;
    36.                  }
    37.              }
    38.          }
    39.          private EndSimulationEntityCommandBufferSystem endsim;
    40.          protected override JobHandle OnUpdate(JobHandle inputDependencies)
    41.          {
    42.              EntityQuery q = GetEntityQuery(ComponentType.ReadOnly<MyMassData>(), ComponentType.ReadOnly<Translation>());
    43.              NativeArray<MyMassData> masses2 = q.ToComponentDataArray<MyMassData>(Allocator.TempJob);
    44.              NativeArray<Translation> poss2 = q.ToComponentDataArray<Translation>(Allocator.TempJob);
    45.              (Allocator.TempJob);
    46.              var job = new ForEachGravJob
    47.              {
    48.                  masses = masses2,
    49.                  translations = poss2,
    50.              };
    51.              var handle = job.Schedule(this, inputDependencies);
    52.              return handle;
    53.          }
    54.     }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Welcome,

    Is your
    ForceData : IComponentData?
    Does it have properties (none empty) inside?
    You still should be able pass it into a job, without [ReadOnly] attribute.
     
  3. GamerLordMat

    GamerLordMat

    Joined:
    Oct 10, 2019
    Posts:
    185
    Thanks for the quick respons. Yes it is. It holds just a float3.
    I tried to just delete the readonly field, still same error :/
     
  4. Curlyone

    Curlyone

    Joined:
    Mar 15, 2018
    Posts:
    41
    your IComponentData should be a struct, not class.
     
    ExtraCat likes this.
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    @Curlyone may have valid point.
    @GamerLordMat can you show us part of the code with definition of ForceData, MyMassData components?
     
  6. GamerLordMat

    GamerLordMat

    Joined:
    Oct 10, 2019
    Posts:
    185
    I really forgot to make it a struct o_O, changing it to a struct worked out. I thank you all a lot.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    It's struct, not class ICD version?
    Edit: Ah already answered, not refreshed the page :)
     
  8. GamerLordMat

    GamerLordMat

    Joined:
    Oct 10, 2019
    Posts:
    185
    Thank you everyone for your help, it is a stupid error but I didnt know how to interpret the error code.
    Btw, how to close this question know?
     
  9. psuong

    psuong

    Joined:
    Jun 11, 2014
    Posts:
    126
    I think you can just edit the title to mark is as resolved.