Time counter

4
• 43 favourites

Stats

12,749 visits, 24,948 views

Translations

This tutorial hasn't been translated.

4.

So to get the time to counter to display itself and get the counter to actually count, you will need to create a global variable, and set the system time value into this global variable. And a example of that is to make it like this, where the global variable is called CurrentTime:

At every tick, I save the system time from Construct into the CurrentTime variable. And now you can see that the counter is slowly counting upward as time progresses!

But what if you want it to count down, and not upward?

Then we can see the following example:

The calculation I use here is like following:

``````    int(HOURS x MINUTES x SECONDS) - SYSTEMTIME

``````

(x means multiplication)

This calculation is showing a countdown from 2 hours 0 minutes and 0 seconds. Since there is 60 seconds in a minute, I need to multiply with 60. And since there is 60 minutes in a hour, I need to multiply it with 60. And Since I want to have 2 hours, I need to multiply the result with 2. The system time variable comes directly from Construct.

So now, the counter will start at 02:00:00, and start to go downwards.

So, that is it! Here you have a a basic, functional time counter for your game/application! Its easy to use, and easy to add and remove from it. If you want to use the time value outside the function, you only change the local variable into a global one!

Get Construct 2 (*.capx) example here:

Simple counter Construct project example

Note for example:

At the top of the event, there is a System->Every tick-event.

In this event there are two different actions, and one is disabled.

- The "System - Set CurrentTime to time" action, makes the counter count upwards, starting at 00:00:00 and keeps counting.

- The "System - Set CurrentTime to int(2 60 60) - time" action, makes the

counter count downwards, starting at 02:00:00 (but you will see 01:59:59) and

keeps counting down.

Please also note, that this example of the countdown will not stop at 00:00:00, it

will count down until stoped. So if you want it to stop at a certain time, you will

need to create a check yourself when it should stop.

I hope this tutorial will be of some help for you!

[Update]

I created a Plugin with similar functionality as this tutorial.

If anyone should be interested, look at:

[Update]

I have gotten couple of questions on how to "reset" the time counter when you exit the layout and come back in. What you basically need to do, is to get the current time for every time you enter your layout. So that the counter doesn't count from a older time value.

So what you can do is:

- Create a global number variable, call it "countdowntime" or what you want.

- create a event with the "On start layout".

- Create a action in the "On start layout" where you set the "countdowntime" variable to "Amount of time to count down + time"

This will set a specific value, meaning the current time PLUS your count down time. So whenever you start the layout, you will take the current system time, and you will add the countdown value to that time.

Now you use this "countdowntime" and you subtract that along with the current time (time), at every tick or whenever you want, like so: "countdowntime" - time

Here is a construct file for it:

http://dl.dropbox.com/u/52716812/Timecounter_with_Restart.capx