Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Why I can't add component in this situation

Discussion in 'Data Oriented Technology Stack' started by Micz84, Oct 26, 2018.

  1. Micz84


    Jul 21, 2012
    I create simple particle system just to practice ECS pattern, and I have strange error. I have a Job that checks if particle life has ended in first implementation I was just destroying entities, but then I have decided to reuse them instead to get better performance but I get error:

    ArgumentException: All entities passed to EntityManager must exist. One of the entities has already been destroyed or was never created.
    EntityCommandBuffer was recorded in Assets.KillParticleSystem and played back in Unity.Entities.EndFrameBarrier.

    Here is my system with a job.
    Code (CSharp):
    1. public class KillParticleSystem : JobComponentSystem
    2.     {
    3.         [RequireSubtractiveComponent(typeof(DeadParticle))]
    4.         public struct KillParticlesJob : IJobProcessComponentDataWithEntity< Particle>
    5.         {
    6.             public EntityCommandBuffer.Concurrent CommandBuffer;
    7.             public float _DeltaTime;
    9.             public void Execute(Entity entity, int index,  ref Particle particle)
    10.             {
    11.        -= _DeltaTime;
    12.                 if ( < 0) {
    13.                     // CommandBuffer.DestroyEntity(index, entity); // this works
    14.                     CommandBuffer.AddComponent(index, new DeadParticle()); // this causes error mentioned above
    15.                 }
    17.             }
    18.         }
    19.         [Inject] EndFrameBarrier barrier;
    20.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    21.         {
    22.             return new KillParticlesJob() { _DeltaTime = UnityEngine.Time.deltaTime, CommandBuffer = barrier.CreateCommandBuffer().ToConcurrent() }.Schedule(this, inputDeps);
    23.         }
    I do not destroy any entity in my code.
  2. eizenhorn


    Oct 17, 2016
    Because you not pass "entity" to ECB
    CommandBuffer.AddComponent(index, entity, new DeadParticle());

    In your case it try to get entity created before by CommandBuffer.CreateEntity(); but can't find it, this expected, because you not create it before :)
    rigidbuddy and TZ- like this.
  3. Micz84


    Jul 21, 2012
    I was looking at the API and have missed that. :oops:
    Thanks :)
    TZ- likes this.
  4. Antypodish


    Apr 29, 2014
    Can someone confirm, if this approach actually work with Burst?
    I think I read somewhere, that IJobProcessComponentDataWithEntity and Burst is not compatible.
    By far, I am failing to use it with burst, do to error.

    Any alternative suggestions?

    Good way, is to always look into overloads, when hovering mouse over. This is how I discovered new option. ;)
  5. eizenhorn


    Oct 17, 2016
    It's not true.
    And again my Indicaros System :) Burst work with all job types if all requirements are met
    Code (CSharp):
    1. [BurstCompile]
    2.     private struct
    3.         ProcessIndicatorsMoving : IJobProcessComponentDataWithEntity<IndicatorComponent, WorldMeshRenderBounds>
    4.     {
    5.         [NativeDisableParallelForRestriction] public ComponentDataFromEntity<Position>        posArray;
    6.         [NativeDisableParallelForRestriction] public ComponentDataFromEntity<HasIndicator>    entities;
    7.         [ReadOnly]                            public BufferFromEntity<IndicatorBufferElement> buffer;
    8.         [ReadOnly]                            public int                                      clusterIndex;
    9.         [ReadOnly]                            public float                                    clusterCellSize;
    10.         public EntityCommandBuffer.Concurrent ecb;
    12.         public void Execute(Entity indicatorEntity, int index, [ReadOnly] ref IndicatorComponent res,
    13.             ref WorldMeshRenderBounds bounds)
    14.         {
    15. ...
  6. Antypodish


    Apr 29, 2014
    Then I do something definitely wrong somewhere.

    I have same
    Code (CSharp):
    1. public EntityCommandBuffer.Concurrent ecb;
    Code (CSharp):
    1. ProcessIndicatorsMoving : IJobProcessComponentDataWithEntity
    And at initialization seams executes correctly, but after few seconds of run time, it throws error

    Unexpected exception Burst.Compiler.IL.CompilerException: Unexpected exception ---> Burst.Compiler.IL.CompilerException: Error while processing function `System.Int32 Unity.Entities.TypeManager::GetTypeIndex<T>()` ---> Burst.Compiler.IL.CompilerException: Unexpected error while processing `IL_0001: ldsfld System.Int32 Unity.Entities.TypeManager/StaticTypeLookup`1<T>::typeIndex` at C:\Users\...\Documents\Unity3D\ECS\OOPECS_Tweening\Library\PackageCache\com.unity.entities@0.0.12-preview.21\Unity.Entities\Types\TypeManager.cs(110,13) ---> System.NotSupportedException: Loading from a static field `System.Int32 Unity.Entities.TypeManager/StaticTypeLookup`1<T>::typeIndex` is not supported by burst
    at Burst.Compiler.IL.ILVisitor.NotSupported (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x0016b] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVisitor.Ldsfld (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x0006c] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVisitor.CompileInternal (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x004de] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVerifier.CompileInternal (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x00018] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVisitor.Compile (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x0001e] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVisitor.ProcessBlock (Burst.Compiler.IL.Syntax.ILBlock block) [0x0009a] in <37bebafd236f4ccd943dc039a926a017>:0
    --- End of inner exception stack trace ---
    at Burst.Compiler.IL.ILVisitor.ProcessBlock (Burst.Compiler.IL.Syntax.ILBlock block) [0x000e9] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVisitor.ProcessBlocks () [0x0002e] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVisitor.ProcessFunctionBody (Burst.Compiler.IL.Syntax.ILFunction function) [0x00103] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVisitor.VisitPendingFunctions () [0x0000e] in <37bebafd236f4ccd943dc039a926a017>:0
    --- End of inner exception stack trace ---
    at Burst.Compiler.IL.ILVisitor.VisitPendingFunctions () [0x00033] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVisitor.VisitEntryPointFunction (Burst.Compiler.IL.MethodReferenceWithHash methodReference) [0x00066] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVisitor.VisitEntryPointFunction (Burst.Backend.Module module, Burst.Compiler.IL.MethodReferenceWithHash methodReference) [0x0001a] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILVerifier.VisitEntryPointFunction (Burst.Backend.Module module, Burst.Compiler.IL.MethodReferenceWithHash methodReference) [0x00000] in <37bebafd236f4ccd943dc039a926a017>:0
    --- End of inner exception stack trace ---
    at Burst.Compiler.IL.ILVerifier.VisitEntryPointFunction (Burst.Backend.Module module, Burst.Compiler.IL.MethodReferenceWithHash methodReference) [0x0001f] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.ILHash.CompileHash (Burst.Backend.Module module, Burst.Compiler.IL.MethodReferenceWithHash methodReference) [0x00000] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.NativeCompiler.ComputeHash () [0x000ea] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.Jit.JitCompiler.CompileMethod (Mono.Cecil.MethodReference methodReference, Burst.Compiler.IL.Jit.JitOptions jitOptions) [0x000aa] in <37bebafd236f4ccd943dc039a926a017>:0
    at Burst.Compiler.IL.Jit.JitCompilerService.Compile (Burst.Compiler.IL.Jit.JitCompilerService+CompileJob job) [0x002b2] in <37bebafd236f4ccd943dc039a926a017>:0

    While compiling job: System.Void Unity.Entities.JobProcessComponentDataExtensions/JobStruct_Process1_WE`2<ECS.JSEvaluatorSystem/RunJob,ECS.IsInstanceActiveTag>::Execute(Unity.Entities.JobProcessComponentDataExtensions/JobStruct_Process1_WE`2<T,U0>&,System.IntPtr,System.IntPtr,Unity.Jobs.LowLevel.Unsafe.JobRanges&,System.Int32)

    May I ask, how do you initialize ecb?

    I don't know if I got right steps / approach, but I use before class declaration
    Code (CSharp):
    1. public class JSEvaluatorBarrier : BarrierSystem {}
    Then I have
    Code (CSharp):
    1. [UpdateAfter ( typeof ( UnityEngine.Experimental.PlayerLoop.FixedUpdate ) ) ]
    2. class JSEvaluatorSystem : JobComponentSystem
    3. {
    4.         [Inject] private JSEvaluatorBarrier barrier ;
    5. ...
    And then in OnUpdate

    Code (CSharp):
    1. protected override JobHandle OnUpdate (JobHandle inputDeps)
    2.         {
    4.             var runJob = new RunJob ()
    5.             {
    6.                 ecb = barrier.CreateCommandBuffer ().ToConcurrent (),
    7. ...
    Could it be, that my Inject causes issue?
    In fact can I remove inject now somehow, with barrier?
    Last edited: Dec 21, 2018
  7. eizenhorn


    Oct 17, 2016
    In bursted job you only can call DestroyEntity from ECB, all other ECB features not allowed in burst.
    ecb in my case like this:
     [Inject] private   EndFrameBarrier _barrier;

    passed to job.
    Antypodish likes this.
  8. Antypodish


    Apr 29, 2014
    Oh yes, that what I read in past. That explains, as I try add tag component.
    Oh well, I need go with different route.