UE4中Lambda的一些用法

时间:2021-07-27 19:05:30

跟Delegate一起用

FTimerDelegate TimerCallback;
TimerCallback.BindLambda([]
{
// callback;
});

FTimerHandle Handle;
GetWorld()->GetTimerManager().SetTimer(Handle, TimerCallback, 5.0f, false);

UI回调事件

 SNew(SButton).OnClicked_Lambda([&]()
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Button Clicked!"));
return FReply::Handled();
})

跨线程执行

 FFunctionGraphTask::CreateAndDispatchWhenReady([=]()
{
// game thread code
}
, TStatId(), nullptr, ENamedThreads::GameThread);

批量执行

void USkeletalMeshComponent::SetAllBodiesBelowSimulatePhysics( const FName& InBoneName, bool bNewSimulate, bool bIncludeSelf )
{
int32 NumBodiesFound = ForEachBodyBelow(InBoneName, bIncludeSelf, /*bSkipCustomPhysicsType=*/ false, [bNewSimulate](FBodyInstance* BI)
{
BI->SetInstanceSimulatePhysics(bNewSimulate);
});
...
}