Can delegate definitions be defined in their own separate header file?

I’d like to have a C++ header file that just contains delegate definitions. Something like this?

#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(F_Message, FString, Message);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(F_Error, FString, ErrorMessage);

When I try to do this and compile, I get tons of compile errors. The only why I’ve worked around this in the past was to create a dummy C++ class inherited from UObject, where the class is completely empty. Does this have something to do with needing a .generated.h file as well?

Same problem here.

Hi Boon…

Ultimately what I did was just create a brand new class… Left it empty and defined my delegates above it.

Don’t really like having to do that, but it works.

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(F_Message, FString, Message);

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(F_FileFound, FString, FileName);


UCLASS()
class SOME_API UCustomDelegates : public UObject
{
	GENERATED_BODY()
};
1 Like

You don’t need the UCLASS(), there is a UDELEGATE() macro for this case specifically. Remember to add the #include "<FileName>.generated.h" to the includes above.

Take a look at “Engine/Source/Runtime/UMG/Public/Animation/WidgetAnimationEvents.h” from the engine sources as an example (https://github.com/EpicGames/UnrealEngine/blob/16dc333db3d6439c7f2886cf89db8907846c0e8a/Engine/Source/Runtime/UMG/Public/Animation/WidgetAnimationEvents.h#L9 on github)

2 Likes

Thanks, I’ll take a look!