timers_handle

By now, you should have noticed that in every Tick function there is a float parameter named DeltaTime. This parameter is equal to the seconds it took in order for the last frame to be completed. In this tutorial, I’m going to create a function which fires every X seconds using Timers

Creating a Timer

In UE4 we can set custom timers for specific events to fire. Say that you want a function named DoAPeriodicCheck to run every 10 seconds to happen, well that’s easy!

First of all, create a class which inherits from Actor (I named it TimerActor) and add the following property with the corresponding macro:

UPROPERTY(EditAnywhere)
float LoopTime;

Moreover, we need a function that will execute every time that our custom timer will tick. I created the following function:

void DoAPeriodicCheck();

My header file is included in the following image:

timers_handle

Switch to your source file and type in the following function (this is the function that is going to be executed every LoopTime seconds):

Ok, now all we need to do is to tell the Actor in the BeginPlay function we want our timer to start. This can be done with the following code:

That’s all, now compile your code, create a Blueprint based on the class we created above and place it in your level. Then, set a value to the LoopTime, press play and wait for the DoAPeriodicCheck() to fire. Your output should be similar to the image below:

timers_output

Share postShare on Facebook0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0