UE5 Pointer To Delegate As Class Variable

Advertisement

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()

5/5

Gamechanger plugin for notehighway rhythm games and music visualizers

5/5

Add Text and 2D UMG widgets to Level Sequences

Consider investing in some of our plugins. They might save you some time.  Also helps supports these tutorials.

Instantly get access to our plugins like Blender Curves Importer and UMG Cinematics and more when  you support us on Patreon!

 Join Us On Discord For More Daily Tips!