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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

(Solved) Simple job using EntityCommandBuffer breaks when 730 entities are processed

Discussion in 'Entity Component System' started by Marius_L, Apr 8, 2019.

  1. Marius_L

    Marius_L

    Joined:
    Jan 16, 2016
    Posts:
    7
    I am getting different errors after 730 entities with archetype exist in the world. 729 Entities works.
    Here are some of the errors, whichever one shows up is seemingly random.

    ArgumentException: All entities passed to EntityManager must exist. One of the entities has already been destroyed or was never created.

    Assertion failure. Value was False
    Expected: True

    Assertion failure. Values are not equal.
    Expected: 1 == 2

    ArgumentException: Object reference not set to an instance of an object

    ArgumentException: Invalid command buffer

    Code (CSharp):
    1. public class SelectEntitySystem : JobComponentSystem
    2. {
    3.     struct RemoveSelectionsJob : IJobForEachWithEntity<Selected>
    4.     {
    5.         [ReadOnly] public EntityCommandBuffer buffer;
    6.         public void Execute(Entity entity, int index, [ReadOnly] ref Selected selected)
    7.         {
    8.             buffer.RemoveComponent<Selected>(entity);
    9.         }
    10.     }
    11.  
    12.     private EntityCommandBufferSystem bufferSystem;
    13.  
    14.     protected override void OnCreateManager()
    15.     {
    16.         bufferSystem = World.Active.GetExistingSystem<EntityCommandBufferSystem>();
    17.     }
    18.  
    19.  
    20.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    21.     {
    22.         JobHandle combinedInputDeps = inputDeps;
    23.         if (Input.GetMouseButtonDown(1))
    24.         {
    25.             // Remove all previous selections
    26.             EntityCommandBuffer unselectBuffer = bufferSystem.CreateCommandBuffer();
    27.             JobHandle unselect = new RemoveSelectionsJob() { buffer = unselectBuffer }.Schedule(this, inputDeps);
    28.             bufferSystem.AddJobHandleForProducer(unselect);
    29.             combinedInputDeps = JobHandle.CombineDependencies(inputDeps, unselect);
    30.         }
    31.  
    32.         return combinedInputDeps;
    33.     }
    34. }
     
  2. Piefayth

    Piefayth

    Joined:
    Feb 7, 2017
    Posts:
    61
    Dunno if this is what is causing it, but you probably want to use a EntityCommandBuffer.Concurrent when doing an IJobForEach.
     
    Antypodish and Marius_L like this.
  3. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    I'm pretty sure RemoveSelectionsJob.buffer should not be ReadOnly, and should also be of type EntityCommandBuffer.Concurrent.
     
    Marius_L likes this.
  4. ilih

    ilih

    Joined:
    Aug 6, 2013
    Posts:
    1,320
    You need to use concurrent command buffer:
    [ReadOnly] public EntityCommandBuffer.Concurrent buffer;

    EntityCommandBuffer unselectBuffer = bufferSystem.CreateCommandBuffer().ToConcurrent();


    Jobs are running for each chunk at the same time, not concurrent command buffer worked fine when you have one chunk, but when you have more than one chunk you get the error.
    In your case, one chunk can contain 729 entities, and with 730 entities you have two chunks.
     
    Marius_L likes this.
  5. Marius_L

    Marius_L

    Joined:
    Jan 16, 2016
    Posts:
    7
    Thank you, guys. I did not realize the buffer needed to be Concurrent in IJobForEach. On another note, the code as-is worked by scheduling the job with ScheduleSingle.
     
  6. ilih

    ilih

    Joined:
    Aug 6, 2013
    Posts:
    1,320
    ScheduleSingle is run the job in a single thread, so you don't have a problem with parallel access from the other threads.