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

Resolved Baker.GetEntity doesn't return world-wide Entity

Discussion in 'Entity Component System' started by Knedlo, Jan 15, 2023.

  1. Knedlo

    Knedlo

    Joined:
    Oct 15, 2012
    Posts:
    40
    Hi there,

    I have a problem getting correct Entities in a BlobAsset on a component baked in a subscene. It looks like the entitiy ids that gets stored in a BlobArray are relative to the subscene. The code follows:

    Code (CSharp):
    1. public struct Grid_C : IComponentData
    2.     {
    3.         public int Width;
    4.         public int Height;
    5.  
    6.         public BlobAssetReference<Tiles> Tiles;
    7.     }
    8.  
    9.     public class GridAuthoring : MonoBehaviour
    10.     {
    11.         public int width;
    12.         public int height;
    13.  
    14.         public List<GameObject> tiles = new();
    15.     }
    16.    
    17.     public class GridBaker : Baker<GridAuthoring>
    18.     {
    19.         public override void Bake(GridAuthoring authoring)
    20.         {
    21.             if (authoring.tiles.Count != authoring.width * authoring.height) throw new Exception($"Tile count is not equal to Width by Height.");
    22.            
    23.             var buffer = AddBuffer<Pieces_DB>();
    24.             // Create and set tiles blobArray to Grid. It is then used to address tiles by coordinates.
    25.             using var bb = new BlobBuilder(Allocator.Temp);
    26.             ref var tilesBlobAsset = ref bb.ConstructRoot<Tiles>();
    27.             var tilesArray = bb.Allocate(ref tilesBlobAsset.Value, authoring.tiles.Count);
    28.             foreach (var tile in authoring.tiles)
    29.             {
    30.                 var coord = GetComponent<CoordinatesAuthoring>(tile);
    31.                 var tileEntity = GetEntity(tile);
    32.                 buffer.Add(tileEntity);
    33.                 tilesArray[coord.coordinates.ToFlatArrayIndex(authoring.width)] = tileEntity;
    34.                 Debug.Log($"Coords: {coord.coordinates} => tile: {tileEntity}");
    35.             }
    36.             var blob = bb.CreateBlobAssetReference<Tiles>(Allocator.Persistent);
    37.             AddBlobAsset(ref blob, out var hash);
    38.             AddComponent(new Grid_C()
    39.             {
    40.                 Width = authoring.width,
    41.                 Height = authoring.height,
    42.                 Tiles = blob,
    43.             });
    44.         }
    45.     }
    Now I am logging the entites and can clearly see, that their IDs are wrong. However there's also a buffer that I'm adding the enities to and when I inspect that, it has correct entity ids. I am thinking that the baking process finalizes entity references in later stages than when I am making the BlobAsset, but I couldn't find any mention of that in documentation.

    What is also weird is, that I am unable to break the execution in the baker.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    You can't store entities in blob assets, it won't remap on load.

    Remapping is only supported in IComponentData and IBufferElement
     
    Knedlo likes this.
  3. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Knedlo likes this.
  4. Knedlo

    Knedlo

    Joined:
    Oct 15, 2012
    Posts:
    40
    Thanks guys,

    I wasn't planning on serializing/deserializing the data, but rather store them upon init and use them during one session. I was using this approach with convert workflow without subscenes (where I was creating the blob during conversion) and it was working as expected.

    I have an alternative solution using Aspects, so I'll try that.

    @WAYN_Games Thanks for the link, I will check it out. I was actually watching your video on baking yesterday, which explained that subscenes are baked in isolation. I would swear I read the whole docs about baking, streaming and blobs, but I didn't find anything about this.
     
  5. fas3r_

    fas3r_

    Joined:
    May 6, 2021
    Posts:
    11
    Hello,

    I started to look at ECS again, and I was using something similar than Knedlo. Now looking at the latest documention on entities ( 1.0.0.pre-65 ), I'm wondering if it could be useful to combine "UntypedWeakReferenceId" and "Blob asset"

    As indicated in the documentation, "An Editor script can write weak reference IDs to a ScriptableObject then later, during the baking process, a baker can read the IDs from the ScriptableObject and write them into an ECS component."

    Would it be useful to store the reference Id of the prefab in a BlobArray of strings and then use `
    RuntimeContentManager` to load async the prefab from a system ? maybe burst system ?

    thanks