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

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

源码分析

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

为了实现UE4中的延迟功能,可以使用UnLua中的定时器实现。在UnLua中,可以使用定时器方法`Delay`来实现延迟执行某个函数的功能。通过调用`Delay`方法,并传入延迟时间和要执行的函数,即可实现延迟功能。具体步骤如下: 1. 在需要使用延迟功能的地方,调用`Delay`方法,并传入延迟时间和要执行的函数。例如: ```lua Delay(2, function() -- 在这里编写需要延迟执行的代码 print("Delayed execution") end) ``` 这段代码会在延迟2秒后执行函数内的代码。 2. 使用UnLua的定时器功能,可以在UnLua中创建一个定时器,通过调用`UnLua_AddTimer`方法来实现定时器的创建和管理。例如: ```lua local TimerHandle = UnLua_AddTimer(1, false, function() -- 在这里编写定时器需要执行的代码 print("Timer execution") end) ``` 这段代码会创建一个定时器,每隔1秒执行一次函数内的代码。 通过以上步骤,可以使用UnLua的定时器功能来实现UE4中的延迟和定时器功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [UE4中UnLua基础](https://blog.csdn.net/qq_37340753/article/details/121872880)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值