Hello,
I have a job that copies the positions of the tanks and another job that consumes them to check the distance, for this I use a NativeArray but it shows me the following warning and I don’t understand the reason.
Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
using Components.Tags;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
namespace Systems
{
[UpdateInGroup(typeof(SimulationSystemGroup))]
public class BulletVsTankCollisionSystem : JobComponentSystem
{
private EntityQuery _tankGroup;
private EntityQuery _bulletGroup;
private BeginInitializationEntityCommandBufferSystem _commandBufferSystem;
protected override void OnCreate()
{
_tankGroup = GetEntityQuery(new EntityQueryDesc
{
All = new[] {ComponentType.ReadOnly<Tank>(), ComponentType.ReadOnly<Translation>()},
});
_bulletGroup = GetEntityQuery(new EntityQueryDesc
{
All = new[] {ComponentType.ReadOnly<Bullet>(), ComponentType.ReadOnly<Translation>()},
});
_commandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
}
[BurstCompile]
private struct CopyPositions : IJobForEachWithEntity<Translation>
{
public NativeArray<float3> Positions;
public NativeArray<Entity> Entities;
public void Execute(Entity entity, int index, [ReadOnly] ref Translation localToWorld)
{
Positions[index] = localToWorld.Value;
Entities[index] = entity;
}
}
[BurstCompile]
[RequireComponentTag(typeof(Bullet))]
private struct CheckDistanceJob : IJobForEachWithEntity<Translation>
{
[DeallocateOnJobCompletion] [ReadOnly] public NativeArray<float3> CopyTankPositions;
[DeallocateOnJobCompletion] [ReadOnly] public NativeArray<Entity> CopyTankEntities;
[ReadOnly] public EntityCommandBuffer CommandBuffer;
public void Execute(
Entity entity,
int index,
[ReadOnly] ref Translation translation
)
{
// TODO: Check distance, add damage...
CommandBuffer.DestroyEntity(entity);
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var tankCount = _tankGroup.CalculateLength();
var copyTankPositions = new NativeArray<float3>(tankCount, Allocator.TempJob,
NativeArrayOptions.UninitializedMemory);
var copyTankEntities = new NativeArray<Entity>(tankCount, Allocator.TempJob,
NativeArrayOptions.UninitializedMemory);
var copyTankPositionsJob = new CopyPositions
{
Positions = copyTankPositions,
Entities = copyTankEntities
};
var copyTankPositionsJobHandle = copyTankPositionsJob.Schedule(_tankGroup, inputDeps);
var checkDistanceJob = new CheckDistanceJob
{
CopyTankPositions = copyTankPositions,
CopyTankEntities = copyTankEntities,
CommandBuffer = _commandBufferSystem.CreateCommandBuffer()
};
var checkDistanceJobHandle = checkDistanceJob.Schedule(_bulletGroup, copyTankPositionsJobHandle);
_commandBufferSystem.AddJobHandleForProducer(checkDistanceJobHandle);
return checkDistanceJobHandle;
}
}
}
How can I solve that? Is there a better way to do it?
Thanks in advance.