How to add a interface function to a multicast delegate?

Hello community,

I want to extend my actors with an event which gets broadcasted by an other actor’s multicast event.

For that I made an interface to ensure that the receiving Actor implements the right function with the right signature. The receiving actor just has to register this function to the broadcasting actor’s multicast delegate. I know I can use GetAllActorsWithInterface. I don’t want to use it because it doesn’t perform well with 1000s of actors.

Since the interface is an object I try to use AddUObject but can’t get it to work.
Hope someone can point me in the right direction.



DECLARE_MULTICAST_DELEGATE_TwoParams(FOnMyEventSignature, int32, bool);
    
UCLASS(BlueprintType)
class GAME_API ABroadcastingActor : public AActor
{
    ...
    void RegisterActor(AActor* Actor)
private:
    TArray<FOnMyEventSignature> MyEventDelegates;
};

void ABroadcastingActor::RegisterActor(AActor* Actor)
{
    IMyInterface* Interface = Cast<IMyInterface>(Actor);
    if (Interface)
    {
    	FOnMyEventSignature OnMyEvent = MyEventDelegates[0];
    	**OnMyEvent.AddUObject(Interface, &IMyInterface::OnMyInterfaceFunction);**
    }
}
    
class IMyInterface
{
    GENERATED_IINTERFACE_BODY()
public:
    UFUNCTION(BlueprintImplementableEvent)
    void OnMyInterfaceFunction(int32 MessageID, bool IsActive);
};