Search Unity

Convenience feature: GetSingleton() ref return

Discussion in 'Entity Component System' started by Ramobo, Oct 12, 2019.

  1. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    212
    I say it would in some cases be more convenient than having to do SetSingleton(). e.g.:
    Code (CSharp):
    1. ref SingletonInts singletonInts = ref GetSingleton<SingletonInts>();
    2.  
    3. singletonInts.value1 = 0;
    4. singletonInts.value2 += 1;
    As opposed to:
    Code (CSharp):
    1. SingletonInts singletonInts = GetSingleton<SingletonInts>();
    2.  
    3. SetSingleton(new SingletonInts
    4. {
    5.     value1 = 0
    6.     value2 = singletonInts.value2 + 1
    7. });
    You can't access the ref variable from a lambda expression due to what sounds like an edge case to me, but this is still useful for, say, an input system.
     
    Last edited: Oct 12, 2019