Advertisement
orfeasel

Lambda with parameter

Feb 10th, 2016
25,278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. void ALambdaActor::BeginPlay()
  2. {
  3.     Super::BeginPlay();
  4.  
  5.     //Declaring an Array of Actors
  6.     TArray<AActor*> ActorsArray;
  7.  
  8.     //Declaring a delegate with one int32 parameter
  9.     DECLARE_DELEGATE_OneParam(MyUsefulDelegate, int32);
  10.  
  11.     //The following functions populates the ActorsArray with all the Actors which reside inside our current level
  12.     UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), ActorsArray);
  13.  
  14.     //Declaring a MyUsefulDelegate
  15.     MyUsefulDelegate Del;
  16.    
  17.     //Binding a Lambda to it
  18.     //In this lambda we pass the ActorsArray by value since we won't make any changes
  19.     //or want any changes reflected outside the lambda expression
  20.     //If we don't pass the ActorsArray in the capturelist then we won't be able to have access to it!
  21.     Del.BindLambda([ActorsArray](int32 ActorIndex)
  22.     {
  23.         //Print the name of the Actor which has an index equal to the one we provided (ActorIndex)
  24.         //Make sure we provided a valid index for our array
  25.         if (ActorsArray.IsValidIndex(ActorIndex))
  26.             GLog->Log("Actor with given index:" + ActorsArray[ActorIndex]->GetName());
  27.         else
  28.             GLog->Log("You've entered an invalid index. That's unfortunate :(");
  29.     });
  30.  
  31.     //Show me the 16th Actor of the Array - Don't forget that TArray is zero-based!
  32.     Del.ExecuteIfBound(15);
  33.    
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement