Search Unity

Unexpected error while processing function Unity.Entities.TypeManager.GetTypeIndex

Discussion in 'Data Oriented Technology Stack' started by mnarimani, Aug 13, 2019.

  1. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    184
    I have a shooting system that searches for players that are shooting (shooting component's value is true).
    This is the code:

    Code (CSharp):
    1.  using Unity.Burst;
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5. using Unity.Mathematics;
    6. using Unity.Transforms;
    7. using UnityEngine;
    8.  
    9. namespace Diamond
    10. {
    11.     public class ShootingSystem : JobComponentSystem
    12.     {
    13.         private EntityCommandBufferSystem commandBufferSystem;
    14.         private EntityQuery shootingPlayersQuery;
    15.         private EntityQuery bulletPrefabQuery;
    16.  
    17.         protected override void OnCreate()
    18.         {
    19.             commandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    20.             bulletPrefabQuery = GetEntityQuery(ComponentType.ReadOnly<BulletPrefab>());
    21.         }
    22.  
    23.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    24.         {
    25.             shootingPlayersQuery = GetEntityQuery(
    26.                 ComponentType.ReadOnly<PlayerTag>(),
    27.                 ComponentType.ReadOnly<Rotation>(),
    28.                 ComponentType.ReadOnly<IsShooting>());
    29.             shootingPlayersQuery.SetFilter(new IsShooting(true));
    30.  
    31.             NativeArray<Entity> shootingPlayers = shootingPlayersQuery.ToEntityArray(Allocator.TempJob);
    32.  
    33.             if (shootingPlayers.Length == 0)
    34.             {
    35.                 shootingPlayers.Dispose();
    36.                 return inputDeps;
    37.             }
    38.  
    39.             var job = new ShootingJob
    40.             {
    41.                 BulletPrefab = bulletPrefabQuery.GetSingleton<BulletPrefab>().Value,
    42.                 Players = shootingPlayers,
    43.                 Positions = GetComponentDataFromEntity<Translation>(),
    44.                 Rotations = GetComponentDataFromEntity<Rotation>(),
    45.                 CommandBuffer = commandBufferSystem.CreateCommandBuffer().ToConcurrent()
    46.             };
    47.  
    48.             JobHandle jobHandle = job.Schedule(shootingPlayers.Length, 32, inputDeps);
    49.  
    50.             commandBufferSystem.AddJobHandleForProducer(jobHandle);
    51.  
    52.             return jobHandle;
    53.         }
    54.  
    55.         [BurstCompile]
    56.         private struct ShootingJob : IJobParallelFor
    57.         {
    58.             public EntityCommandBuffer.Concurrent CommandBuffer;
    59.             [ReadOnly] public Entity BulletPrefab;
    60.  
    61.             [DeallocateOnJobCompletion, ReadOnly] public NativeArray<Entity> Players;
    62.             [ReadOnly] public ComponentDataFromEntity<Translation> Positions;
    63.             [ReadOnly] public ComponentDataFromEntity<Rotation> Rotations;
    64.  
    65.             public void Execute(int index)
    66.             {
    67.                 Entity player = Players[index];
    68.                 if (!Positions.HasComponent(player))
    69.                     Debug.Log("Something is really wrong");
    70.                 if (!Rotations.HasComponent(player))
    71.                     Debug.Log("Something is really wrong");
    72.  
    73.                 Entity bullet = CommandBuffer.Instantiate(index, BulletPrefab);
    74.  
    75.                 CommandBuffer.SetComponent(index, bullet, Positions[player]);
    76.                 Rotation rotation = Rotations[player];
    77.                 CommandBuffer.SetComponent(index, bullet, new Rotation { Value = rotation.Value });
    78.  
    79.                 float3 movementDirection = math.rotate(rotation.Value, new float3(0, 0, 1));
    80.                 CommandBuffer.AddComponent(index, bullet, new MovementDirection(movementDirection));
    81.             }
    82.         }
    83.     }
    84. }
    85.  
    However, I'm getting an error which I can't understand:

    C:\Dev\Unity\DiamondProject\DiamondClient\Library\PackageCache\com.unity.entities@0.1.1-preview\Unity.Entities\Types\TypeManager.cs(919,13): error: Unexpected error while processing function `Unity.Entities.TypeManager.GetTypeIndex<Unity.Transforms.Rotation>()`. Reason: Value cannot be null.
    Parameter name: method
    at Unity.Entities.TypeManager.GetTypeIndex() (at C:\Dev\Unity\DiamondProject\DiamondClient\Library\PackageCache\com.unity.entities@0.1.1-preview\Unity.Entities\Types\TypeManager.cs:912)
    at Unity.Entities.ComponentType.ReadWrite() (at C:\Dev\Unity\DiamondProject\DiamondClient\Library\PackageCache\com.unity.entities@0.1.1-preview\Unity.Entities\Types\ComponentType.cs:43)
    at Unity.Entities.EntityCommandBufferData.AddEntityComponentCommand(Unity.Entities.EntityCommandBufferData* this, Unity.Entities.EntityCommandBufferChain* chain, int jobIndex, Unity.Entities.ECBCommand op, Unity.Entities.Entity e, Unity.Transforms.Rotation component) (at C:\Dev\Unity\DiamondProject\DiamondClient\Library\PackageCache\com.unity.entities@0.1.1-preview\Unity.Entities\EntityCommandBuffer.cs:427)
    at Unity.Entities.EntityCommandBuffer.Concurrent.SetComponent(Unity.Entities.EntityCommandBuffer.Concurrent* this, int jobIndex, Unity.Entities.Entity e, Unity.Transforms.Rotation component) (at C:\Dev\Unity\DiamondProject\DiamondClient\Library\PackageCache\com.unity.entities@0.1.1-preview\Unity.Entities\EntityCommandBuffer.cs:1659)
    at Diamond.ShootingSystem.ShootingJob.Execute(Diamond.ShootingSystem.ShootingJob* this, int index) (at C:\Dev\Unity\DiamondProject\DiamondClient\Assets\Scripts\Systems\ShootingSystem.cs:77)
    at Unity.Jobs.IJobParallelForExtensions.ParallelForJobStruct`1<Diamond.ShootingSystem.ShootingJob>.Execute(ref Diamond.ShootingSystem.ShootingJob jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex)
    Internal compiler exception: System.ArgumentNullException: Value cannot be null.
    Parameter name: method
    at Mono.Cecil.Mixin.CheckMethod (System.Object method) [0x00012] in <28cdca1704d2491781795499c297b78b>:0
    at Mono.Cecil.MethodSpecification..ctor (Mono.Cecil.MethodReference method) [0x00006] in <28cdca1704d2491781795499c297b78b>:0
    at Mono.Cecil.GenericInstanceMethod..ctor (Mono.Cecil.MethodReference method) [0x00000] in <28cdca1704d2491781795499c297b78b>:0
    at Burst.Compiler.IL.Intrinsics.TypeManagerProcessor.TryGetMethodBody (Burst.Compiler.IL.Syntax.ILBuilder builder, Burst.Compiler.IL.Syntax.ILFunctionReference method, Mono.Cecil.Cil.MethodBody& methodBody) [0x0044c] in <3179d4839c86430ca331f2949f40ede5>:0
    at Burst.Compiler.IL.Syntax.ILBuilder.GetMethodBody (Burst.Compiler.IL.Syntax.ILFunctionReference binding) [0x00020] in <3179d4839c86430ca331f2949f40ede5>:0
    at Burst.Compiler.IL.Syntax.ILBuilder.CreateFunctionFromRef (Burst.Compiler.IL.Syntax.ILFunctionReference funcRef) [0x000b4] in <3179d4839c86430ca331f2949f40ede5>:0
    at Burst.Compiler.IL.Syntax.ILBuilder.VisitPendingFunctionReferences () [0x000c1] in <3179d4839c86430ca331f2949f40ede5>:0
    While compiling job: System.Void Unity.Jobs.IJobParallelForExtensions/ParallelForJobStruct`1<Diamond.ShootingSystem/ShootingJob>::Execute(T&,System.IntPtr,System.IntPtr,Unity.Jobs.LowLevel.Unsafe.JobRanges&,System.Int32)

    Why it's giving me this error?
     
    Last edited: Aug 13, 2019
  2. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    184
    I'm getting this error on ecs samples too.
    Can someone confirm if this is a bug or not?
     
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,370
    You can't use Set/AddComponent on ECB with burst. Only methods without using type registry allowed. It will be allowed in future.
     
  4. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    184
    If that's the case I would appreciate if Unity devs added a more civil error message explaining issues like this.
    Also, They should remove the burst compile from their samples too:
    https://github.com/Unity-Technologi...pawnAndRemove/SpawnerSystem_SpawnAndRemove.cs

    This is the code:
    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5. using Unity.Mathematics;
    6. using Unity.Transforms;
    7. using Random = Unity.Mathematics.Random;
    8.  
    9. // JobComponentSystems can run on worker threads.
    10. // However, creating and removing Entities can only be done on the main thread to prevent race conditions.
    11. // The system uses an EntityCommandBuffer to defer tasks that can't be done inside the Job.
    12.  
    13. // ReSharper disable once InconsistentNaming
    14. [UpdateInGroup(typeof(SimulationSystemGroup))]
    15. public class SpawnerSystem_SpawnAndRemove : JobComponentSystem
    16. {
    17.     // BeginInitializationEntityCommandBufferSystem is used to create a command buffer which will then be played back
    18.     // when that barrier system executes.
    19.     //
    20.     // Though the instantiation command is recorded in the SpawnJob, it's not actually processed (or "played back")
    21.     // until the corresponding EntityCommandBufferSystem is updated. To ensure that the transform system has a chance
    22.     // to run on the newly-spawned entities before they're rendered for the first time, the SpawnerSystem_FromEntity
    23.     // will use the BeginSimulationEntityCommandBufferSystem to play back its commands. This introduces a one-frame lag
    24.     // between recording the commands and instantiating the entities, but in practice this is usually not noticeable.
    25.     //
    26.     BeginInitializationEntityCommandBufferSystem m_EntityCommandBufferSystem;
    27.  
    28.     protected override void OnCreate()
    29.     {
    30.         // Cache the BeginInitializationEntityCommandBufferSystem in a field, so we don't have to create it every frame
    31.         m_EntityCommandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    32.     }
    33.  
    34.     struct SpawnJob : IJobForEachWithEntity<Spawner_SpawnAndRemove, LocalToWorld>
    35.     {
    36.         public EntityCommandBuffer.Concurrent CommandBuffer;
    37.  
    38.         [BurstCompile]
    39.         public void Execute(Entity entity, int index, [ReadOnly] ref Spawner_SpawnAndRemove spawner, [ReadOnly] ref LocalToWorld location)
    40.         {
    41.             var random = new Random(1);
    42.  
    43.             for (var x = 0; x < spawner.CountX; x++)
    44.             {
    45.                 for (var y = 0; y < spawner.CountY; y++)
    46.                 {
    47.                     var instance = CommandBuffer.Instantiate(index, spawner.Prefab);
    48.  
    49.                     // Place the instantiated in a grid with some noise
    50.                     var position = math.transform(location.Value, new float3(x * 1.3F, noise.cnoise(new float2(x, y) * 0.21F) * 2, y * 1.3F));
    51.                     CommandBuffer.SetComponent(index, instance, new Translation { Value = position });
    52.                     CommandBuffer.SetComponent(index, instance, new LifeTime { Value = random.NextFloat(10.0F, 100.0F) });
    53.                     CommandBuffer.SetComponent(index, instance, new RotationSpeed_SpawnAndRemove { RadiansPerSecond = math.radians(random.NextFloat(25.0F, 90.0F)) });
    54.                 }
    55.             }
    56.  
    57.             CommandBuffer.DestroyEntity(index, entity);
    58.         }
    59.     }
    60.  
    61.     protected override JobHandle OnUpdate(JobHandle inputDependencies)
    62.     {
    63.         // Instead of performing structural changes directly, a Job can add a command to an EntityCommandBuffer to
    64.         // perform such changes on the main thread after the Job has finished. Command buffers allow you to perform
    65.         // any, potentially costly, calculations on a worker thread, while queuing up the actual insertions and
    66.         // deletions for later.
    67.  
    68.         // Schedule the job that will add Instantiate commands to the EntityCommandBuffer.
    69.         var job = new SpawnJob
    70.         {
    71.             CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent()
    72.         }.Schedule(this, inputDependencies);
    73.  
    74.         // SpawnJob runs in parallel with no sync point until the barrier system executes.
    75.         // When the barrier system executes we want to complete the SpawnJob and then play back the commands
    76.         // (Creating the entities and placing them). We need to tell the barrier system which job it needs to
    77.         // complete before it can play back the commands.
    78.         m_EntityCommandBufferSystem.AddJobHandleForProducer(job);
    79.  
    80.         return job;
    81.     }
    82. }
    BurstCompile attribute on Execute method in SpawnJob should be removed to avoid confusing people.
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,370
    In 2019.3 alpha cycle they added support for this.
     
  6. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    184
    Are you sure? I have Unity 2019.3.0a11 and this is happening. Maybe I have to wait for the next alpha.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,370
    Ah sorry, it's still not supported yet ) But Unity says it'll be in 2019.3 cycle.