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 Issue with BlobAsset Construction and Array Lengths

Discussion in 'Entity Component System' started by Dog_Like, Jul 10, 2023.

  1. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    I am trying to use Blob in my code and encountered an issue. Here is the problem I'm facing:

    In my code, I have a struct called BlobNavNode which contains several fields, including BlobArray<int> Vertex, BlobArray<int> ConnectionIndex, and BlobArray<long> ConnectionCost. I am using the BlobBuilder to construct a BlobAsset containing an array of BlobNavNode structs.

    However, when I inspect the constructed BlobAsset at runtime, I noticed that the lengths of the ConnectionCost, ConnectionIndex, and Vertex arrays within each BlobNavNode are all zero. This is unexpected because I specified the lengths for these arrays during construction.

    Here is a snippet of my code:

    Code (CSharp):
    1. // ...
    2.  
    3. var nodeBuilder = new BlobBuilder(Allocator.Temp);
    4. ref var nodeData = ref nodeBuilder.ConstructRoot<BlobArray<BlobNavNode>>();
    5. var nodeArrayBuilder = nodeBuilder.Allocate(ref nodeData, nodes.Count);
    6.  
    7. for (var index = 0; index < nodes.Count; index++)
    8. {
    9.     var node = nodes[index];
    10.     ref var blobNode = ref nodeBuilder.ConstructRoot<BlobNavNode>();
    11.  
    12.     // Set fields of blobNode...
    13.  
    14.     nodeBuilder.Construct(ref blobNode.ConnectionCost, node.connectionCost);
    15.     nodeBuilder.Construct(ref blobNode.ConnectionIndex, node.connectionIndex);
    16.     nodeBuilder.Construct(ref blobNode.Vertex, node.vertex);
    17.  
    18.     nodeArrayBuilder[index] = blobNode;
    19. }
    20.  
    21. // ...
    22.  
    I'm not sure why the lengths of these arrays are not being set correctly. Any insights or suggestions on how to resolve this issue would be greatly appreciated.

    Thank you!
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,976
    Don't call ConstructRoot on BlobNavNode since that isn't actually a root of your blob.
     
  3. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    The problem has been resolved.
     
  4. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    I need a BlobArray of BlobNavNode. Each BlobNavNode needs to be constructed individually. If I don't do it this way, I receive a prompt suggesting to do so.
    The problem has been resolved.
    The issue was caused by the order of construction in Construct.