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

IBufferElementData aliasing

Discussion in 'Entity Component System' started by TLRMatthew, Nov 29, 2019.

  1. TLRMatthew

    TLRMatthew

    Joined:
    Apr 10, 2019
    Posts:
    65
    I have two IBufferElementData of the same type on two different Entities. When I try to pass them to the same Job, Unity complains that they're the same NativeArray.

    Code (CSharp):
    1. var undoBuffer = EntityManager.GetBuffer<TreesBuffer>(_currentUndoEntity).Reinterpret<Entity>();
    2.  
    3. var changedTreesEvent = GetSingletonEntity<ChangedTreesEvent>();
    4. var eventBuffer = EntityManager.GetBuffer<TreesBuffer>(changedTreesEvent).Reinterpret<Entity>();
    5.  
    6. result = new ApplyChangesJob
    7. {
    8.     AllCheckedTrees = _treesToCheck,
    9.     TreesToDemolish = _treesToDemolish,
    10.     TreeIndicesToMarkChecked = _treeIndicesToMarkChecked,
    11.     Mountain = mountain,
    12.     EventEntity = changedTreesEvent,
    13.     EventBuffer = eventBuffer,
    14.     UndoBuffer = undoBuffer,
    15.     CommandBuffer = _ecbSystem.CreateCommandBuffer(),
    16. }.Schedule(result);
    InvalidOperationException: The writable NativeArray ApplyChangesJob.EventBuffer is the same NativeArray as ApplyChangesJob.UndoBuffer, two NativeArrays may not be the same (aliasing).


    I've triple checked that _currentUndoEntity and changedTreesEvent are different Entities, and they both have a TreeBuffer on them. I've tried both having the buffer be part of the archetype when creating the Entities, as well as using AddBuffer after creating them. I've also tried not Reinterpreting one of the buffers in case that was causing the problem.

    Is this a bug, or am I misunderstanding something about how buffers work under the hood? I've got other TreesBuffers elsewhere that work fine so this seems to be a problem specifically with passing two to the same job.
     
  2. TLRMatthew

    TLRMatthew

    Joined:
    Apr 10, 2019
    Posts:
    65
    I seem to be able to get around the issue for now by splitting it into two separate jobs, but it'd be great to know why this is happening!

    Code (CSharp):
    1. result = new ApplyChangesJob
    2. {
    3.     AllCheckedTrees = _treesToCheck,
    4.     TreesToDemolish = _treesToDemolish,
    5.     TreeIndicesToMarkChecked = _treeIndicesToMarkChecked,
    6.     Mountain = mountain,
    7.     EventEntity = changedTreesEvent,
    8.     EventBuffer = eventBuffer,
    9.     CommandBuffer = _ecbSystem.CreateCommandBuffer(),
    10. }.Schedule(result);
    11.  
    12. result = new SetupUndoJob
    13. {
    14.     TreesToDemolish = _treesToDemolish,
    15.     UndoBuffer = undoBuffer
    16. }.Schedule(result);
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    Aliasing logic happens by type for ECS types. Use BufferFromEntity instead and pass in the two entities into the job.
     
    TLRMatthew likes this.