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

Question Correct way of handling dynamic entities SubScenes within LinkedEntityGroup

Discussion in 'Entity Component System' started by instriker_911, Nov 10, 2022.

  1. instriker_911

    instriker_911

    Joined:
    Mar 31, 2022
    Posts:
    27
    Context

    I have some cases where the bakers create the top levels entities, but the systems create attached entities dynamically at runtime.

    Here is a sample code that emulate what I'm trying to do:

    Code (CSharp):
    1.  
    2. public struct DynamicSubSceneEntitiesInit : IComponentData
    3. {
    4.     public int dummy;
    5. }
    6.  
    7. public class DynamicSubSceneEntitiesSpawner : MonoBehaviour
    8. {
    9.     private class Baker : Baker<DynamicSubSceneEntitiesSpawner>
    10.     {
    11.         public override void Bake(DynamicSubSceneEntitiesSpawner authoring)
    12.         {
    13.             AddComponent(new DynamicSubSceneEntitiesInit());
    14.         }
    15.     }
    16. }
    17.  
    18. public partial struct DynamicSubSceneEntitiesSpawnerSystem : ISystem
    19. {
    20.     public void OnCreate(ref SystemState state) { }
    21.  
    22.     public void OnDestroy(ref SystemState state) { }
    23.  
    24.     public void OnUpdate(ref SystemState state)
    25.     {
    26.         var ecb = new EntityCommandBuffer(Allocator.Temp); ;
    27.         foreach (var (_current, entity) in SystemAPI.Query<RefRO<DynamicSubSceneEntitiesInit>>().WithEntityAccess())
    28.         {
    29.             var newEntity = ecb.CreateEntity();
    30.             ecb.AddComponent(newEntity, new Parent { Value = entity });
    31.             ecb.AddComponent(newEntity, new LocalToWorld());
    32.             ecb.AddComponent(newEntity, new Translation() { Value = new float3(2, 2, 2) });
    33.  
    34.             ecb.RemoveComponent<DynamicSubSceneEntitiesInit>(entity);
    35.             var buffer = ecb.AddBuffer<LinkedEntityGroup>(entity);
    36.             buffer.Add(entity);
    37.             buffer.Add(newEntity);
    38.         }
    39.         ecb.Playback(state.EntityManager);
    40.     }
    41. }
    42.  
    In unity, I create a simple SubScene with a single entity containing the authoring class for the baker:

    upload_2022-11-9_20-50-51.png

    Problem

    I can run the project and everything goes well at runtime until I stop debugging, then I get the following error on the scene unload:

    What it look likes is the dynamically created entity added to the LinkedEntityGroup does not have the SceneTag, so the delete just fail.

    Question

    What is the recommanded way of handling the dynamic entities so they are attached correctly to the subscene? Should the Scene system automatically add the SceneTag when the entity parent / LinkedEntityGroup is added to an other entity also having a SceneTag?

    What would be the best practices?

    Thank you.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    I wouldn't attach LinkedEntityGroup to a subscene entity dynamically. Instead, make one of your dynamically generated entities have the LEG and whenever its parent becomes invalid, have a system destroy that generated entity.
     
  3. instriker_911

    instriker_911

    Joined:
    Mar 31, 2022
    Posts:
    27
    I guess this would work. But this still feel strange. In my case, the baked entity is the a Map Entity + Loader Entity.

    At runtime, I load the map data, then create maps cells entities based on the map size. I was adding the cells as Children + LinkedEntityGroup of the map so when I delete the Map, the cell would be gone with it.

    Maybe there is something wrong with my understanding of the LinkedEntityGroup, but I though this was this use case where I can delete the root and all the dependants entities would be gone. So that seems strange to write what seems to be a boiler plate code system to do that cleanup when a feature handling that was already implemented.

    Prior to 1.0, the subscene where optionals, so I didn't have this issue.

    Would you still recommend to do this "cleanup system" based on those additionnal infos?
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    You aren't deleting the root. Unity is deleting the root during subscene unload using API that isn't fully compatible with LinkedEntityGroup. Some may consider it a Unity bug. Personally, I recognize the performance tradeoff being made. And because of that, I still recommend my solution.
     
    instriker_911 likes this.