Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved InvalidOperationException: The TranslationTypeHandle has not been assigned or constructed. All cont

Discussion in 'Entity Component System' started by KAV2008, Aug 13, 2020.

  1. KAV2008

    KAV2008

    Joined:
    Aug 13, 2020
    Posts:
    11
    I am trying to calculate and assign targets for entities in the game, but I couldn't fix this error:
    Code (CSharp):
    1. InvalidOperationException: The UNKNOWN_OBJECT_TYPE FindTargetBurstJob.JobData.TranslationTypeHandle has not been assigned or constructed. All containers must be valid when scheduling a job.
    2. Unity.Entities.JobChunkExtensions.ScheduleInternal[T] (T& jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn, Unity.Jobs.LowLevel.Unsafe.ScheduleMode mode, System.Boolean isParallel)
    Here is the code:
    Code (CSharp):
    1. public class FindTargetJobSystem : SystemBase {
    2.  
    3.     private struct EntityWithPosition
    4.     {
    5.         public Entity entity;
    6.         public float3 position;
    7.     }
    8.  
    9.     private EntityQuery unitQuery;
    10.     private EntityQuery targetQuery;
    11.  
    12.  
    13.     private EndSimulationEntityCommandBufferSystem endSimulationEntityCommandBufferSystem;
    14.  
    15.     protected override void OnCreate()
    16.     {
    17.         base.OnCreate();
    18.         endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    19.     }
    20.     protected override void OnUpdate()
    21.     {
    22.         targetQuery = GetEntityQuery(typeof(Enemy), ComponentType.ReadOnly<Translation>());
    23.  
    24.         NativeArray<Entity> targetEntityArray = targetQuery.ToEntityArray(Allocator.TempJob);
    25.         NativeArray<Translation> targetTranslationArray = targetQuery.ToComponentDataArray<Translation>(Allocator.TempJob);
    26.  
    27.         NativeArray<EntityWithPosition> targetArray = new NativeArray<EntityWithPosition>(targetEntityArray.Length, Allocator.TempJob);
    28.  
    29.         for (int i = 0; i < targetEntityArray.Length; i++) {
    30.             targetArray[i] = new EntityWithPosition {
    31.                 entity = targetEntityArray[i],
    32.                 position = targetTranslationArray[i].Value,
    33.             };
    34.         }
    35.  
    36.         targetEntityArray.Dispose();
    37.         targetTranslationArray.Dispose();
    38.  
    39.         unitQuery = GetEntityQuery(ComponentType.ReadOnly<Friend>(), ComponentType.ReadOnly<Translation>(), ComponentType.Exclude<HasTarget>());
    40.         NativeArray<Entity> closestTargetEntityArray = new NativeArray<Entity>(unitQuery.CalculateEntityCount(), Allocator.TempJob);
    41.        
    42.         FindTargetBurstJob findTargetBurstJob = new FindTargetBurstJob()
    43.         {
    44.             targetArray = targetArray,
    45.             closestTargetEntityArray = closestTargetEntityArray
    46.         };
    47.         this.Dependency = findTargetBurstJob.ScheduleParallel(unitQuery, this.Dependency);
    48.  
    49.  
    50.         var entityCommandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().AsParallelWriter();
    51.  
    52.         Entities
    53.             .WithStoreEntityQueryInField(ref unitQuery)
    54.             .ForEach((Entity entity,int entityInQueryIndex) =>
    55.             {
    56.                 if (closestTargetEntityArray[entityInQueryIndex] != Entity.Null)
    57.                 {
    58.                     entityCommandBuffer.AddComponent(entityInQueryIndex, entity, new HasTarget { targetEntity = closestTargetEntityArray[entityInQueryIndex] });
    59.                 }
    60.             }).ScheduleParallel();
    61.  
    62.         this.Dependency = findTargetBurstJob.ScheduleParallel(unitQuery, this.Dependency);  
    63.  
    64.         endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(this.Dependency);
    65.  
    66.     }
    67.  
    68.  
    69.     [BurstCompile]
    70.     private struct FindTargetBurstJob : IJobChunk
    71.     {
    72.         [DeallocateOnJobCompletion] [ReadOnly] public NativeArray<EntityWithPosition> targetArray;
    73.         public NativeArray<Entity> closestTargetEntityArray;
    74.  
    75.         [ReadOnly] public ComponentTypeHandle<Translation> TranslationTypeHandle;
    76.  
    77.         public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
    78.         {
    79.             var translations = chunk.GetNativeArray(TranslationTypeHandle);
    80.  
    81.             for (int i = 0; i < translations.Length; i++)
    82.             {
    83.                 float3 unitPosition = translations[i].Value;
    84.                 Entity closestTargetEntity = Entity.Null;
    85.                 float3 closestTargetPosition = float3.zero;
    86.  
    87.                 for (int j = 0; j < targetArray.Length; j++)
    88.                 {
    89.                     EntityWithPosition targetEntityWithPosition = targetArray[j];
    90.  
    91.                     if (closestTargetEntity == Entity.Null)
    92.                     {
    93.                         closestTargetEntity = targetEntityWithPosition.entity;
    94.                         closestTargetPosition = targetEntityWithPosition.position;
    95.                     }
    96.                     else
    97.                     {
    98.                         if (math.distance(unitPosition, targetEntityWithPosition.position) < math.distance(unitPosition, closestTargetPosition))
    99.                         {
    100.                             closestTargetEntity = targetEntityWithPosition.entity;
    101.                             closestTargetPosition = targetEntityWithPosition.position;
    102.                         }
    103.                     }
    104.                 }
    105.                 closestTargetEntityArray[i] = closestTargetEntity;
    106.             }
    107.  
    108.         }
    109.     }
    110.  
    111. }
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,674
    You did not add TranslationTypeHandle field with initialized ComponentTypeHandle<Translation> on job initialization (42-46 rows)
     
  3. KAV2008

    KAV2008

    Joined:
    Aug 13, 2020
    Posts:
    11
    It definitely is the reason, thank you so much!