TimerDelegate with Const& Parameters fails to compile

My code looks like this:

void AARPGPlayerController::PreClientTravel(const FString & PendingURL, ETravelType TravelType, bool bIsSeamlessTravel)
{

	const FTimerDelegate timerDelegate = FTimerDelegate::CreateUObject(this, &Super::PreClientTravel, PendingURL, TravelType, bIsSeamlessTravel);
	FTimerHandle timerHandle;
	PlayerCameraManager->StartCameraFade(0, 1, 1, FLinearColor(0, 0, 0), false, true);
	GetWorldTimerManager().SetTimer(timerHandle,timerDelegate,1.0f,false);
}

and Unreal refuses to compile it.
The corresponding Compile Error looks like this:

Which looks like it has problems with the const FString & parameter

Hi,

You are correct. This is a limitation of the delegate API when passing payloads.

Because you can’t change the signature of the function to pass PendingURL by pointer or by value, you could try something like this instead:

TWeakObjectPtr<AARPGPlayerController> WeakThis = this;
const FTimerDelegate timerDelegate = FTimerDelegate::CreateLambda([=]()
{
    if (Super* This = WeakThis)
    {
        This->PreClientTravel(PendingURL, TravelType, bIsSeamlessTravel);
    }
});

This is basically what the CreateUObject function does internally, but without the API issues. The weak pointer emulates how the delegate normally stores the object pointer, by preventing the delegate from firing when the object has expired. You may not need this functionality if you can guarantee the timer will not outlive the object, in which case it can be slightly simpler.

Hope this helps,

Steve