Hi,
I have a JobComponent system. The point is to find Interactables around any InteractableActors, and find the best one the Actor can interact with by calculating a score.
This is the JobComponentSystem:
CODE
public class InteractableSystems : JobComponentSystem
{
private EntityQuery actorsQuery;
private EntityQuery playerQuery;
private EntityQuery interactablesQuery;
protected override void OnCreate()
{
this.actorsQuery = this.GetEntityQuery(ComponentType.ReadOnly<InteractableActor>(), ComponentType.ReadOnly<Translation>());
this.interactablesQuery = this.GetEntityQuery(ComponentType.ReadOnly<Interactable>(), ComponentType.ReadOnly<Translation>(),
ComponentType.Exclude<InteractableDisabled>());
this.RequireForUpdate(this.actorsQuery);
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var handle = new ClearBuffersJob().Schedule(this, inputDeps);
handle = new FindInteractbleAroundDistanceJob
{
Distance = 2,
InteractablesEntities = this.interactablesQuery.ToEntityArray(Allocator.TempJob),
InteractablesPositions = this.interactablesQuery.ToComponentDataArray<Translation>(Allocator.TempJob)
}.Schedule(this, handle);
handle = new FindInteractableInRangeJob
{
MinDistance = 1f
}.Schedule(this, handle);
return handle;
}
[BurstCompile]
private struct ClearBuffersJob : IJobForEachWithEntity_EB<InteractableAroundBuffer>
{
public void Execute(Entity entity, int index, DynamicBuffer<InteractableAroundBuffer> b0)
{
b0.Clear();
}
}
[BurstCompile]
[RequireComponentTag(typeof(InteractableActor))]
private struct FindInteractbleAroundDistanceJob : IJobForEachWithEntity_EBC<InteractableAroundBuffer, Translation>
{
public int Distance;
[ReadOnly] [DeallocateOnJobCompletion] public NativeArray<Entity> InteractablesEntities;
[ReadOnly] [DeallocateOnJobCompletion] public NativeArray<Translation> InteractablesPositions;
public void Execute(Entity entity, int index, DynamicBuffer<InteractableAroundBuffer> buffer, ref Translation translation)
{
for (var i = 0; i < this.InteractablesEntities.Length; i++)
{
if (math.distancesq(translation.Value, this.InteractablesPositions[i].Value) < this.Distance * this.Distance)
{
buffer.Add(new InteractableAroundBuffer
{
InteractableEntity = this.InteractablesEntities[i],
Position = this.InteractablesPositions[i].Value
});
}
}
}
}
[BurstCompile]
private struct FindInteractableInRangeJob : IJobForEachWithEntity_EBCC<InteractableAroundBuffer, Translation, Rotation>
{
public float MinDistance;
public void Execute(Entity entity, int index, DynamicBuffer<InteractableAroundBuffer> b, ref Translation t, ref Rotation r)
{
var closestIndex = -1;
var maxScore = float.NegativeInfinity;
for (var i = 0; i < b.Length; i++)
{
var interactable = b[i];
var dir = interactable.Position - t.Value;
var dist = math.lengthsq(dir);
var dot = math.dot(math.forward(r.Value), math.normalize(dir));
var score = 1f / dist * math.remap(-1, 1, 1, 3, dot);
if (score > maxScore && dist <= this.MinDistance)
{
maxScore = score;
closestIndex = i;
}
}
if (closestIndex != -1)
{
var e = b[closestIndex];
e.IsInRange = true;
b[closestIndex] = e;
}
}
}
}
I’m not sure what’s happening…
Maybe the code is really not optimized or how exactly I should interpret the Profiler and Entity Debugger
because the jobs take in average 0.16 ms (In Entity Debugger / Profiler)
For reference, I only have 1 Actor and 6 Interactables for now.
This is the Entity Debugger view:
The Profiler:
And the Timeline has:
ClearBuffersJob (Burst): 0.003 ms (Worker 3)
ClearBuffersJob (CleanUp): 0.001 ms (Worker 3)
FindInteractbleAroundDistanceJob (Burst): 0.003 ms (Worker 5)
FindInteractableInRangeJob (Burst): 0.002 ms (Worker 5)
Which makes a total of 0.008 ms…
Not 0.16 ms…
So my questions are:
- Should I trust the time in the EntityDebugger / Profiler ?
- Is it because I don’t have much yet so everything has to wait and in fact I’m only taking 0.008 ms to do the job… (clue from the WaitForJobGroupID)
- My code is just garbage.
- Why it is generating GC ??
Thanks !