先说明一下,本文并非探究三者底层实现上的异同,只是从源码角度来看三者执行效果的异同
源码分析
话不多说,先上源码
Delay函数
void UKismetSystemLibrary::Delay(const UObject* WorldContextObject, float Duration, FLatentActionInfo LatentInfo )
{
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
{
FLatentActionManager& LatentActionManager = World->GetLatentActionManager();
if (LatentActionManager.FindExistingAction<FDelayAction>(LatentInfo.CallbackTarget, LatentInfo.UUID) == NULL)
{
LatentActionManager.AddNewAction(LatentInfo.CallbackTarget, LatentInfo.UUID, new FDelayAction(Duration, LatentInfo));
}
}
}
RetriggerableDelay函数
void UKismetSystemLibrary::RetriggerableDelay(const UObject* WorldContextObject, float Duration, FLatentActionInfo LatentInfo)
{
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
{
FLatentActionManager& LatentActionManager = World->GetLatentActionManager();
FDelayAction* Action = LatentActionManager.FindExistingAction<FDelayAction>(LatentInfo.CallbackTarget, LatentInfo.UUID);
if (Action == NULL)
{
LatentActionManager.AddNewAction(LatentInfo.CallbackTarget, LatentInfo.UUID, new FDelayAction(Duration, LatentInfo));
}
else
{
// Reset the existing delay to the new duration
Action->TimeRemaining = Duration;
}
}
}
对比可以发现,函数Delay中,在未找到已存在的相同Delay Action的情况下才会向LatentActionManager中添加一个新Action,如果找到了就什么都不做;而RetriggerableDelay函数中,在找到已存在的相同Delay Action的时候会重置Delay Action时间,未找到则和Delay函数一样设置新Action
再来看SetTimer() ,由于SetTimer()函数过长,只放部分关键代码
void FTimerManager::InternalSetTimer(FTimerHandle& InOutHandle, FTimerUnifiedDelegate&& InDelegate, float InRate, bool InbLoop, float InFirstDelay)
{
SCOPE_CYCLE_COUNTER(STAT_SetTimer);
// not currently threadsafe
check(IsInGameThread());
if (FindTimer(InOutHandle))
{
// if the timer is already set, just clear it and we'll re-add it, since
// there's no data to maintain.
InternalClearTimer(InOutHandle);
}
......
}
可以看到在SetTimer函数中上来就是先检查是否可以根据传入的TimerHandle找到对应的Timer,如果找到了则清楚这个Timer,故使用SetTimer的效果是和RetriggerableDelay函数的效果一致,即每次设置,重置计时
省流总结
SetTimer和RetriggerableDelay函数的执行效果一致,都是每次设置,重置计时,而Delay函数则是被设置时,只有相同Delay已计时结束才会再设置