Search Unity

Stale data in ToEntityArray(Allocator.TempJob)

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Aug 24, 2019.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Wondering if someone could help me out. When I run my game I create entities and pass then to my system, all is well.

    Now when I edit my live game object the ToEntityArray size reflect the old length suggesting
    eTriangleffer = eqTriangleComp.ToEntityArray(Allocator.TempJob) did not update.



    Code (CSharp):
    1.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    2.         {
    3.             eqVertexComp = em.CreateEntityQuery(ComponentType.ReadOnly<VertexComponent>());
    4.             eqTriangleComp = em.CreateEntityQuery(ComponentType.ReadOnly<TriangleComponent>());
    5.             QBuildData = GetEntityQuery(ComponentType.ReadOnly<HexSphereBuildDataComponent>());
    6.  
    7.             //
    8.             // Get data
    9.             var hexSphereBuildComponent = GetSingletonEntity<HexSphereBuildDataComponent>();
    10.             hexSphereBuildDataSingleton = EntityManager.GetComponentData<HexSphereBuildDataComponent>(hexSphereBuildComponent);
    11.  
    12.             //
    13.             // Schedule CopyVertexToTileVertex
    14.             inputDeps = new CopyVertexToTileVertex()
    15.             {
    16.                 eTriangleffer = eqTriangleComp.ToEntityArray(Allocator.TempJob)
    17.             }.Schedule(this, inputDeps);
    18.             ecbs.AddJobHandleForProducer(inputDeps);
    19.  
    20.             //
    21.             // Schedule CopyTrianglesToTileTriangles
    22.             inputDeps = new CopyTrianglesToTileTriangles()
    23.             {
    24.                 eVertexBeffer = eqVertexComp.ToEntityArray(Allocator.TempJob),
    25.                 eTriangleffer = eqTriangleComp.ToEntityArray(Allocator.TempJob),
    26.                 dTileVertex = GetComponentDataFromEntity<TileVertexComponent>(true)
    27.             }.Schedule(this, inputDeps);
    28.             ecbs.AddJobHandleForProducer(inputDeps);
    29.  
    30.             //
    31.             // Schedule TagTiles
    32.             inputDeps = new TagTiles()
    33.             {
    34.                 ecb = ecbs.CreateCommandBuffer().ToConcurrent(),
    35.  
    36.             }.Schedule(this, inputDeps);
    37.             ecbs.AddJobHandleForProducer(inputDeps);
    38.  
    39.             //
    40.             // Complete
    41.             inputDeps.Complete();
    42.  
    43.             /* Handled in HexSphereBuildBarrierSystem
    44.             //
    45.             // Remove hex vertex and triangle components so the system doen not run jobs
    46.             // TODO : Looking into versions on entities
    47.             EntityManager.RemoveComponent(eqVertexComp, typeof(VertexComponent));
    48.             EntityManager.RemoveComponent(eqTriangleComp, typeof(TriangleComponent));
    49.             */
    50.  
    51.             //TEST
    52.             eqVertexComp.Dispose();
    53.             eqTriangleComp.Dispose();
    54.             //QBuildData.Dispose();
    55.  
    56.             return inputDeps;
    57.         }
    58.  
    59.         #endregion
    60.     }

    here is the job

    Code (CSharp):
    1.         /// <summary>
    2.         /// Copying TileTriangleComponent data to TriangleComponent and calculating CenterPosition.
    3.         /// </summary>
    4.         //[BurstCompile]
    5.         struct CopyTrianglesToTileTriangles : IJobForEach<TriangleComponent, TileTriangleComponent>
    6.         {
    7.             [DeallocateOnJobCompletion]
    8.             [ReadOnly] public NativeArray<Entity> eVertexBeffer;
    9.             [DeallocateOnJobCompletion]
    10.             [ReadOnly] public NativeArray<Entity> eTriangleffer;
    11.             [ReadOnly] public ComponentDataFromEntity<TileVertexComponent> dTileVertex;
    12.  
    13.             public void Execute([ReadOnly] ref TriangleComponent t, ref TileTriangleComponent tt)
    14.             {
    15.                 Debug.Log(eVertexBeffer.Length);
    16.  
    17.                 tt.VertA = eVertexBeffer[t.VertA];
    18.                 tt.VertB = eVertexBeffer[t.VertB];
    19.                 tt.VertC = eVertexBeffer[t.VertC];
    20.                 tt.NeighborsAccrossAB = eTriangleffer[t.NeighborsAccrossAB];
    21.                 tt.NeighborsAccrossBC = eTriangleffer[t.NeighborsAccrossBC];
    22.                 tt.NeighborsAccrossCA = eTriangleffer[t.NeighborsAccrossCA];
    23.  
    24.                 float3 pA = dTileVertex[tt.VertA].Position;
    25.                 float3 pB = dTileVertex[tt.VertB].Position;
    26.                 float3 pC = dTileVertex[tt.VertC].Position;
    27.  
    28.                 tt.CenterPosition = new float3((pA.x + pB.x + pC.x) / 3, (pA.y + pB.y + pC.y) / 3, (pA.z + pB.z + pC.z) / 3);
    29.  
    30.             }
    31.         }
    Error is IndexOutOfRangeException: Index 30 is out of range of '20' Length. for

    tt.VertB = eVertexBeffer[t.VertB];

    I dont understand why

    eqTriangleComp = em.CreateEntityQuery(ComponentType.ReadOnly<TriangleComponent>());
    eVertexBeffer.Length
    has not updated to new number of entities.