Timers in your games

Favourite 128 favourites
Tutorial written by ramonesOriginally published on 25th, January 2013 - 2 revisions


Download capx.

Simple Timer

There are many uses for timers in games. Timing a game session, score based on time, countdowns, cooldowns, etc. Creating a timer is pretty simple.


Every tick you add dt to the timer variable. dt is the time since the last tick (see the dt tutorial) so adding dt every tick means timer will equal the total time (in seconds) passed since you started adding. After 1 second the timer variable will equal 1 etc.

Converting seconds into hr/min/sec

To display as hours/minutes/seconds you can convert with the following formulas:
hours = floor(timer / 3600)
minutes = floor(timer / 60 % 60)
seconds = floor(timer % 60)

Countdown Timer

.


For a countdown timer, set the variable to the countdown duration. Then subtract dt every tick as long as the timer > 0. I subtract dt by setting the variable to max(0, variable - dt). This ensures that the value won’t go below 0.

When the value reaches 0 you can start your game or do whatever it is you want to do after the countdown. You may need a ‘System: Trigger Once’ condition alongside the ‘System: countdown = 0’ condition so that your event only happens once when the timer ends.

Gameplay Timer

Timing gameplay works in the same way:


Every x seconds

.
There’s the built-in ‘Every x seconds’ condition that runs every x seconds from the start of the game. But sometimes you want an event to run every x seconds starting when the level starts or when a button is pressed etc.


Same deal as before, subtracting dt from the timer until it reaches 0. This time the timer variable is an instance variable of the turret, as each turret will have it’s own separate timer. When the timer reaches 0, the turret shoots and the timer is reset to the value stored in the turret’s delay variable. Of course you don’t need to create your own timers for everything, sometimes ‘System: Wait x seconds’ or ‘System: Every x seconds’ will do the job just fine.

Timing duration

.


Another way to time the duration of something is to just store the start time and the end time. The total time taken is then endTime - startTime.

Download capx.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Comments

2
leonhard 7,988 rep

Great tutorial, will definitely be useful to someone

Saturday, January 26, 2013 at 1:44:00 PM
4
Hillstrom 4,593 rep

Nice how you give many "real" examples for timers.
One for each situation present in the majority of games.
I'm not a fan of global variables, it won't be better to create a "timer" object or a "systemController" object to manage global settings for the map?

(I wonder if there's an impact in performance for doing it this way... mmm)

Wednesday, January 30, 2013 at 4:19:05 PM
1
r4dicaldreamer 3,240 rep

great...I'll need this
thank you, ramones

Tuesday, February 05, 2013 at 10:12:08 AM
1
tyfricko 6,590 rep

Yes, super useful. Good work :)

Sunday, February 17, 2013 at 4:35:32 PM
2
ViiVoices 2,942 rep

Thank you! Great Tutorial!

Tuesday, February 26, 2013 at 3:54:29 AM
1
SergioRM 6,968 rep

Thank you! 1 Favourited !!

Tuesday, February 26, 2013 at 10:56:53 PM
1
Tekniko 17.9k rep

Thanks for this. Exactly what I was looking for.

Thursday, March 21, 2013 at 4:27:49 PM
1
KennyOops 4,012 rep

Wonderful! Thanks a lot!

Saturday, July 13, 2013 at 8:29:09 AM
1
Lordshiva1948 44.2k rep

Thank you mate very nice of you to share this info

Sunday, August 11, 2013 at 9:18:56 AM
1
masterfelix 1,094 rep

Thanks, helped a lot in my project.

Monday, September 02, 2013 at 2:07:07 PM
1
theunreal 1,168 rep

Thanks! Awesome!

Friday, October 04, 2013 at 12:48:06 PM
2
ameersak 573 rep

very good! was looking for this!

Thursday, December 05, 2013 at 11:56:43 AM
2
mrkhingston 367 rep

Nice timer demo. A lot of cool stuff happening there. Useful. Thanks :)

Thursday, April 10, 2014 at 2:04:07 AM
1
Beren 420 rep

Thank you very much, great tutorial. I was struggling with "Every x seconds" and why even with other conditions it would count and now I understand. Plus, timer is much more interesting.

Thursday, April 24, 2014 at 3:55:19 PM
1
DarkBoge 1,582 rep

I am grateful that this was here when I needed it. Thank you.

Tuesday, July 08, 2014 at 7:13:11 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.