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

Why is the LinkedEntityGroup component added when instanting entities?

Discussion in 'Entity Component System' started by soundeos, Apr 9, 2022.

  1. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    I'm generating entities with the EntityManager.Instantiate method using an entity prefab. When I inspect the archetype I see the LinkedEntityGroup component (see the attachment please). It's the largest in size. Why is it there? What is the usage of this?
     

    Attached Files:

    • arc.png
      arc.png
      File size:
      28.1 KB
      Views:
      228
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    LinkedEntityGroup is used both during instantiation to create all linked entities, but also when you destroy an entity so it can destroy all it's linked entities as well.
     
  3. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    But there is no linked entity. It's just this one. And the value of the LinkedEntityGroup item is the entity itself. I'm sending the image of the component value. Check it out, please.
     

    Attached Files:

    • arc2.png
      arc2.png
      File size:
      73.5 KB
      Views:
      204
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695

    “The LinkedEntityGroup buffer makes the entity be the root of a set of connected entities. Referenced Prefabs automatically add a LinkedEntityGroup with the complete child hierarchy. EntityManager.Instantiate uses LinkedEntityGroup to instantiate the whole set of entities automatically. EntityManager.SetEnabled uses LinkedEntityGroup to enable the whole set of entities”
    https://docs.unity3d.com/Packages/com.unity.entities@0.0/api/Unity.Entities.LinkedEntityGroup.html
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    Yeah I see what you're saying. It does seem a bit unneeded to had the buffer if you have no children or linked entities.

    having a look at source, you couldn't even run a post conversion cleanup system in GameObjectAfterConversionGroup because adding LinkedEntityGroups occurs last

    Code (CSharp):
    1.                    conversion.MappingSystem.CreatePrimaryEntities();
    2.  
    3.                     conversionWorld.GetExistingSystem<GameObjectBeforeConversionGroup>().Update();
    4.                     conversionWorld.GetExistingSystem<GameObjectConversionGroup>().Update();
    5.                     conversionWorld.GetExistingSystem<GameObjectAfterConversionGroup>().Update();
    6.                 }
    7.  
    8.                 using (s_AddPrefabComponentDataTag.Auto())
    9.                     conversion.MappingSystem.AddPrefabComponentDataTag();
    10.  
    11. #if !UNITY_DISABLE_MANAGED_COMPONENTS
    12.                 using (s_CreateCompanionGameObjects.Auto())
    13.                     conversion.MappingSystem.CreateCompanionGameObjects();
    14. #endif
    15.  
    16.                 using (s_GenerateLinkedEntityGroups.Auto())
    17.                     conversion.MappingSystem.GenerateLinkedEntityGroups();
     
  6. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    Sorry but I don't understand. I just created a simple prefab and generate an entity with it. There is no hierarchy. If you look at the attached image of the component value, you can see that the value is the same as the entity.

    Is it adding itself by default? If so, why?
     
  7. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    That's bad. And I don't understand why the size of it too big. Isn't it just a reference value?
     
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    For performance by default buffers allocate into the chunk. Only once the buffer exceeds it's max chunk capacity is it moved out.

    This is defined by
    InternalBufferCapacityAttribute

    Where
    public const int DefaultBufferCapacityNumerator = 128;

    So as LinkedEntityGroup does not assign a InternalBufferCapacity it allocates a size of 128.
    The remaining 16 come from the actual buffer data (m_InternalCapacity etc).

    So yeah by default, all buffers have a size of 144 but if you add a InternalBufferCapacity[0] to your IBufferElement then it should only be 16 in your chunk.
     
  9. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    Such a waste... I removed the LinkedEntityGroup component after intantiating the entity. Hope it won't cause any problems.
     
    Last edited: Apr 10, 2022
  10. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    180
    Why not just remove the buffer if you don't need it?
     
  11. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    Well I just did that and edited my comment at the same time you post this :)
     
    Krooq likes this.
  12. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    943
    LinkedEntityGroup requires the first element as the root parent. It must have added itself automatically when the LinkedEntityGroup elements are created even when there are no child entities to add.