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. Dismiss Notice

Systems not recognized dependency on fixed array

Discussion in 'Entity Component System' started by dartriminis, Jun 25, 2018.

  1. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    For my use case, I am storing an Entity in a SharedComponentData. I then use that Entity to access a fixed array using FixedArrayFromEntity. I do this in two systems, one writing to the fixed array, and another reading from the fixed array. However, the systems do not appear to be recognizing the dependency on the fixed array. When I run my game, it gives me the generic "... must call complete before you can read from the NativeArray..." error.

    Is this intended behavior, a bug, or should I rethink my implementation?

    Code (CSharp):
    1. public struct ArrayElement
    2.     {
    3.         public int Value;
    4.     }
    5.  
    6.     public struct SharedComponent1 : ISharedComponentData
    7.     {
    8.         public Entity Value;
    9.     }
    10.  
    11.     public static class Startup
    12.     {
    13.         [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    14.         public static void OnSceneLoaded()
    15.         {
    16.             var entityManager = World.Active.GetOrCreateManager<EntityManager>();
    17.  
    18.             var e1 = entityManager.CreateEntity(ComponentType.FixedArray(typeof(ArrayElement), 16));
    19.  
    20.             var e2 = entityManager.CreateEntity(typeof(SharedComponent1));
    21.             entityManager.SetSharedComponentData(e2, new SharedComponent1 { Value = e1 });
    22.         }
    23.     }
    24.  
    25.     public class SystemA : JobComponentSystem
    26.     {
    27.         private struct Group
    28.         {
    29.             [ReadOnly] public SharedComponentDataArray<SharedComponent1> SharedComponentDataArray;
    30.         }
    31.  
    32.         [Inject] private Group                              _group;
    33.         [Inject] private FixedArrayFromEntity<ArrayElement> _fixedArrayFromEntity;
    34.  
    35.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    36.         {
    37.             return new WriteElementsJob { Items = _fixedArrayFromEntity[_group.SharedComponentDataArray[0].Value] }.Schedule(inputDeps);
    38.         }
    39.  
    40.         private struct WriteElementsJob : IJob
    41.         {
    42.             public NativeArray<ArrayElement> Items;
    43.  
    44.             public void Execute()
    45.             {
    46.                 for (var i = 0; i < 16; i++)
    47.                     Items[i] = new ArrayElement { Value = i };
    48.             }
    49.         }
    50.     }
    51.  
    52.     public class SystemB : JobComponentSystem
    53.     {
    54.         private struct Group
    55.         {
    56.             [ReadOnly] public SharedComponentDataArray<SharedComponent1> SharedComponentDataArray;
    57.         }
    58.  
    59.         [Inject] private Group                              _group;
    60.         [Inject] private FixedArrayFromEntity<ArrayElement> _fixedArrayFromEntity;
    61.  
    62.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    63.         {
    64.             return new ReadElementsJob { Items = _fixedArrayFromEntity[_group.SharedComponentDataArray[0].Value] }.Schedule();
    65.         }
    66.  
    67.         private struct ReadElementsJob : IJob
    68.         {
    69.             [ReadOnly] public NativeArray<ArrayElement> Items;
    70.  
    71.             public void Execute()
    72.             {
    73.                 for (var i = 0; i < 16; i++)
    74.                 {
    75.                     var item = Items[i];
    76.                 }
    77.             }
    78.         }
    79.     }
    Thanks,
     
  2. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    You forgot to pass in the input dependencies on the Read Job Schedule.

    Okay, after running your code that didn't fix it.'

    But you are basically trying to set Items before the previous job that works with Items is finished, at schedule time.
     
    Last edited: Jun 25, 2018
  3. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    Indeed I did. Unfortunately, adding it in does not solve the problem :(
     
  4. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    I was playing around with it for a bit, tried many different ways. Passing the whole FixedArrayFromEntity to the job and accessing the content inside the job, using the JobComponentSystem GetFixedArrayFromEntity etc.
    It seems like there is something going wrong with the dependencies, even though they should be correctly set.
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    I'm finding that automatic dependencies only works for IComponentData, and nothing else (FixedArrays, ISharedComponentData etc)

    I'm not sure if this is intended behaviour or not.