Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Destroying a parent entity does not destroy some children (LinkedEntityGroup)

Discussion in 'Data Oriented Technology Stack' started by PhilSA, Oct 10, 2019 at 11:38 PM.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,084
    I have entities 1, 2, and 3. Entity3 is child of Entity2, and Entity2 is child of Entity1

    • Entity1
      • Has no parent
      • Has Entity2 in LinkedEntityGroup buffer
    • Entity2
      • Has Entity1 as parent
      • Has Entity3 in LinkedEntityGroup buffer
    • Entity3
      • Has Entity2 as parent

    When I destroy Entity1 with entityCommandBuffer.DestroyEntity() from a job, Entity1 and Entity2 disappear, but Entity3 still exists. I'm guessing this is not really a bug but more of a problem with my understanding of what LinkedEntitiesGroup does.

    I guess an alternative question would be: what are the rules for child entities to be destroyed along with their parents?
     
    Last edited: Oct 11, 2019 at 1:35 AM
    Opeth001 and Kichang-Kim like this.
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,496
    root LinkedEntityGroup should have all child entites for destroy them. If you look at conversion workflow it puts all child entities to root LinkedEntityGroup and put entity itself.
    Root -> Child 1 -> Child 2 Child 3

    Root LinkedEntityGroup - Root Child 1 Child 2 Child 3
    Child 1 LinkedEntityGroup - Child 1 Child 2 Child 3

    It's what I remember, if it's not changed from time when I checked it :)
    Edit: Yeah if you look at source code it's not changed:
    Code (CSharp):
    1. f (entityCount == 0 || entityPtr[0] != srcEntity)
    2.                     throw new ArgumentException("LinkedEntityGroup[0] must always be the Entity itself.");
    Code (CSharp):
    1. /// <summary>
    2.     /// The LinkedEntityGroup buffer makes the entity be the root of a set of connected entities.
    3.     /// </summary>
    4.     /// <remarks>
    5.     /// Referenced Prefabs automatically add a LinkedEntityGroup with the complete child hierarchy. //<------
    Code (CSharp):
    1. void AddToDestroyList(Chunk* chunk, int indexInChunk, int batchCount, int inputDestroyCount,
    2.             ref UnsafeList entitiesList, ref int minBufferLength, ref int maxBufferLength)
    3.         {
    4.             int indexInArchetype = ChunkDataUtility.GetIndexInTypeArray(chunk->Archetype, m_LinkedGroupType);
    5.             if (indexInArchetype != -1)
    6.             {
    7.                 var baseHeader = ChunkDataUtility.GetComponentDataWithTypeRO(chunk, indexInChunk, m_LinkedGroupType);
    8.                 var stride = chunk->Archetype->SizeOfs[indexInArchetype];
    9.                 for (int i = 0; i != batchCount; i++)
    10.                 {
    11.                     var header = (BufferHeader*) (baseHeader + stride * i);
    12.  
    13.                     var entityGroupCount = header->Length - 1;
    14.                     if (entityGroupCount == 0)
    15.                         continue;
    16.  
    17.                     var entityGroupArray = (Entity*) BufferHeader.GetElementPointer(header) + 1;
    18.  
    19.                     if (entitiesList.Capacity == 0)
    20.                         entitiesList.SetCapacity<Entity>(inputDestroyCount * entityGroupCount /*, Allocator.TempJob*/);
    21.                     entitiesList.AddRange<Entity>(entityGroupArray, entityGroupCount /*, Allocator.TempJob*/);
    22.  
    23.                     minBufferLength = math.min(minBufferLength, entityGroupCount);
    24.                     maxBufferLength = math.max(maxBufferLength, entityGroupCount);
    25.                 }
    26.             }
    27.         }
     
    Last edited: Oct 11, 2019 at 3:07 PM
    YurySedyakin and PhilSA like this.