Here’s a tiny header-only C++ library that provides a class for executing lambdas with a frame delay in Unreal Engine. You can get it on
Github.
It’s experimental, so use at your own risk. ;D
I dug up the impressive C++ template witchery for converting lambdas to std::function from a
Stackoverflow answer here.
FrameTimer lets you do stuff like this:
// This function will execute after 60 frames have elapsed, printing a debug message
FrameTimer.Create(60, []()
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Cyan, TEXT("60 frame delay!"));
});
// This function will execute after 100 frames have elapsed,
// and will check to make sure that the captured pointers still point to valid UObjects
FrameTimer.Create(100, [=]()
{
auto SafeThis = FrameTimer.MakeWeakPtr(this);
auto SafeOtherObjectPtr = FrameTimer.MakeWeakPtr(OtherObjectPtr);
if (SafeThis.IsValid() && SafeOtherObjectPtr.IsValid())
{
// Do something with SafeThis and SafeOtherObjectPtr
}
});