Jobs not running in working threads
Recently I've been experimenting with Unity DOTS. I'm making a Job to find the nearest target using JobComponentSystem. This is the code as far:
public class FindTargetSystem : JobComponentSystem
{
[BurstCompile]
private struct EntityPosition
{
public Entity entity;
public float3 position;
public bool isIdle;
}
private JobHandle jobHandle;
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
EntityQuery entityQuery = GetEntityQuery(typeof(WeaponComponentData), ComponentType.ReadOnly<Translation>());
NativeArray<Entity> entities = entityQuery.ToEntityArray(Allocator.TempJob);
NativeArray<Translation> translations = entityQuery.ToComponentDataArray<Translation>(Allocator.TempJob);
NativeArray<EntityPosition> entityPositions = new NativeArray<EntityPosition>(entities.Length, Allocator.TempJob);
for (int i = 0; i < entityPositions.Length; ++i)
{
entityPositions[i] = new EntityPosition
{
entity = entities[i],
position = translations[i].Value,
isIdle = OrderManager.Instance.IsEntityIdle(entities[i])
};
}
jobHandle = Entities.ForEach((Entity entity, ref WeaponComponentData weapon, in Translation translation) =>
{
for (int i = 0; i < entityPositions.Length; ++i)
{
if (entityPositions[i].entity == entity && entityPositions[i].isIdle)
{
Entity targetEntity = Entity.Null;
float3 idleEntityPos = new float3(translation.Value.x, 0f, translation.Value.z);
float distance = float.PositiveInfinity;
float checkingDistance = 0f;
for (int j = 0; j < entityPositions.Length; ++j)
{
if (entity != entityPositions[j].entity)
{
float3 xzPos = new float3(entityPositions[j].position.x, 0f, entityPositions[j].position.z);
checkingDistance = math.distance(idleEntityPos, xzPos);
if (checkingDistance < distance)
{
distance = checkingDistance;
targetEntity = entityPositions[j].entity;
}
}
}
weapon.actualTargetEntity = targetEntity;
}
}
}).Schedule(inputDeps);
jobHandle.Complete();
entities.Dispose();
translations.Dispose();
entityPositions.Dispose();
return jobHandle;
}
}
The problem is that this code ALWAYS runs in the main thread, according to the profiler. If I understand good, calling Complete() on a JobHandler, returns the control immediately to the main thread, but if I don't call it, the console throws many errors... what I'm doing wrong ?
Unity Version: 2019.3.6f1
Greetings !
Your answer
Follow this Question
Related Questions
How to communicate between scripts and gameobject use tag in unity 1 Answer
How to reset score when game restart,How to reset score on game restart? 0 Answers
Coroutine Waits, But Calls Multiple Times per Update. Is There a Way to Fix This? 1 Answer
How can I separate my Code efficiently ? 0 Answers
Problem with IF statement C# 0 Answers