What are the purpose of delegate handles?

I’m more or less familiar with the basics of delegates in UE. However I’ve been working on a system to create and join servers for a multiplayer project, and I’ve come across something I haven’t encountered before - delegate handles! Such as in this guide on creating multiplayer sessions.

I’ve already gotten a system up and running following another tutorial not using these separate delegate handles, so they’re clearly not strictly necessary. But I’ve scoured and scoured the internet, and I haven’t been able to find a single resource anywhere explaining what the purpose of a delegate handle is, or when or why we’d want to use them vs the way I’ve been using delegates up to this point. I was hoping someone could either explain it or else link to me somewhere that does.

Thanks!

1 Like

They are simply ID’s for a specific binding. They can be used to remove bindings by their unique ID instead of by object/function name etc. Really it’s just a wrapper for an “int” counter belonging to a given delegate instance.

Note that Delegate Handles only exist on non-dynamic delegates and cannot be serialized.

Here’s an example use of them:

Note that when you “remove” a delegate, the handle you pass in is not invalidated. If you want to reuse a handle after removal, you must reset it yourself.

1 Like

Thanks for the reply!

So I think I basically get the gist of what they’re doing in your example, but I’m confused about something. It’s looks like in your example, you’re using the delegate handle as the delegate itself. Such as in this bit of code:

PostInitWorldDelegateHandle = FCoreUObjectDelegates::PostLoadMapWithWorld.AddUObject(this, &UST_WorldSubsystem::PostInitWorldInternal);

Whereas in the other example I was looking at, a delegate was set:

OnCreateSessionCompleteDelegate = FOnCreateSessionCompleteDelegate::CreateUObject(this, &UNWGameInstance::OnCreateSessionComplete)

And then a separate handle created which references the delegate (“Sessions” here being a reference to the IOnlineSessions interface)

OnCreateSessionCompleteDelegateHandle = Sessions->AddOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegate);

A nearly identical example can be found in the UE4 Shooter Game example from Epic themselves (although this is from 2017, so perhaps something has changed?)

I suppose what I’m not understanding is what exactly the use of handles in the latter two examples is even doing :thinking: It seems like they’re just being set and then cleared when the function completes without ever being used to do anything, meaning we could remove them and it wouldn’t have any affect on the functionality at all. Are they just there as a reference in case we want to use them for something else later?

EDIT: So I tried actually commenting out all the handles (but leaving the delegates themselves alone) in my project. It compiled and launched fine, but when I tried to create a server nothing happened. It works fine when they’re not commented out. Guess that means they’re doing something under the hood. I’ll have to reference the backup of the version I had set up previously that didn’t use handles at all to figure out what I changed.