Search Unity

false alert of `EntityCommandBuffer` in multiple jobs of one system?

Discussion in 'Entity Component System' started by quabug, Mar 22, 2020.

  1. quabug

    quabug

    Joined:
    Jul 18, 2015
    Posts:
    66
    The code block below seems safe to me, or am I doing something wrong?
    It is throw exception on running.
    Code (CSharp):
    1.     public struct FooComponent : IComponentData {}
    2.     public struct BarComponent : IComponentData {}
    3.  
    4.     public class TestSystem : SystemBase
    5.     {
    6.         private EndSimulationEntityCommandBufferSystem _endSimulationEntityCommandBufferSystem;
    7.  
    8.         protected override void OnCreate()
    9.         {
    10.             _endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    11.             EntityManager.CreateEntity(typeof(FooComponent));
    12.             EntityManager.CreateEntity(typeof(BarComponent));
    13.             EntityManager.CreateEntity(typeof(FooComponent), typeof(BarComponent));
    14.         }
    15.         protected override void OnUpdate()
    16.         {
    17.             {
    18.                 var ecb = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
    19.                 Dependency = Entities.WithAll<FooComponent>().ForEach((Entity entity) =>
    20.                 {
    21.                     var e = ecb;
    22.                 }).ScheduleParallel(Dependency);
    23.             }
    24.  
    25.             {
    26.                 var ecb = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
    27.                 Dependency = Entities.WithAll<BarComponent>().ForEach((Entity entity) =>
    28.                 {
    29.                     var e = ecb;
    30.                 }).ScheduleParallel(Dependency);
    31.             }
    32.         }
    33.     }
    Code (CSharp):
    1. ArgumentException: The previously scheduled job TestSystem:<>c__DisplayClass_OnUpdate_LambdaJob0 writes to the UNKNOWN_OBJECT_TYPE <>c__DisplayClass_OnUpdate_LambdaJob0.JobData.ecb. You must call JobHandle.Complete() on the job TestSystem:<>c__DisplayClass_OnUpdate_LambdaJob0, before you can write to the UNKNOWN_OBJECT_TYPE safely.
    2.  
     
  2. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    328
    You need to add the dependency for the ECB.
    Code (CSharp):
    1. _ecb.AddJobHandleForProducer(dependencies);
     
    Vagabond_Games likes this.
  3. quabug

    quabug

    Joined:
    Jul 18, 2015
    Posts:
    66
    Thank you, you're right...
    I used to have this line of code, and then delete it for no reason?
    What a blockhead I am..