How to add a interface function to a multicast delegate?


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.
I just get:

error C2338: You cannot use UObject method delegates with raw pointers.

Hope someone can point me in the right direction.

DECLARE_MULTICAST_DELEGATE_TwoParams(FOnMyEventSignature, int32, bool);

class GAME_API ABroadcastingActor : public AActor
	void RegisterActor(AActor* Actor)
	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
	void OnMyInterfaceFunction(int32 MessageID, bool IsActive);

Unreal engine’s interface support is very limited. Either you cast it to the “UObject” class your actor inherits or you make an interface that takes a delegate reference and register it in your actor instead with the “UObject” pointer (i.e. the “this” keyword). The latter is probably the best design.