Search Unity

Feature Request IsCreated() for SharedStatic

Discussion in 'Burst' started by CookieStealer2, Jun 14, 2022.

  1. CookieStealer2

    CookieStealer2

    Joined:
    Jun 25, 2018
    Posts:
    119
    It would be nice if we could get some methods for checking if a SharedStatic instance is created. Currently we can only use GetOrCreate() and as far as I can tell there is no way of telling if we got or created an instance.

    The docs say:
    To me that sounds like the instance is allocated but not cleared. So It can have any values, making it impossible to tell if it was created or not.

    Although, how SharedStatic is being used by the TypeManager.GetTypeIndex<T>() in Unity.Entities contradicts this:
    Code (CSharp):
    1.  
    2.         // Marked as internal as this is used by StaticTypeRegistryILPostProcessor
    3.         internal sealed class SharedTypeIndex<TComponent>
    4.         {
    5.             public static readonly SharedStatic<int> Ref = SharedStatic<int>.GetOrCreate<TypeManagerKeyContext, TComponent>();
    6.         }
    7.  
    8.         public static int GetTypeIndex<T>()
    9.         {
    10.             var index = SharedTypeIndex<T>.Ref.Data;
    11.  
    12.             if (index <= 0)
    13.             {
    14.                 ManagedException<T>();
    15.                 BurstException<T>();
    16.             }
    17.  
    18.             return index;
    19.         }
    Here they simply check if the index is 0 or less. So what does "undefined initialization state" mean?
    Is memory cleared or not? Even if it is cleared I think It would be nice to have IsCreated() methods to avoid creating unnecessary instances.
     
    jamesnw likes this.