I am developing a plugin that works on both UE4 and UE5. UE5 has the UAudioDeviceNotificationSubsystem which I am using in the following manner:
// in definition of class "Foo"
UFUNCTION()
void OnDefaultAudioRenderDeviceChanged(EAudioDeviceChangedRole AudioDeviceRole, FString DeviceId);
// in Foo.cpp
#if ENGINE_MAJOR_VERSION == 5
UAudioDeviceNotificationSubsystem::Get()->DefaultRenderDeviceChanged.AddDynamic(this, &Foo::OnDefaultAudioRenderDeviceChanged);
#endif
void Foo::OnDefaultAudioRenderDeviceChanged(EAudioDeviceChangedRole AudioDeviceRole, FString DeviceId)
{
#if ENGINE_MAJOR_VERSION == 5
// do stuff
#endif
}
This works fine on UE5 but I cannot get it to compile on UE4 no matter what I try. Obviously EAudioDeviceChangedRole is not defined in UE4 so I am trying to conditionally define it but cannot manage to do so in a way that would work on both UE4 and UE5. UHT either complains about defining stuff within preprocessor statements, about EAudioDeviceChangedRole not being a UCLASS, USTRUCT or ENUM or about duplicate definitions.
Is there any way to make use of UAudioDeviceNotificationSubsystem on UE5 while still having the same code files for UE4 (even if it is a dummy OnDefaultAudioRenderDeviceChanged definition doing nothing). The most relevant topic I found was #define doesn't work with UFUNCTIONS which suggests this is not possible, but here’s hoping something changed or the right person didn’t see that topic. Surely this is a limitation that someone else has encountered/overcome since UE5 was released?