Search Unity

Order of execution in UnityEvent.Invoke().

Discussion in 'Scripting' started by m_szadko, Aug 10, 2018.

  1. m_szadko

    m_szadko

    Joined:
    Jul 11, 2018
    Posts:
    1
    I know that there was similar threads but I'd like to get some official piece of documentation.

    In those threads :
    https://forum.unity.com/threads/solved-unityevent-invoke-calling-order.265292/#post-1753393
    https://forum.unity.com/threads/the-order-in-which-unityevents-are-invoked.286496/
    https://forum.unity.com/threads/button-on-click-execution-order.497841/

    we can read that "...So internally this uses a delegate where call order is not defined..."

    but I've found this
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/using-delegates
    and there is a sentence "...When allMethodsDelegate is invoked, all three methods are called in order...".
    In addition this method
    https://docs.microsoft.com/en-us/do...ate.getinvocationlist?view=netframework-4.7.2
    suggest that delegates are called in order of the occurrence in the invocation list.

    So is there any official info about order invoking methods on UnityEvent?
    And what about C# delegates? Am I missunderstanding something from the docs or its just really common mistake that delegates are invoking in random order?

    If you can post some official writings about those topics I would really appreciate it.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    C# delegates are executed in the order they are added. Per their documentation.

    UnityEvent is not necessarily a delegate (though it is defined to be implemented by utilizing a delegate at some point internally).

    It's order is undefined not because of the delegate. But because there is no guarantee of the order in which the delegate is built.

    There's 2 common reasons something like this is left undefined.

    1) the designer is lazy and didn't define it (a total possibility if you've looked through Unity's documentation)

    2) the designer explicitly leaves it undefined (usually they describe it as undefined) because they want to allow themselves the ability to change the ordering in the future if there is ever a need to change it to resolve issues like bugs, speed issues, or any other sort of refactoring that may occur.

    This is similar to Components on a GameObject. Just because you see them in some order in the inspector, Unity does not define that order. At runtime the order in which the components have Awake/Start/Update called, how they're returned by GetComponents, and all that is undefined. Just in case they ever have to change it for whatever reason.

    Since UnityEvent explicitly say the call order is not defined we have a case 2 situation.

    What they mean is that "do not rely on the call order as it could change at any time".

    I've also found that in cases like this (such as the components), the order changes between editor and builds.
     
  3. Wilhelm_LAS

    Wilhelm_LAS

    Joined:
    Aug 18, 2020
    Posts:
    55
    Is it order dependent now? I need to call UnityEvent's in order. I already saw answers from 2014, 2018, 2020 which is too old but not 2021-2022 or 2023.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    The source code is here from what I can see: https://github.com/Unity-Technologi.../Runtime/Export/UnityEvent/UnityEvent.cs#L742

    It all looks like a list that's added to so likely in the order they're added but the source is right there. It's not like there'd be any good reason to shuffle the order randomly really.

    Most things don't specify order simply because you shouldn't ever rely on an order because that's fragile.
     
    Bunny83 and Wilhelm_LAS like this.