Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. 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

UnityEvent source code extremely suspicious usage of invokes

Discussion in 'Scripting' started by doctorpangloss, Dec 14, 2019.

  1. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    254
    Code (CSharp):
    1.     /// <summary>
    2.     ///   <para>Invoke all registered callbacks (runtime and persistent).</para>
    3.     /// </summary>
    4.     public void Invoke()
    5.     {
    6.       List<BaseInvokableCall> baseInvokableCallList = this.PrepareInvoke();
    7.       for (int index = 0; index < baseInvokableCallList.Count; ++index)
    8.       {
    9.         InvokableCall invokableCall1 = baseInvokableCallList[index] as InvokableCall;
    10.         if (invokableCall1 != null)
    11.         {
    12.           invokableCall1.Invoke();
    13.         }
    14.         else
    15.         {
    16.           InvokableCall invokableCall2 = baseInvokableCallList[index] as InvokableCall;
    17.           if (invokableCall2 != null)
    18.           {
    19.             invokableCall2.Invoke();
    20.           }
    21.           else
    22.           {
    23.             BaseInvokableCall baseInvokableCall = baseInvokableCallList[index];
    24.             if (this.m_InvokeArray == null)
    25.               this.m_InvokeArray = new object[0];
    26.             baseInvokableCall.Invoke(this.m_InvokeArray);
    27.           }
    28.         }
    29.       }
    30.     }
    Why is the invoke being casted* twice?
     
    Last edited: Dec 14, 2019
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,527
    Where is it being called twice?
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,365
    It's not. If the first attempt succeeds, it stops there, but if it fails it attempts a second time. If the second attempt succeeds, it stops there, but if it fails it attempts a third time. At least that's my interpretation of what it's attempting to do by trying to make multiple attempts. Regardless though it's written to only succeed once before it stops.
     
  4. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    254
    The same cast and null check is done twice. That's definitely a pretty strange thing to do.
     
  5. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    634
    I’d have to agree that this is a bit odd.
    The first if else block if you already know that the invocable call is already null. Why check it another time with the exact same type the exact same way if the first one failed. This is something that is always null and cannot be not null.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,527
    Oh. Yeah, that part looks like a mistake.
     
unityunity