UnrealSharp
  • Home
  • FAQ
  • Getting Started
    • Setup
    • Your First Script
    • Debugging
    • Packaging
  • Documentation
    • Classes
      • Properties
        • C++ Functions As C# Properties
        • C++ Properties with Getters/Setters
      • Functions
        • Flags
        • MetaData
      • Default Actor Components
      • Helper Methods
    • Structs
    • Enums
    • Interfaces
    • Delegates
    • Collections
      • TArray
      • TNativeArray
      • TSet
      • TMap
    • Multiplayer
      • Replicated Properties
      • RPCs (Remote Procedure Calls)
      • Replicated UObjects
    • Primary Data Assets
      • Loading Primary Data Assets
    • Loading Soft References
    • Trace Channels
    • Static Variables
      • FWorldStaticVar<T>
      • FGameStaticVar<T>
    • Logging
    • Async
    • Gameplay Tags
      • Gameplay Tag Container
    • Extension / Mixin Methods
    • Subsystems
    • Module Lifecycle
  • Links
    • Github Repository
    • Roadmap
    • Discord
Powered by GitBook
On this page
  • Delegate Declaration
  • Class Member Declaration
  • Delegate API Example
  1. Documentation

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 MyShowcaseMulticastDelegate(int a);

// Single delegates.
[USingleDelegate]
public delegate void MyShowcaseDelegate(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 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]
    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.

protected override void BeginPlay()
{
    // Subscribe with a lambda. Must have a UFunction attribute
    MyMulticastDelegate += [UFunction](int a) =>
    {
        PrintString($"MyCallback invoked with {a}");
    };

    // Subscribe to the delegate with a callback
    MyMulticastDelegate += MyFunctionCallback;
    
    // Invoke the delegate
    MyMulticastDelegate.Invoke(1337);

    // Check if the delegate contains the callback
    if (MyMulticastDelegate.Contains(MyCallback))
    {
        PrintString("Delegate contains MyCallback");
    }
    
    // Unsubscribe from the delegate
    MyMulticastDelegate -= MyCallback;

    base.BeginPlay();
}

// A callback to an Unreal Engine exposed delegate must be a UFunction.
[UFunction]
public void MyFunctionCallback(int a)
{
    PrintString($"MyCallback invoked with {a}");
}
PreviousInterfacesNextCollections

Last updated 5 months ago