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 vs Pointer

Discussion in 'Entity Component System' started by l33t_P4j33t, Jan 1, 2021.

  1. l33t_P4j33t

    l33t_P4j33t

    Joined:
    Jul 29, 2019
    Posts:
    232
    is it good design to store
    Code (csharp):
    1. struct Component : IComponentData {
    2.    public BlobAssetReferece<T> Value;
    3. }
    as just
    Code (csharp):
    1. struct Component : IComponentData {
    2.    public T* Value;
    3. }
    ?
    my thinking is that if the blob's lifetime is controlled elsewhere, then there's no point of boxing T. its just extra fluff since all a blob asset reference is it seems is a container around a pointer that essentially does this
    Code (csharp):
    1. struct BlobAssetReferece<T> {
    2.    T *m_Field;
    3.    public T Get() => *m_Field;
    4. }
    also i like wacky symbols like -> * that make the source code look cool
     
    Last edited: Jan 1, 2021
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    Burst might get extra safety checks or optimizations around this. Plus, you don't have to declare the component as unsafe.
     
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    I think intent is important. The blob structs are designed for a certain purpose.

    If I see a random pointer on a component, I have no idea who or what is responsible for lifetime management and freeing the resource. Whereas if I see a BlobAssetReference I know how it should be handled.

    There's also NativeReference, which you won't be attaching on a component, but provides a safe way to access unmanaged memory.
     
    Baggers_ likes this.
  4. marijnz

    marijnz

    Joined:
    Sep 20, 2012
    Posts:
    67
    +1 to what the others said. Your future self will thank you for not going to wacky (unsafe pointers) way ;)