UE5 Pointer To Delegate As Class Variable

You Cannot Set A Member Variable As A Pointer To A Delegate

Almost all the time, you want a reference to unreal engine delegates, not a copy. At this point you may have tried and attempted to do something like:

UPROPERTY()

FSomeDelegate* delegatePtr;

Or

FSomeDelegate& delegateRef;

 

This will not work and Visual Studio will have warned you even before building or as building errors. Specifically for the Pointer type, You may get the error: 

 

cannot have an exposed pointer to this type.

 

and for the reference method , you may get a build error saying something along the lines of: 

 

references must be initialized.

 

That is because in c++ a reference needs to be set at declaration.

The Workaround

A solution that often works is to get a pointer to the UObject* that contains the unreal engine delegate. The downside is you will need to go through this UObject* to access the delegate every time in code but it’s a small tradeoff. Here is an example of calling the Broadcast function through the UObject.

UObject->DelegateVariable.Broadcast()