Delegates

Exposing Delegates from C# to Unreal

Delegate Declaration

When exposing delegates to Unreal Engine, some additional steps are required compared to standard C#.

Delegates exposed to Unreal Engine needs to have parameters that are supported by reflection.

// Delegates need the attribute to generate the correct glue for the type.

// Multicast delegates.
[UMultiDelegate]
public delegate void FMyShowcaseMulticastDelegate(int a);

// Single delegates.
[USingleDelegate]
public delegate void FMyShowcaseDelegate(int a);

Class Member Declaration

In UnrealSharp, delegates can be exposed as class members in the following ways:

[UClass]
public class ADelegateShowcaseClass : AActor
{
    [UProperty(PropertyFlags.BlueprintAssignable)]
    public partial TMulticastDelegate<MyShowcaseMulticastDelegate> MyMulticastDelegate { get; set; }
    
    [UFunction(FunctionFlags.BlueprintCallable)]
    public void MyFunctionWithCallback(TDelegate<MyShowcaseDelegate> singleDelegate)
    {
        
    }

    // Single delegates as properties can't be Blueprint exposed, but reflection exposed. Unreal Engine limitation.
    [UProperty]
    public partial TDelegate<MyShowcaseDelegate> MySingleDelegate { get; set; }
}

Delegate API Example

Delegates exposed to Unreal Engine follow the same syntax as standard C# delegates, with some additional features.

Last updated