Performance of Multicast Delegates and their Ordering in Comparison to an Array of Delegates

Under the hood, are multicast delegates implemented using contiguous memory to store the delegates or are they implemented using Linked List like structure?

It does not specific in the Unreal Documentation, but is the order in which delegates are added to the multi-cast delegate always guaranteed? Or are they randomly fired off regardless of their original order in which they were added to the multicast delegate.

What would be the advantages of multicast delegates over just a TArray of delegates where order is assured as well as the fact that would be stored in contiguous memory?

The invocation list is an array of delegate instances.

The order of invocation is not guaranteed by the contract. Technically, the order depends on when delegates are inserted or removed. However, you should not rely on that, because we may change the internal implementation details in the future.

If you maintain your own array of delegates, then you would be in full control of the order of execution. That being said, such a design would also not be advisable, because it will temporarily couple the delegate handlers to each other. You should design your code in such a way that each handler can successfully execute regardless of whether and when any other handlers execute. The handlers should not have to know about each other’s existence. Otherwise you’re asking for subtle and very difficult to find bugs down the road.

Thank you! This is exactly what I wanted to know.

On the subject of delegate coupling, is it therefore inadvisable to “daisy chain” delegates or events together.
i.e

PlayerInput triggers →

UTriggerComponent OnTriggerPulled which then triggers →

BFiringComponent OnWeaponFire- which then triggers →

Both UPatricleEmitter, UAudioComponent’s respective delegates
(these last two would be in no particular order)

OnTriggerPulled, OnWeaponfired are now coupled by order (OnWeaponFired must come after OnTriggerPulled

But OnTriggerPulled is not the only Component which can trigger OnWeaponFired

I wanted to decouple the components of a TriggerComponent and a FiringComponent since an actor may have only one of those. (In FiringComponent’s case their would be other possible Components that could cause it’s delegate to fireoff)

Is this advisable or is this a no-no?

It is OK for the UTriggerComponent to trigger an event on BFiringComponent. What I meant about delegate coupling is that the delegates registered inside the same multicast delegate should not depend on each other, i.e. all handlers of a particular multicast delegate should be independent from each other.

I think I understand now what you mean to accomplish by having an array of delegates. It is not really a delegate that broadcast an event pseudo-simultaneously, but a way to structure the order of execution of multiple functions. I think that is OK if you can make this apparent from your API, so that there is no misunderstanding.