Time counter

4

Index

Stats

12,983 visits, 25,248 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 7 Dec, 2011. Last updated 19 Feb, 2019

All cool games have a time counter of some sort. Regardless if it counts upward or downward. So to get a nice counter going, you can use this basic, and simple setup.

[Update]

I created a Plugin with similar functionality as this tutorial.

If anyone should be interested, look at:

Forum thread about Time Manager Plugin

Ok, now for the tutorial!

Here you can see the final structure of my time counter:

Now you see how I have structured it. And below I will explain each step in more detail.

1.

First off, I set a couple of local variables that will store some needed values. These values are only local, since they are only needed for the time calculation, but if you rather have it as global, that would be fine too!

TotalTime - Is a Text variable that will contain the final time text.

Hours - Is a Number variable and will contain how many hours I have.

Minutes - Is a Number variable and will contain how many minutes I have.

Seconds - Is a Number variable and will contain how many seconds I have.

I have also addded a global variable, called CurrentTime.

This variable will contain the system time, that I get from Construct.

This will be explained later in the tutorial.

2.

At every tick, I need to set some of the local variables. I do this

at every tick, so that there is a flow in the updates and it moves smoothly.

- I set the TotalTime to zero (or as a Text: ""), so I avoid getting some strange strings in the text.

- The calculation to get hours from my global time variable, looks like this:

    int(int(CurrentTime/60)/60)%24)

What I do there, is I take the global time variable, I divide it by 60, to loose the seconds. And then I divide that result with 60 to loose the minutes. This result I convert into a integer with the int() function, because I dont need any decimals at all. Then when I have the integer, I use %24 to scale the value into 24-hour space. That means, when the result is 24, it will be automaticly converted to 0, since there is only 24 hours in a day. The same is if the result is -1, it will be automaticly converted to 24, since there is no negative hours.

- The calculation to get minutes from my global time variable, looks like this:

    int(CurrentTime/60)%60

As with hours, I take the global time variable, and I divide it by 60, to loose the seconds. And the result I use %60 to scale the value into 60-minute space. That means when the result is 60, it will be automaticly converted to 0, since there is only 60 mins in a hour. The same if the result is -1, it will be converted to 59, since there is no negative minutes.

- The calculation to get seconds from my global time variable, looks like this:

    int(CurrentTime%60)

The difference between hours, minutes and seconds, is that I dont need to divide the global time variable with 60, since I already got the seconds! So I use %60 on the value so its scaled into the 60-second space. Since there is no 61 seconds or -1 seconds.

So now I have resetted the TotalTime, and I have gotten how many hours, minutes and seconds there is in the system time.

Now we will look at adding these values into the TotalTime Text and display it!

  • 0 Comments

Want to leave a comment? Login or Register an account!