Timers

Scene Timers

You can use conditions and actions to launch scene timers. Then you can run actions when a timer reaches a certain value. The most useful timers are seen when used in a condition to compare the time elapsed in a timer and the action to reset a timer to 0. It is important to remember to always reset a timer prior to using it. If the timer doesn't exist, it will be created automatically in memory.

You can give any name to your timers. Timer names are text/string expressions, so they must be entered between quotes!

Here is a more advanced Timer example. The timer is used to briefly protect the player from damage. The amount of time given to protect the player depends on the value of variable “player_undestroyable_time

See it in action! 🎮
Open this example online: https://editor.gdevelop-app.com?project=example://asteroids

Object timers

In addition to scene timers, you can use timers on your game object and their instances. This means that each instance of the object will hold its own timer in memory. It will be independent from other object instances. This feature lets you launch object instances actions after every X number of seconds.

Object timers are set up in the same way as scene timers. You have to initialize an object instance timer with the action to Start/reset a timer:

Note that as with scene timers, object timers names are string/text expressions. They must be typed between quotes!. The above action will create a timer called “color” on each instance of the Sprite object, and start these timers.

Now you can check the timer's values:

The condition above checks to see if the time elapsed in the “color” timer is greater than an object variable (you can use an object variable in an expression to get its value). The condition will pick every Sprite instance with a timer value greater than the time they “request” with the object variable “color_time”. Then the Sprite color is changed to a random one and the object timer is reset.

Using object variables to check object timers is useful. For example, you can have several instances of enemies fire bullets with different firing rates. Another use of object timers can be seen when used with an enemy that collides with a “fire rate power-up”. You can set the variable “fire_rate_time” to be less than the normal value, meaning that the time between each bullet will decrease.

See it in action! 🎮
Open this example online: https://editor.gdevelop-app.com?project=example://objects-timers

Advanced: simulate timers with variables

Scene and Object Timers are convenient. However, sometimes you can need to simulate timers with variables. Complex tasks require it. This allows you to have more control over the timers. Using variables, you can increase or decrease the value by adding or subtracting time from the variable. If you want all the instances of your object on a scene to have their own custom timer, use a variable along with TimeDelta expression.

This use case will increase the variable at a speed of 1000 units per seconds. When using 1000 units per second, the “timer” runs in milliseconds.

For example, as seen below, you can compare a variable (Damage Timer) to set an action after 0.5 seconds. Then the “timer can be reset.