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 the 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?
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
// This system updates all entities in the scene with both a RotationSpeed_IJobForEach and Rotation component.
//[UpdateAfter(typeof(Collision))]
// ReSharper disable once InconsistentNaming
public class ForEachGrav : JobComponentSystem
{
struct ForEachGravJob : IJobForEach<ForceData, MyMassData, Translation>
{
[DeallocateOnJobCompletion] [ReadOnly] public NativeArray<MyMassData> masses;
[DeallocateOnJobCompletion] [ReadOnly] public NativeArray<Translation> translations;
//[DeallocateOnJobCompletion] [ReadOnly] public NativeArray<Velocity> velocities;
//public float DeltaTime;
//public EntityCommandBuffer.Concurrent buffer;
// The [ReadOnly] attribute tells the job scheduler that this job will not write to rotSpeedIJobForEach
public void Execute( ref ForceData f, [ReadOnly] ref MyMassData mass, [ReadOnly] ref Translation t)
{
//Entity
// Rotate something about its up vector at the speed given by RotationSpeed_IJobForEach.
float maxVel = 0.5f;
float3 newForce = new float3(0, 0, 0);
var mass1 = mass.mass;
float3 posi1 = t.Value;
var force1 = f.Value;
for (int j = 0; j < masses.Length; j++)
{
var mass2 = masses[j];
var posi2 = translations[j];
//float3 vel2 = velocities[j].velocity;
// Rotate something about its up vector at the speed given by RotationSpeed_IJobChunk.
//f berechnen
//pos
float3 p1 = posi1;
float3 p2 = posi2.Value;
float d = mass2.mass * mass1 * 6.67f;
//float d = 1;
p1 = p2 - p1;
//float3 p1Norm = math.normalize(p1);
float magnitude = (math.length(p1));
//d = (d / magnitude) * GravitationsKonstante;
//d = (d) * GravitationsKonstante;
//Debug.Log(d);
if (magnitude != 0 && magnitude > 1f)
{
p1 = (p1 / math.pow(magnitude, 3)) * d; //force
newForce += p1;
}
f.Value = newForce;
}
}
}
private EndSimulationEntityCommandBufferSystem endsim;
protected override void OnCreate()
{
//endsim = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
base.OnCreate();
}
// OnUpdate runs on the main thread.
protected override JobHandle OnUpdate(JobHandle inputDependencies)
{
EntityQuery q = GetEntityQuery(ComponentType.ReadOnly<MyMassData>(), ComponentType.ReadOnly<Translation>());
NativeArray<MyMassData> masses2 = q.ToComponentDataArray<MyMassData>(Allocator.TempJob);
NativeArray<Translation> poss2 = q.ToComponentDataArray<Translation>(Allocator.TempJob);
//NativeArray<Translation> velocitiesThis = q.ToComponentDataArray<Velocity>(Allocator.TempJob);
var job = new ForEachGravJob
{
masses = masses2,
translations = poss2,
//buffer = endsim.CreateCommandBuffer().ToConcurrent(),
//DeltaTime = Time.deltaTime
};
var handle = job.Schedule(this, inputDependencies);
//endsim.AddJobHandleForProducer(handle);
return handle;
}
}