Search Unity

Bug error DC0013 while reading data inside lambda

Discussion in 'Entity Component System' started by sunrisebacon, Jun 6, 2020.

  1. sunrisebacon

    sunrisebacon

    Joined:
    Jul 15, 2017
    Posts:
    16
    Unity 2020.10b11 / Entities 0.11.0-preview.7

    It looks like I stumbled across a bug with Lambdas inside parallel jobs.
    I'm trying to read a buffer element from a lambda inside a job.
    It's only a read but I get this error message inside Unity (maybe I don't understand it correctly?!?)
    error DC0013: Entities.ForEach Lambda expression writes to captured variable


    Code (CSharp):
    1.  
    2. using System;
    3. using Unity.Entities;
    4.  
    5. namespace Buggy
    6. {
    7.     public struct TestData : IBufferElementData
    8.     {
    9.         public int Value;
    10.     }
    11.  
    12.     public class BuggySystem : SystemBase
    13.     {
    14.         protected override void OnUpdate()
    15.         {
    16.             BufferFromEntity<TestData> entityBuffer = GetBufferFromEntity<TestData>(true);
    17.             DynamicBuffer<TestData> buffer = entityBuffer[Entity.Null];
    18.             Entities
    19.                 .WithReadOnly(buffer)
    20.                 .ForEach((int entityInQueryIndex, int nativeThreadIndex, Entity entity) =>
    21.             {
    22.                 // error DC0013: Entities.ForEach Lambda expression writes to captured variable '<>9__1'. This is only supported when you use .Run().
    23.                 Func<int, int> func = (index) => buffer[index].Value;
    24.             }).ScheduleParallel();
    25.         }
    26.     }
    27. }
    28.  

    Side note: When I debug Entities code with Rider, Unity freezes and becomes unresponsive like every second time.
    It's quite painful... :/

    Edit: I realized this is true for NativeArrays as well so I changed the title/description.
     
    Last edited: Jun 6, 2020
  2. joepl

    joepl

    Unity Technologies

    Joined:
    Jul 6, 2017
    Posts:
    87
    The error is confusing, but this is not currently allowed in a bursted/scheduled Entities.ForEach. Looking at the IL, it appears that it is storing the created delegate back into an Action on the DisplayClass. This should be allowed with burst off and .Run but not with schedule jobs or burst on.

    I'll see if we can give a better error in this case though!