Hello Unreal Engine forum, I request your assistance.
There seems to be a problem with the following code/Blueprint and I cannot figure out where it goes wrong (also attached as a zip with source, content, uproject and config):
First version:
.h
#include "DelegateObject.generated.h"
DECLARE_DYNAMIC_DELEGATE(FMySimpleDynamicDelegate);
UCLASS(BlueprintType, EditinlineNew)
class BPCS_DELEGATE_PIE_API UDelegateObject : public UObject
{
GENERATED_BODY()
public:
UDelegateObject();
UFUNCTION()
void DefaultDelegateFunction();
UFUNCTION(BlueprintCallable, Category = "Delegate")
bool ExecuteDelegateIfBound();
UPROPERTY(BlueprintReadWrite, Category = "Delegate")
FMySimpleDynamicDelegate MySimpleDynamicDelegate;
};
.cpp
#include "DelegateObject.h"
UDelegateObject::UDelegateObject()
: Super()
{
MySimpleDynamicDelegate.BindDynamic(this, &UDelegateObject::DefaultDelegateFunction);
}
void UDelegateObject::DefaultDelegateFunction()
{
UE_LOG(LogTemp, Log, TEXT("UDelegateObject::DefaultDelegateFunction"));
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("UDelegateObject::DefaultDelegateFunction"));
}
}
bool UDelegateObject::ExecuteDelegateIfBound()
{
return MySimpleDynamicDelegate.ExecuteIfBound();
}
Brief overview (please see zip file for full Actor-Blueprint):
Construction Script: Construct Object, assign to variable, Bind Delegate to BP function which just logs like UDelegateObject::DefaultDelegateFunction, execute
Second version:
.h
#include "GameFramework/Actor.h"
#include "DelegateActor.generated.h"
UCLASS(Blueprintable)
class BPCS_DELEGATE_PIE_API ADelegateActor : public AActor
{
GENERATED_BODY()
public:
ADelegateActor();
virtual void BeginPlay() override;
UPROPERTY(BlueprintReadWrite, Category = "Delegate", EditAnywhere, Instanced)
class UDelegateObject* DelegateObject;
};
.cpp
#include "DelegateActor.h"
#include "DelegateObject.h"
ADelegateActor::ADelegateActor()
: Super()
{
DelegateObject = nullptr;
}
void ADelegateActor::BeginPlay()
{
Super::BeginPlay();
if (DelegateObject)
{
if (!DelegateObject->ExecuteDelegateIfBound())
{
UE_LOG(LogTemp, Log, TEXT("ADelegateActor::BeginPlay delegate object not bound"));
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("ADelegateActor::BeginPlay delegate object not bound"));
}
}
}
}
Brief overview (please see zip file for full Actor-Blueprint):
Construction Script: Construct Object, assign to native member variable, Bind Delegate to BP function which just logs like UDelegateObject::DefaultDelegateFunction, execute
BeginPlay: execute using native member variable
Causing Construction Script to run for both Blueprint Actor subclasses calls the expected delegate. Whenever I “Play in Editor” the delegate is NOT bound at all anymore (or as it turns out, not bound correctly/consistently, see below). Even though it should be bound to some default function. If I use “Play Standalone” instead the blueprint function is called and bound as expected both times, when running construction script and from BeginPlay.
I already tried to debug what happens to the delegates during duplication of the Level for PIE including all of its Actors and stuff but had trouble spotting the exact cause of this so I could then act/fix accordingly.
Although there are a couple of strange things going on. First of all the UDelegateObject’s MySimpleDynamicDelegate should be bound to the Actors. Checking UDelegateObject::ExecuteDelegateIfBound() this is the case when Construction Script runs in the editor. For PIE the Object the delgates are bound to turn into UDelegateObject’s. The function name is still as expected though. So the bound function cannot be found correctly. Either the Object, or the FunctionName does not match.
Stepping over the MySimpleDynamicDelegate.ExecuteIfBound() will then show the this pointer as name “None”_<some numbers> etc.
Looking at the serialization of UDelegateProperties during PIE duplication the TScriptDelegate’s FWeakObjectPtr.Get call (within its serializer) also returns UObjects with name “None”_<some numbers>.
Am I doing something wrong? Is this an issue/bug with PIE?
As promised here’s the zip file: BPCS_Delegate_PIE.zip (379 KB)
I’m using 4.11.0 built from source.
Thanks for any help.