UE5 C++ Timer的使用

1.创建一个FTimerHandle来管理计时器,一个计时器一个handle

FTimerHandle MyTimerHandle;

2.定义一个函数,用于计时器结束之后触发:

 UFUNCTION()
 void OnNext();

3.设置计时器:

GetWorld()->GetTimerManager().SetTimer(MyTimerHandle, this, &MyActor::OnExplode, Time, false);

3.清除计时器:

//清除一个
GetWorld()->GetTimerManager().ClearTimer(MyTimerHandle);
///清除所有
GetWorld()->GetTimerManager().ClearAllTimersForObject(this);

4.SetTimer函数参数定义:

/**
	 * Sets a timer to call the given native function at a set interval.  If a timer is already set
	 * for this handle, it will replace the current timer.
	 *
	 * @param InOutHandle			If the passed-in handle refers to an existing timer, it will be cleared before the new timer is added. A new handle to the new timer is returned in either case.
	 * @param InObj					Object to call the timer function on.
	 * @param InTimerMethod			Method to call when timer fires.
	 * @param InRate				The amount of time (in seconds) between set and firing.  If <= 0.f, clears existing timers.
	 * @param InbLoop				true to keep firing at Rate intervals, false to fire only once.
	 * @param InFirstDelay			The time (in seconds) for the first iteration of a looping timer. If < 0.f InRate will be used.
	 */
	template< class UserClass >
	FORCEINLINE void SetTimer(FTimerHandle& InOutHandle, UserClass* InObj, typename FTimerDelegate::TMethodPtr< UserClass > InTimerMethod, float InRate, bool InbLoop = false, float InFirstDelay = -1.f)
	{
		InternalSetTimer(InOutHandle, FTimerUnifiedDelegate( FTimerDelegate::CreateUObject(InObj, InTimerMethod) ), InRate, InbLoop, InFirstDelay);
	}