Search Unity

Filling Component Data

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Jun 13, 2019.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    I keep running into this issue. I want to sparsely set data on my components with a job.


    eg. This is what im trying to do

    // Set vertexComponentEntities[vertexA].connectedTriA = entity ??
    // Set vertexComponentEntities[vertexB].connectedTriB = entity ??
    // Set vertexComponentEntities[vertexC].connectedTriC = entity ??
    Code (CSharp):
    1.  
    2. [BurstCompile]
    3.         struct SetHexTriangles : IJobForEachWithEntity<TriangleComponent>
    4.         {
    5.  
    6.             [NativeDisableParallelForRestrictionAttribute]
    7.             public ComponentDataFromEntity<VertexTrianglesComponent> vertexComponentEntities;
    8.             [ReadOnly] public EntityCommandBuffer.Concurrent ecb;
    9.  
    10.             public void Execute(Entity entity, int index, ref TriangleComponent triangleCompnent)
    11.             {
    12.                 //
    13.                 // Get entities in this triangle
    14.                 Entity vertexA = triangleCompnent.vertA;
    15.                 Entity vertexB = triangleCompnent.vertB;
    16.                 Entity vertexC = triangleCompnent.vertC;
    17.  
    18.                 if (vertexComponentEntities[vertexA].connectedTriA == Entity.Null)
    19.                 {
    20.                     // Set vertexComponentEntities[vertexA].connectedTriA = entity   ??
    21.                 }
    22.  
    23.                 if (vertexComponentEntities[vertexB].connectedTriB == Entity.Null)
    24.                 {
    25.                     // Set vertexComponentEntities[vertexB].connectedTriB = entity   ??
    26.                 }
    27.  
    28.                 if (vertexComponentEntities[vertexC].connectedTriC == Entity.Null)
    29.                 {
    30.                     // Set vertexComponentEntities[vertexC].connectedTriC = entity   ??
    31.                 }
    32.             }
    33.         }

    But the only way I know how to use EntityCommandBuffer would instantiate the whole VertexTrianglesComponent witch obviously does not work. How would I handle cases like this ? would I have to split them all out into their own components ? , this doesnt seem ideal to me.

    This shows the problem, hopefully

    Code (CSharp):
    1.         //[BurstCompile]
    2.         struct SetHexTriangles : IJobForEachWithEntity<TriangleComponent>
    3.         {
    4.  
    5.             [NativeDisableParallelForRestrictionAttribute]
    6.             public ComponentDataFromEntity<VertexTrianglesComponent> vertexComponentEntities;
    7.             [ReadOnly] public EntityCommandBuffer.Concurrent ecb;
    8.  
    9.             public void Execute(Entity entity, int index, ref TriangleComponent triangleCompnent)
    10.             {
    11.                 //
    12.                 // Get entities in this triangle
    13.                 Entity vertexA = triangleCompnent.vertA;
    14.                 Entity vertexB = triangleCompnent.vertB;
    15.                 Entity vertexC = triangleCompnent.vertC;
    16.  
    17.                 if (vertexComponentEntities[vertexA].connectedTriA == Entity.Null)
    18.                 {
    19.                     ecb.SetComponent<VertexTrianglesComponent>(index, vertexComponentEntities[vertexA].connectedTriC, new VertexTrianglesComponent
    20.                     {
    21.                         connectedTriA = entity,
    22.                         connectedTriB = Entity.Null,
    23.                         connectedTriC = Entity.Null
    24.                     });
    25.                 }
    26.  
    27.                 if (vertexComponentEntities[vertexB].connectedTriB == Entity.Null)
    28.                 {
    29.                     ecb.SetComponent<VertexTrianglesComponent>(index, vertexComponentEntities[vertexB].connectedTriC, new VertexTrianglesComponent
    30.                     {
    31.                         connectedTriA = Entity.Null,
    32.                         connectedTriB = entity,
    33.                         connectedTriC = Entity.Null
    34.                     });
    35.                 }
    36.  
    37.                 if (vertexComponentEntities[vertexC].connectedTriC == Entity.Null)
    38.                 {
    39.                     ecb.SetComponent<VertexTrianglesComponent>(index, vertexComponentEntities[vertexC].connectedTriC, new VertexTrianglesComponent
    40.                     {
    41.                         connectedTriA = Entity.Null,
    42.                         connectedTriB = Entity.Null,
    43.                         connectedTriC = entity
    44.                     });
    45.                 }
    46.             }
    47.         }
     
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Are you thinking not assigning an Entity is taking up less memory or something? Trying to understand what you think sparse is and what benefit it's giving you.
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    No the issues is during subdivision I dont not know all three connectedTriA connectedTriB and connectedTriC per Vertex Entity. What I do know is witch vertices make a triangle, but each vertex can have up to 5 connected triangles. In the non ECS code I can simply fill the data as I go

    Screen Shot 2019-06-13 at 12.45.18 AM.png

    Screen Shot 2019-06-13 at 12.47.46 AM.png
     
    Last edited: Jun 13, 2019
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Ok still a bit confused but why store it all as entities? Why not just two native arrays for verts and tris?
     
  5. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    The name is really misleading , essentially think of a live planet with spacial relations, constant moving tectonic plates, erosion etc.

    These mechanics seem ideal for ESC
     
    Last edited: Jun 13, 2019
  6. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Like @snacktime said it doesn't make a lot of sense but even if you want to set any component data this way you don't need any ECB:
    Code (CSharp):
    1.         if (vertexComponentEntities[vertexA].connectedTriA == Entity.Null) {
    2.           var vertexTriangleComponent = vertexComponentEntities[vertexA];
    3.           vertexTriangleComponent.connectedTriA = entity;
    4.           vertexComponentEntities[vertexA] = vertexTriangleComponent;
    5.         }
     
    RoughSpaghetti3211 likes this.
  7. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    I had this but it never updated my entities, Ill go diff to see the difference . Thanks for the help guys
     
  8. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    My suspicion there would be you were doing something like this:
    Code (csharp):
    1.  
    2. vertexComponentEntities[vertexA].connectedTriA = entity
    3.  
    Which is the equivalent of this:
    Code (csharp):
    1.  
    2. var vertexTriangleComponent = vertexComponentEntities[vertexA]
    3. vertexTriangleComponent.connectedTriA = entity;
    4.  
     
  9. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    It wouldn't compile. In ComponentDataFromEntity the [] accessor is a property. When a property returns a struct, the compiler don't let you set a field directly. It's the same with transform.position.x = 0

    []'s
     
  10. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    It was a typo .. Thanks everyone for the help