Keywords: UE4, Lambda Notes

Origin: 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
    //Typing the logic of our function here just like any other function...
    GLog->Log("Destroying Actor now...");
});//Don't forget the ";" in the end of your parenthesis!
GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDelegate, 5.f, false);

Exampe 3:

void ALambdaActor::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());
            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!


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