Created
February 11, 2024 21:38
-
-
Save kirby561/06e829b2fd8067659dc395fcb88b1306 to your computer and use it in GitHub Desktop.
Gist for a Youtube video "How to declare and use Delegates in Unreal Engine 5"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "TestClasses.h" | |
DEFINE_LOG_CATEGORY_STATIC(TestClassSub, Log, All); | |
void UTestClass::InitDelegates() { | |
_someDelegate1.BindStatic(&UTestClass::SomeStaticDelegateFunction); | |
int hey = 5; | |
_someDelegate2.BindLambda([hey](FString param) { | |
UE_LOG(TestClassSub, Warning, TEXT("Lambda: %s, %d"), *param, hey); | |
}); | |
_someDelegate3.BindWeakLambda(this, [](FString param) { | |
UE_LOG(TestClassSub, Warning, TEXT("Lambda: %s"), *param); | |
}); | |
SomeRawClass* someRawClass = new SomeRawClass(); | |
_someDelegate4.BindRaw(someRawClass, &SomeRawClass::SomeRawMethod); | |
_someSharedPtr = TSharedPtr<SomeRawClass>(new SomeRawClass()); | |
_someDelegate5.BindSP(_someSharedPtr.ToSharedRef(), &SomeRawClass::SomeRawMethod); | |
_someThreadSafeSharedPtr = TSharedPtr<SomeRawClass, ESPMode::ThreadSafe>(new SomeRawClass()); | |
_someDelegate6.BindThreadSafeSP(_someThreadSafeSharedPtr.ToSharedRef(), &SomeRawClass::SomeRawMethod); | |
_someDelegate7.BindUFunction(this, TEXT("SomeDelegateUFunction")); | |
_someDelegate8.BindUObject(this, &UTestClass::SomeDelegateUFunction); | |
_someReturnValDelegate.BindLambda([](FString param) -> bool { | |
UE_LOG(TestClassSub, Warning, TEXT("Lambda: %s"), *param); | |
return true; | |
}); | |
/* | |
AddStatic | |
AddLambda | |
AddWeakLambda | |
AddRaw | |
AddSP | |
AddThreadSafeSP | |
AddUFunction | |
AddUObject | |
*/ | |
_someMulticastDelegate.AddLambda([hey](FString param) { | |
UE_LOG(TestClassSub, Warning, TEXT("First Lambda: %s"), *param); | |
}); | |
_someMulticastDelegate.AddLambda([hey](FString param) { | |
UE_LOG(TestClassSub, Warning, TEXT("Second Lambda: %s"), *param); | |
}); | |
FDelegateHandle handle = _someMulticastDelegate.AddLambda([hey](FString param) { | |
UE_LOG(TestClassSub, Warning, TEXT("Not going to printed"), *param); | |
}); | |
_someMulticastDelegate.Remove(handle); | |
_someDynamicDelegate.BindDynamic(this, &UTestClass::SomeDelegateUFunction); | |
_someDynamicMulticastDelegate.AddDynamic(this, &UTestClass::SomeDelegateUFunction); | |
_someDynamicMulticastDelegate.RemoveDynamic(this, &UTestClass::SomeDelegateUFunction); | |
_someDynamicMulticastDelegate.AddUniqueDynamic(this, &UTestClass::SomeDelegateUFunction); | |
} | |
void UTestClass::ExecuteDelegates() { | |
_someDelegate1.Execute(TEXT("BindStatic")); | |
_someDelegate2.Execute(TEXT("BindLambda")); | |
_someDelegate3.Execute(TEXT("BindWeakLambda")); | |
_someDelegate4.Execute(TEXT("BindRaw")); | |
_someDelegate5.Execute(TEXT("BindSP")); | |
_someDelegate6.Execute(TEXT("BindThreadSafeSP")); | |
_someDelegate7.Execute(TEXT("BindUFunction")); | |
_someDelegate8.Execute(TEXT("BindUObject")); | |
bool returnVal = _someReturnValDelegate.Execute("ReturnValLambda"); | |
UE_LOG(TestClassSub, Warning, TEXT("Return val from lambda = %d"), returnVal); | |
_someMulticastDelegate.Broadcast(TEXT("MulticastDelegate")); | |
_someDynamicDelegate.Execute(TEXT("DynamicDelegate")); | |
_someDynamicMulticastDelegate.Broadcast(TEXT("DynamicMulticast")); | |
} | |
void UTestClass::SomeStaticDelegateFunction(FString param) { | |
UE_LOG(TestClassSub, Warning, TEXT("SomeStaticDelegateFunction: %s"), *param); | |
} | |
void UTestClass::SomeDelegateUFunction(FString param) { | |
UE_LOG(TestClassSub, Warning, TEXT("SomeDelegateUFunction: %s"), *param); | |
} | |
void SomeRawClass::SomeRawMethod(FString param) { | |
UE_LOG(TestClassSub, Warning, TEXT("SomeRawMethod: %s"), *param); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "CoreMinimal.h" | |
#include "Delegates/Delegate.h" | |
#include "TestClasses.generated.h" | |
DECLARE_DELEGATE_OneParam(FSomeDelegate, FString); | |
DECLARE_DELEGATE_RetVal_OneParam(bool, FSomeReturnValDelegate, FString); | |
DECLARE_MULTICAST_DELEGATE_OneParam(FSomeMulticastDelegate, FString); | |
DECLARE_DYNAMIC_DELEGATE_OneParam(FSomeDynamicDelegate, FString, param); | |
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSomeDynamicMulticastDelegate, FString, param); | |
UCLASS() | |
class UTestClass : public UObject { | |
GENERATED_BODY() | |
public: | |
void InitDelegates(); | |
void ExecuteDelegates(); | |
static void SomeStaticDelegateFunction(FString param); | |
UFUNCTION() | |
void SomeDelegateUFunction(FString param); | |
private: | |
FSomeDelegate _someDelegate1; | |
FSomeDelegate _someDelegate2; | |
FSomeDelegate _someDelegate3; | |
FSomeDelegate _someDelegate4; | |
FSomeDelegate _someDelegate5; | |
FSomeDelegate _someDelegate6; | |
FSomeDelegate _someDelegate7; | |
FSomeDelegate _someDelegate8; | |
FSomeReturnValDelegate _someReturnValDelegate; | |
TSharedPtr<class SomeRawClass> _someSharedPtr; | |
TSharedPtr<class SomeRawClass, ESPMode::ThreadSafe> _someThreadSafeSharedPtr; | |
FSomeMulticastDelegate _someMulticastDelegate; | |
FSomeDynamicDelegate _someDynamicDelegate; | |
FSomeDynamicMulticastDelegate _someDynamicMulticastDelegate; | |
}; | |
class SomeRawClass { | |
public: | |
void SomeRawMethod(FString param); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment