[UE4][C++][Timer、Delay、RetriggerableDelay的区别]

文章通过源码分析了Delay、RetriggerableDelay和SetTimer三个函数在执行延迟操作时的差异。Delay函数在找不到已存在的延迟动作时才新建,而RetriggerableDelay和SetTimer在找到已存在的延迟或定时器时会重置时间,实现重触发功能。因此,SetTimer和RetriggerableDelay的行为一致,每次设置都会重置计时,而Delay则不同。

先说明一下,本文并非探究三者底层实现上的异同,只是从源码角度来看三者执行效果的异同

源码分析

话不多说,先上源码
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已计时结束才会再设置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值