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);
}
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