Using Timers

A How To Guide for Using Timers for Gameplay in Unreal Engine 4.

Choose your operating system:

Windows

macOS

Linux

Timers in Unreal Engine 4 are a useful tool that can be used to perform an action once (or repeatedly) at specified intervals. Timers can be used to set actions to happen on a delay or over a period of time. For example, you may want to make the player invincible after obtaining a certain item and then revert after 10 seconds. Or, you may want to apply damage every second to a player that is standing in fire. Each of these scenarios can be accomplished with the use of Timers.

Implementation Guide

This guide covers how to use Timers for gameplay purposes in Blueprints and the nodes associated with Timers.

Creating and Clearing Timers

The steps below will show you how you can create/set a Timer in Blueprints.

For this example, we are using the Blueprint Third Person Template and will be causing fire damage to the player over time.

  1. Inside your project, open the ThirdPersonCharacter Blueprint.

  2. Right-click in the graph and add two Custom Events called PlayerInFire and PlayerNotInFire .

    GHT6B_1.png

  3. In the MyBlueprint window, add a new Function called FireDamage .

    GHT6B_2.png

    This Function will be called over time by the Timer and will apply the damage.

  4. Drag off the PlayerInFire Event and add the Set Timer by Function Name node.

  5. Drag off the PlayerNotInFire Event and add the Clear Timer by Handle node.

  6. For Set Timer by Function Name , set the Function Name to FireDamage .

  7. On the Set Timer , set the Time to 0.75 and check the Looping box.

  8. Connect the Return Value pin of the Set Timer by Function Name node to the Handle pin of the Clear Timer by Handle node.

    GHT6B_3.png

    The Time entered is the interval in which the Function Name Function will be called (every 0.75 seconds).

  9. Create a new Integer variable called PlayerHealth , click Compile , then set the Default Value to 100 .

    GHT6B_4.png

  10. Inside the FireDamage Function, while holding down the Alt key, drag the PlayerHealth variable into the graph.

  11. Hold down Control and drag in the PlayerHealth variable as well.

  12. Drag off the PlayerHealth variable and add an Integer - Integer node, then off the 0 pin, add a Random Integer in Range node.

    GHT6B_5.png

    We used a Random Integer in Range node with Min (1) and Max (4) values, however you can enter a damage value if you wish.

  13. Off the Set node, add a Print String then Control drag in PlayerHealth and connect it to the Print String .

    GHT6B_6.png

    We are not displaying Health Bars in this example; however this will show us that the Health Value is indeed decreasing.

  14. Open the Blueprint_Effect_Fire Blueprint located in the Content/StarterContent/Blueprints folder.

  15. Click the Add Component button and add a Box shape and call it Trigger .

    GHT6B_7.png

    When the player enters/exits this Trigger, we will call the Events in the ThirdPersonCharacter Blueprint.

  16. Locate the My Blueprint tab and Right-click on the Trigger and add the OnComponentBegin and OnComponentEnd Overlap Events.

    GHT6B_8.png

  17. Drag off the Other Actor pin of each and add the Cast To ThirdPersonCharacter node.

  18. Drag off the As Third Person Character C pin and (for the Begin node) add PlayerInFire and (for the End node) PlayerIsNotInFire Events.

    GHT6B_8b.png

  19. Compile and Save , then close the Blueprint.

  20. In the Content Browser , drag the Blueprint_Effect_Fire Blueprint into the level.

    GHT6B_9.png

  21. When you click the Play button to play in the editor, enter the fire and you should see you start to take damage.

This is an example of how you could use timers to create a damage-over-time effect; however you could use timers to represent other things such as a power-up that lasts for X seconds which allows the player to be invincible or have some other ability.

Additional Timer Related Nodes

While the Set Timer by Function Name and Clear Timer by Handle nodes are used to start/stop a Timer, the nodes below may also be of use based on your needs.

Get Timer Elapsed and Get Timer Remaining

GHT6B_10.png

The Get Timer Elapsed by Handle and Get Timer Remaining by Handle nodes will get the amount of time that has elapsed since starting or get the amount of time remaining for the specified Timer (indicated by the Handle). It returns the value as a Float and can be used for several different purposes, for example, changing an effect over time or providing a message when an effect is about to expire.

Pause Timer and Un Pause Timer

GHT6B_11.png

As the names suggest, the Pause Timer by Handle pauses the timer at its current time whereas the Clear Timer by Handle stops it entirely and resets it. The Unpause Timer by Handle node can be used to resume a paused timer and pick up where it left off. One example use case would be if a player has an effect applied to them that runs on a timer and they enter a menu or inventory screen for example and you want to pause the effect and resume it when they return to gameplay.

Timer Exists, Is Timer Active and Is Timer Paused

GHT6B_12.png

The Does Timer Exist by Handle , Is Timer Active by Handle , and Is Timer Paused by Handle nodes all return a True or False value and can be used to determine if a Timer exists, if a Timer is currently active or if a Timer is currently paused respectively.

Set Timer by Event

GHT6B_13.png

The Set Timer by Event node functions similar to the Set Timer by Function Name node except instead of specifying a Function Name, you can connect a Custom Event which will be fired. You can set this to looping as well, just as you could with the Set Timer by Function Name node.

Help shape the future of Unreal Engine documentation! Tell us how we're doing so we can serve you better.
Take our survey
Dismiss