Keywords: UE4, Lambda Notes

Origin: Understanding Lambda Expressions
https://www.orfeasel.com/understanding-lambda-expressions/

Exampe 1:

FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, this, &AActor::Destroy(), 5.f);

Exampe 2:

FTimerHandle TimerHandle;
FTimerDelegate TimerDelegate;
 
//Binding our Lambda expression
TimerDelegate.BindLambda([&]()
{
    //Typing the logic of our function here just like any other function...
    GLog->Log("Destroying Actor now...");
    Destroy();
});//Don't forget the ";" in the end of your parenthesis!
 
GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDelegate, 5.f, false);

Exampe 3:

void ALambdaActor::BeginPlay()
{
    Super::BeginPlay();
 
    //Declaring an Array of Actors
    TArray<AActor*> ActorsArray;
 
    //Declaring a delegate with one int32 parameter
    DECLARE_DELEGATE_OneParam(MyUsefulDelegate, int32);
 
    //The following functions populates the ActorsArray with all the Actors which reside inside our current level
    UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), ActorsArray);
 
    //Declaring a MyUsefulDelegate
    MyUsefulDelegate Del;
   
    //Binding a Lambda to it
    //In this lambda we pass the ActorsArray by value since we won't make any changes
    //or want any changes reflected outside the lambda expression
    //If we don't pass the ActorsArray in the capturelist then we won't be able to have access to it!
    Del.BindLambda([ActorsArray](int32 ActorIndex)
    {
        //Print the name of the Actor which has an index equal to the one we provided (ActorIndex)
        //Make sure we provided a valid index for our array
        if (ActorsArray.IsValidIndex(ActorIndex))
            GLog->Log("Actor with given index:" + ActorsArray[ActorIndex]->GetName());
        else
            GLog->Log("You've entered an invalid index. That's unfortunate :(");
    });
 
    //Show me the 16th Actor of the Array - Don't forget that TArray is zero-based!
    Del.ExecuteIfBound(15);
}
Weak Lambda

TBaseDelegate:

auto TestFun = TBaseDelegate<int32, int32>::CreateWeakLambda(this, [](int32 Value){ return 111 + Value; });

FTimerDelegate:

auto Callback = FTimerDelegate::CreateWeakLambda(this, [](int32 Value) { return 111 + Value; });

BindWeakLambda:

FOnExternalUIChangeDelegate OnExternalUIChangeDelegate;
OnExternalUIChangeDelegate.BindWeakLambda(this, [Delegate](bool bInIsOpening)
{
    Delegate.ExecuteIfBound(bInIsOpening);
});,
TArray::FindByPredicate()
 // Find first odd number
 if (const int32* Val = MyArrayOfInts.FindByPredicate([](int32 n){ return n % 1; }))
 {
     // Val points to odd number
 }

https://answers.unrealengine.com/questions/58463/are-the-examples-of-using-container-find-with-a-pr.html

TArray::Sort()
TArray<FMyStruct*> TestArray;
TestArray.Sort([](const FMyStruct& a, const FMyStruct& b) { return a.field < b.field; });

https://www.reddit.com/r/unrealengine/comments/d6fjii/how_does_one_sort_a_tarray_in_descending_order/

Run Lambda on GameThread and AnyThread

Long Running Async Lambdas, lock-free (UE4)
https://gist.github.com/getnamo/c9ca2095d449381bdeaa

Reference

UE4中Lambda的一些用法
https://blog.csdn.net/xoyojank/article/details/52859518


When we are tired, we are attacked by ideas we conquered long ago. ― Friedrich Nietzsche