FTimerManager::SetTimer

Sets a timer to call the given native function at a set interval.

Windows
MacOS
Linux

References

Module

Engine

Header

/Engine/Source/Runtime/Engine/Public/TimerManager.h

Include

#include "TimerManager.h"

Syntax

template<class UserClass>
void SetTimer
(
    FTimerHandle & InOutHandle,
    UserClass * InObj,
    typename FTimerDelegate::TUObjectMethodDelegate< UserClass >::FMethodPtr InTimerMethod,
    float InRate,
    bool InbLoop,
    float InFirstDelay
)

Remarks

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.

Parameters

Parameter

Description

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.

InObj

Object to call the timer function on.

InTimerMethod

Method to call when timer fires.

InRate

The amount of time (in seconds) between set and firing. If <= 0.f, clears existing timers.

InbLoop

true to keep firing at Rate intervals, false to fire only once.

InFirstDelay

The time (in seconds) for the first iteration of a looping timer. If < 0.f InRate will be used.

Example Code

MyCharacter.cpp at line 97:

void AMyCharacter::ToggleTimer()  
{  
    UWorld* World = GetWorld();  
    if( World )  
    {  
        // If the timer has expired or does not exist, start it  
        if( ( SampleTimerHandle.IsValid() == false ) || ( bTimerExpired) )  
        {  
            World->GetTimerManager().SetTimer(SampleTimerHandle, this, &AMyCharacter::SampleTimerExpired, 10.0f);  
            bTimerExpired = false;  
        }     
        else  
        {         
            if( World->GetTimerManager().IsTimerPaused(SampleTimerHandle) == true )  
            {  
                World->GetTimerManager().UnPauseTimer(SampleTimerHandle);  
            }  
            else  
            {  
                World->GetTimerManager().PauseTimer(SampleTimerHandle);  
            }  
        }  

    }  
}  

void AMyCharacter::SampleTimerExpired()  
{  
    bTimerExpired = true;  
}  

MyCharacter.cpp at line 439:

        if (SampleTimerHandle.IsValid() == true)  
        {  
            UWorld* World = GetWorld();  
            float TimerTime = 0.0f;  
            if (World != nullptr)  
            {  
                if (bTimerExpired == true)  
                {  
                    TheHud->TimerText = FString::Printf(TEXT("Timer Has Expired"));  
                    TheHud->TimerTextColor = FColor::Red;  
                }  
                else  
                {  
                    if (World->GetTimerManager().IsTimerPaused(SampleTimerHandle) == true)  
                    {  
                        TheHud->TimerText = FString::Printf(TEXT("Current Time is %f - Timer Is Paused"), World->GetTimerManager().GetTimerRemaining(SampleTimerHandle));  
                        TheHud->TimerTextColor = FColor::Yellow;  
                    }  
                    else  
                    {  
                        TheHud->TimerText = FString::Printf(TEXT("Current Time is %f"), World->GetTimerManager().GetTimerRemaining(SampleTimerHandle));  
                        TheHud->TimerTextColor = FColor::Blue;  
                    }  
                }  
            }  
        }  
        else  
        {  
            TheHud->TimerText = TEXT("Timer Not Started");  
            TheHud->TimerTextColor = FColor::Blue;  
        }
Select Skin
Light
Dark

Welcome to the new Unreal Engine 4 Documentation site!

We're working on lots of new features including a feedback system so you can tell us how we are doing. It's not quite ready for use in the wild yet, so head over to the Documentation Feedback forum to tell us about this page or call out any issues you are encountering in the meantime.

We'll be sure to let you know when the new system is up and running.

Post Feedback