Search Unity

EntityCommandBuffer.RemoveComponent<T>() strange behaviour

Discussion in 'Entity Component System' started by DrSpritz, Jun 10, 2018.

  1. DrSpritz

    DrSpritz

    Joined:
    Oct 6, 2013
    Posts:
    29
    Hi guys!

    I have an issue with removing component from an entity when duplicate it in Hierarchy. It happens only when i do this through a job.

    Here is my IComponentData code
    Code (CSharp):
    1. using Unity.Entities;
    2. using System;
    3.  
    4. [Serializable]
    5. public struct InitialBug : IComponentData { }
    6.  
    7. public class InitialBugComponent : ComponentDataWrapper<InitialBug> { }
    And JobComponentSystem code
    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using UnityEngine.Jobs;
    5.  
    6. public class InitialBugBarrier : BarrierSystem { }
    7.  
    8. public class InitialBugSystem : JobComponentSystem
    9. {
    10.     public struct Data
    11.     {
    12.         [ReadOnly] public EntityArray entity;
    13.         [ReadOnly] public ComponentDataArray<InitialBug> initialBug;
    14.     }
    15.  
    16.     [Inject] Data m_data;
    17.     [Inject] InitialBugBarrier m_barrier;
    18.  
    19.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    20.     {
    21.         JobHandle removeInitialLocalRotationJob = new RemoveInitialLocalRotationJob()
    22.         {
    23.             commandBufer = m_barrier.CreateCommandBuffer(),
    24.             entity = m_data.entity
    25.         }.Schedule();
    26.  
    27.         return removeInitialLocalRotationJob;
    28.     }
    29.  
    30.     [ComputeJobOptimization]
    31.     struct RemoveInitialLocalRotationJob : IJob
    32.     {
    33.         [WriteOnly] public EntityCommandBuffer commandBufer;
    34.         [ReadOnly] public EntityArray entity;
    35.  
    36.         public void Execute()
    37.         {
    38.             for (int i = 0; i < entity.Length; i++)
    39.             {
    40.                 commandBufer.RemoveComponent<InitialBug>(entity[i]);
    41.             }
    42.         }
    43.     }
    44. }
    Then in editor, need to create an EmptyGameObject, add InitialBugComponent to it and hit Play. At this point all works perfectly and removing of component is done, but if you try to duplicate the EmptyGameObject you will get an error:



    What happens and what i'm doing wrong?
     
  2. DrSpritz

    DrSpritz

    Joined:
    Oct 6, 2013
    Posts:
    29
    up

    I've tried the new 6th release of ECS, the problem does not change. Anybody has tried to reproduce that?
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Try turning off [ComputeJobOptimization]
     
  4. DrSpritz

    DrSpritz

    Joined:
    Oct 6, 2013
    Posts:
    29
    Yes it works, thank you!
    But i'm not sure that i understand why it is. Do i should turning off [ComputeJobOptimization] in all jobs that have an EntityCommandBuffer field? It would be great if in such cases the error system notified about this, since it is done for [ReadOnly] and [WriteOnly] attributes.
     
  5. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    Yes at present EntityCommandBuffer doesn't work with burst.
    There is an EntityCommandBuffer.Concurrent that can work in multiple jobs, previously you had to create a new command buffer for each separate job. I dont know if it's burst compatible yet but I would hazard a guess no.
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    It's a known bug.
     
    Afonso-Lage likes this.
  7. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    Well, it does work with DestroyEntity just not RemoveComponent.
     
    recursive likes this.
  8. nzhangaudio

    nzhangaudio

    Joined:
    May 26, 2015
    Posts:
    20
    Does either EntityCommandBuffer or EntityCommandBuffer.Concurrent work with Burst now?
     
  9. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203