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 BlobAssetReference from Aspects

Discussion in 'Entity Component System' started by macoson, Jun 7, 2023.

  1. macoson

    macoson

    Joined:
    Oct 12, 2020
    Posts:
    4
    Hi All,

    I encountered strange result when trying to access blob data from an aspect. All values are correct when accessing blob asset reference from directly from component but results are incorrect or null once I try to access them via aspect. blob asset references are created in the baker. Here is relevant code:
    Code (CSharp):
    1.     public struct StoredResourcesComponent : IComponentData
    2.     {
    3.         public BlobAssetReference<ResourceInfoPool> ResourceInfoPool;
    4.     }
    5.     public struct ResourceInfoPool
    6.     {
    7.         public BlobArray<ResourceInfo> Resources;
    8.     }
    9.     public struct ResourceInfo
    10.     {
    11.         public BlobString Name;
    12.         public ResourceType ResourceType; //enum
    13.         public bool Perishable;
    14.         public Entity Prefab;
    15.     }
    16.  
    17.     public partial class DebugSystem : SystemBase
    18.     {
    19.         protected override void OnCreate()
    20.         {
    21.             RequireForUpdate<StoredResourcesComponent>();
    22.         }
    23.         protected override void OnUpdate()
    24.         {
    25.             Enabled = false;
    26.  
    27.             Debug.Log("#### FROM ASPECT ####");
    28.             var resEntity = SystemAPI.GetSingletonEntity<StoredResourcesComponent>();
    29.             var resAspect = SystemAPI.GetAspect<ResourcesStoreAspect>(resEntity);
    30.             ref ResourceInfo resourceInfoBlob = ref resAspect.AllResources.Resources[0];
    31.             Debug.Log("resourceInfo.Name =" + resourceInfoBlob.Name.ToString());
    32.             Debug.Log("resourceInfo.ResourceType =" + resourceInfoBlob.ResourceType.ToString());
    33.             Debug.Log("resourceInfo.Perishable =" + resourceInfoBlob.Perishable.ToString());
    34.             Debug.Log("resourceInfo.Prefab =" + resourceInfoBlob.Prefab.ToString());
    35.  
    36.             Debug.Log("#### FROM COMPONENT ####");
    37.             var resComponent= SystemAPI.GetSingleton<StoredResourcesComponent>();
    38.             ref BlobArray<ResourceInfo> resourceInfoBlobArray = ref resComponent.ResourceInfoPool.Value.Resources;
    39.             ref ResourceInfo resourceInfo = ref resourceInfoBlobArray[0];
    40.             Debug.Log("resourceInfo.Name =" + resourceInfo.Name.ToString());
    41.             Debug.Log("resourceInfo.ResourceType =" + resourceInfo.ResourceType.ToString());
    42.             Debug.Log("resourceInfo.Perishable =" + resourceInfo.Perishable.ToString());
    43.             Debug.Log("resourceInfo.Prefab =" + resourceInfo.Prefab.ToString());
    44.         }
    45.     }
    46.  
    47.     public readonly partial struct ResourcesStoreAspect : IAspect
    48.     {
    49.         readonly Entity entity;
    50.         readonly RefRW<StoredResourcesComponent> storedResources;
    51.         public ResourceInfoPool AllResources => storedResources.ValueRO.ResourceInfoPool.Value;
    52.     }
    Output:

    Code (CSharp):
    1. #### FROM ASPECT ####
    2. resourceInfo.Name =
    3. resourceInfo.ResourceType =-1173548864
    4. resourceInfo.Perishable =True
    5. resourceInfo.Prefab =Entity(96:388)
    6.  
    7. #### FROM COMPONENT ####
    8. resourceInfo.Name =Food
    9. resourceInfo.ResourceType =Food
    10. resourceInfo.Perishable =True
    11. resourceInfo.Prefab =Entity(38:1)

    Can you advise me if I am doing something wrong, or is this an expected behavior and using blobs with aspect doesn't work.
    Thanks in advance
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    1) AllResources in your IAspect needs to be returned by ref.
    2) You can't store entities in blob assets.
     
  3. macoson

    macoson

    Joined:
    Oct 12, 2020
    Posts:
    4
    Thank you for help, public ref ResourceInfoPool AllResources => ref storedResources.ValueRO.ResourceInfoPool.Value; helped.

    Why storing entities in blob asset is not advisable? The code seems to return correct entity reference, and I am not intending to modify stored entities in blob over runtime.
     
  4. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Blob assets are supposed to be read only so storing entity reference in a blob is not advisable since any event invalidation the entity will make the entity reference in the blob invalid.
    That being said if you store a reference to an entity that your are sure won't be invalidated (like a prefab) you should be fine.
    Final not you can't store an entity reference in a blob asset during baking. Blob don't support entity remapping like component or buffers do. So if you store the entity reference from the baker or baking system, you won't get the correct runtime entity reference in your blob.