Whether the event stays bound?

Hi all, I’m wondering if I need OnNotificationStopDisplay.RemoveDynamic(...) or or it will remove the binding itself. Here’s the Code:

void UHUDWidgetBase::TryToInvokeNotification()
{
	if(CurrentNotification) return;
	if(WidgetQueue.Num() == 0) return;
	
	CurrentNotification = WidgetQueue[0];
	CurrentNotification->NotificationDisplay();
	WidgetQueue.RemoveAt(0);

	CurrentNotification->OnNotificationStopDisplay.AddDynamic(this, &UHUDWidgetBase::TryToInvokeNotification);
	CurrentNotification = nullptr;
}

I’m confused at this point:

CurrentNotification->OnNotificationStopDisplay.AddDynamic(this, &UHUDWidgetBase::TryToInvokeNotification);
	CurrentNotification = nullptr;

Please Help :pray:

Yes, you will need to remove the OnNotificationStopDisplay binding, assuming it’s possible for the notifications to ever call that multiple times.

It may be best to add a dedicated callback function that OnNotificationStopDisplay binds to instead of just binding directly to TryToInvokeNotification. For that dedicated callback function, the notification could pass a reference to self. There you could remove the binding and then call TryToInvokeNotification().

That’s the type of reason largely every unreal C++ delegate passes a reference to self.

Thank you for your answer and explanation.
My immediate thought was that it couldn’t just become unlinked, so I reworked the function a bit:

void UHUDWidgetBase::TryToInvokeNotification()
{
	if(CurrentNotification)
	{
		CurrentNotification->OnNotificationStopDisplay.RemoveDynamic(this, &UHUDWidgetBase::TryToInvokeNotification);
		CurrentNotification = nullptr;
	}

	if(WidgetQueue.Num() == 0) return;

	CurrentNotification = WidgetQueue[0];
	CurrentNotification->NotificationDisplay();
	WidgetQueue.RemoveAt(0);

	CurrentNotification->OnNotificationStopDisplay.AddDynamic(this, &UHUDWidgetBase::TryToInvokeNotification);
}

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.