Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Custom struct in BlobArray

Discussion in 'Data Oriented Technology Stack' started by swejk, Jun 20, 2019.

  1. swejk

    swejk

    Joined:
    Dec 22, 2013
    Posts:
    19
    Hi,

    I have an issue with struct elements in BlobArray, where they have default values in runtime. I think that I am not initializing the array correctly, but I cant find the correct approach anywhere.

    Concretely, with this code, when accessing clipDataRef.Value.Motions from some job, all elements will have default values ( StartFrame = 0, EndFrame = 0, Translation = {0,0,0}).

    Any advice how can I overcome this please ?

    Code (CSharp):
    1. [Serializable]
    2. public struct Motion {
    3.   public int StartFrame;
    4.   public int EndFrame;
    5.   public float3 Translation;
    6. }
    7.  
    8. [Serializable]
    9. public struct Clip {
    10.   public bool Loop;
    11.   public BlobArray<Motion> Motions;
    12. }
    13.  
    14. public class ClipData : MonoBehaviour {
    15.    public Motion[] Motions;
    16.    public bool Loop;
    17. }
    18.  
    19.  
    20. class SomeConversionSystem : GameObjectConversionSystem {
    21.  
    22.   protected override void OnUpdate() {
    23.     Entities.ForEach((ClipData clipData) =>{
    24.         var builder = new BlobBuilder(Allocator.Temp);
    25.         ref var root = ref builder.ConstructRoot<Clip>();
    26.         root.Loop = clipData.Loop;
    27.         var motions = builder.Allocate(clipData.Motions.Length, ref root.Motions);
    28.  
    29.         for(int i = 0; i < clipData.Motions.Length; i++) {
    30.            motions[i] = clipData.Motions[i];
    31.         }
    32.  
    33.        var clipBlob = builder.CreateBlobAssetReference<Clip>(Allocator.Persistent);
    34.  
    35.       builder.Dispose();
    36.  
    37.      var entity = GetPrimaryEntity(clipData.gameObject);
    38.      DstEntityManager.AddComponentData(entity,
    39.                 new SomeComponent {ClipDataRef = clipBlob});
    40.      });
    41.   }
    42. }
     
  2. swejk

    swejk

    Joined:
    Dec 22, 2013
    Posts:
    19
    I did not access the blobReference value correctly. I have this solved now.
    Code (CSharp):
    1. // Bad
    2. var clips = clipDataRef.Value.Motions
    3.  
    4. // Good
    5. ref var clips = ref clipDataRef.Value.Motions
    6.  
     
    JonasMumm likes this.
  3. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    101
    It's been mentioned in another thread that they will add static analysis tools to the compiler to prevent this mistake from happening.
     
    swejk likes this.
  4. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    9
    I've been looking for information about blobs, what is the advantage over a nativearray? Can it store more complex datastructures? eg. An Array of array (eg. If I had to store a rotation for each bone in one animation struct).
     
  5. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    651
    It can store arbitrary data structures, but the best way to think of it is an in-memory file, with the root blob struct being the header. It can have it's own data, but
    BlobPtr<T>
    ,
    BlobArray<T>
    , and others store offsets into that file from the header.
     
    JonasMumm likes this.
unityunity