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

BlobAssetReference in other BlobAsset

Discussion in 'Entity Component System' started by UrsusDominatus1, Oct 19, 2021.

  1. UrsusDominatus1

    UrsusDominatus1

    Joined:
    Dec 23, 2020
    Posts:
    6
    Are there known possibilities to create reference to blob asset inside other blob asset? For example, I have bottom tier data blobs that can be reused in some higher level structures? As far as I understand BlobPtr is method to point to data in same blob asset. Thank you in advance
     
  2. herkip

    herkip

    Joined:
    Dec 21, 2013
    Posts:
    30
    Do you mean something like that?

    Districts has sub reference Modifiers.

    Code (CSharp):
    1. public BlobAssetReference<BlobHashMap<FixedString64, DistrictMemory>> Districts;
    Code (CSharp):
    1.  
    2.     public readonly struct DistrictMemory
    3.     {
    4.         public readonly FixedString64 Tag;
    5.         public readonly FixedString128 Name;
    6.         public readonly uint BuildTime;
    7.         public readonly uint BuildPointCost;
    8.         public readonly uint Cost;
    9.  
    10.         public readonly BlobAssetReference<BlobArray<FixedString64>> Modifiers;
    11.  
    12.         public DistrictMemory(FixedString64 tag, FixedString128 name,
    13.             uint buildTime, uint buildPointCost, uint cost, BlobAssetReference<BlobArray<FixedString64>> modifiers)
    14.         {
    15.             Tag = tag;
    16.             Name = name;
    17.             BuildTime = buildTime;
    18.             BuildPointCost = buildPointCost;
    19.             Cost = cost;
    20.             Modifiers = modifiers;
    21.         }
    22.     }
    If this is what you are looking here are snippets how I am building it.


    Code (CSharp):
    1.  
    2.         private BlobAssetReference<BlobHashMap<FixedString64, DistrictMemory>>         LoadDistricts(DataContainer dataContainer)
    3.         {
    4.             var source = new NativeHashMap<FixedString64, DistrictMemory>(4, Allocator.Temp);
    5.  
    6.             for (int index = 0; index < dataContainer.Jobs.Count; index++)
    7.             {
    8.                 var district = dataContainer.Districts[index];
    9.              
    10.                 source.Add(district.Tag, new DistrictMemory(
    11.                     district.Tag,
    12.                     district.Name,
    13.                     district.BuildTime,
    14.                     district.BuildPointCost,
    15.                     district.Cost,
    16.                     AddModifiers(district.Modifiers.ToArray())));
    17.             }
    18.          
    19.             BlobBuilder builder = new BlobBuilder(Allocator.Temp);
    20.             ref var root = ref builder.ConstructRoot<BlobHashMap<FixedString64, DistrictMemory>>();
    21.             builder.ConstructHashMap(ref root, ref source);
    22.             var blobMapRef = builder.CreateBlobAssetReference<BlobHashMap<FixedString64, DistrictMemory>>(Allocator.Persistent);
    23.  
    24.             return blobMapRef;
    25.         }
    Code (CSharp):
    1.  
    2.         private BlobAssetReference<BlobArray<FixedString64>> AddModifiers(params string[] tags)
    3.         {
    4.             using (var builder = new BlobBuilder(Allocator.Temp)) {
    5.                 ref BlobArray<FixedString64> root = ref builder.ConstructRoot<BlobArray<FixedString64>>();
    6.  
    7.                 var nodearray = builder.Allocate(ref root, tags.Length);
    8.  
    9.                 for (var index = 0; index < tags.Length; index++)
    10.                 {
    11.                     var tag = tags[index];
    12.                     nodearray[index] = tag;
    13.                 }
    14.  
    15.                 return builder.CreateBlobAssetReference<BlobArray<FixedString64>>(Allocator.Persistent);
    16.             }
    17.         }
     
  3. UrsusDominatus1

    UrsusDominatus1

    Joined:
    Dec 23, 2020
    Posts:
    6
    Thank you for quick response. Yes, your case is similar to mine. But I made tiny example that failed for me:

    public struct TestStruct
    {
    public BlobAssetReference<float> blobAssetFloat;
    }
    ....
    var bb = new BlobBuilder(Allocator.Temp);
    ref var ts = ref bb.ConstructRoot<TestStruct>();


    And I have a compile error:
    error ConstructBlobWithRefTypeViolation: You may not build a type TestStruct with Construct as TestStruct.blobAssetFloat.m_data.m_Ptr is a reference or pointer.  Only non-reference types are allowed in Blobs.


    I use Entities 0.17.0-preview.42 package with Unity 2021.2.0b16. Yes I know about "2020 only supported version, but everything went smooth for now. What version are you using?
     
  4. UrsusDominatus1

    UrsusDominatus1

    Joined:
    Dec 23, 2020
    Posts:
    6
  5. herkip

    herkip

    Joined:
    Dec 21, 2013
    Posts:
    30
    Sorry about that I forgot that I had to change the same thing to get it working.

    And I am using 2021.2 too. UI toolkit is so much better on the newer version.
     
    UrsusDominatus1 likes this.
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can have BlobPtr pointing to any data in the same blob. But right now we do not support blobs having ptrs to other independent blobs. You have to handle that yourself. Eg. store an index that in a BlobArray that lives in another Blob.

    Depending on what you need, you may also need a way of looking up the other referenced blob asset. Some ID or index or whatever might work for you. It just totally depends on the exact problem you are trying to solve and how your blob is accessed at runtime.


    Of course, in many situations, just having a bigger blob that basically has both of your blobs combined into one might be the better approach, then you can just setup use BlobPtr. If that is an option, I would expect that to be both simpler in runtime code to manage& faster.
     
  7. UrsusDominatus1

    UrsusDominatus1

    Joined:
    Dec 23, 2020
    Posts:
    6
    Thank you very mush for responses. Joachim, indexing into other blob array is actually viable solution, I didn't think that way, thank you. Making self contained blob asset is always an option, but I try study every aspect of Unity ECS, and wanted to solve my tasks the "proper and logical way" first. Besides such interblob referenses are logical requirement for complex data structures.