Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / TDK's Timer Tutorial

Author
Message
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 21st Aug 2005 01:28 Edited at: 7th Jul 2007 20:48
This is just one of many beginners tutorials available for Dark Basic by TDK_Man. They were primarily written for DB Classic users, but because they start at such a very low level and cater for complete newcomers to programming, the vast majority of them apply equally to DBPro users as well. You can find all of them here:

Dark Basic Tutorials For Complete Beginners


** TDK_Man's Timer Tutorial **


One subject you often see questions about on forums is that of timers. These may be for showing an on-screen display of either time left or time elapsed, though timers have many other uses.

This tutorial will show the seasoned DB user nothing new, but is instead aimed at the new programmer and looks at the way timers work and how they can be used in your programs. Feel free to copy any of the code in this tutorial into DB and run it.

TDK_Man


Timer Tutorial

All PC's have built-in timers which place values into registers for programmers to access. DB has a function called Timer() which accesses the computers timer register and returns values in one thousandths of a second increments.

To convert these values to seconds, we simply have to divide them by 1000. If you need finer timings than one second intervals, then you can divide by 100, 10 or not divide by anything at all to return 10th, 100th and 1000th of a second increments respectively.

The value can obviously be stored in a variable, so if for example, you use:

RetVal=Timer()

...then the current value of the PC's timer is stored in the variable RetVal.

This value can then be used for a multitude of tasks including calling procedures after a set amount of time (as a way to make your programs run the same speed on all spec machines), on-screen clocks & timers or any other timed events in your programs, (maybe it goes dark after playing for an hour, or a plane flies overhead every 15 minutes).

You are also not restricted to a single timer either. You can have as many independent timers as you like in your programs by using different variables. For example:

T1=Timer()
T2=Timer()
T3=Timer()


Will give you three timers which can be used for timing three separate events.

Copy and paste the following code into DB and run it:



The value you see is the contents of the timer register and it is continually changing, and fast! - even when your program is not running! This value is not a lot of use, so modify the code as shown below.



Now the value changes, but ticks over at a more useful once per second. It's still not totally useful as it will display a random value on every machine. To fix this, we need to grab this value into a variable and deduct it from the value of every subsequent use of Timer(). The result is a second counter that starts at 0 (zero):




ELAPSED TIME

To create an 'elapsed time' display, the basic programming procedure is as follows:

1. Grab the current value of Timer() into a 'start time' variable
2. In a loop, read updated values of Timer() into a 'current time' variable
3. Subtract the 'start time' value from the 'current time' value
4. Divide the result by 1000 to give the number of seconds elapsed.

In DB, the code for a 30 second timer would look something like this:



In your own programs, 'T=Timer()' is placed just before entering your main program loop and the line 'Elapsed=(Timer()-T)/1000' is placed somewhere inside your main loop with an If Elapsed= clause immediately following it:



Basically, this program counts the number of elapsed seconds in the variable 'Elapsed' and then checks to see if that value equals 60 (1 minute). If it does, the timer goes back to zero and continues indefinitely.

When the timer hits 60 seconds, (or whatever value you set), then what your program does is up to you. In the above example, the variable MinutesPassed is incremented - effectively counting the number of minutes elapsed. The program could then be set do do something specific when MinutesPassed equals a specific number of minutes.

Your program could just as easily call a function or subroutine when Elapsed reaches a given value.

Once the target value has been reached and the required task completed, you need to reset the timer. So, in our example above, we set the variable Elapsed to equal zero.

The next part of the program is the bit that most new programmers trip up with:

Having reset the variable Elapsed to zero, the formula 'Elapsed=(Timer()-T)/1000' is still using the original start value stored in 'T' and will therefore continue calculating the elapsed time from when the program was first run.

So, the start value variable needs updating with a new start time. We do this by putting another T=Timer() line inside the If 'Elapsed=' block of code. The value of Elapsed will then calculate the number of seconds elapsed from this point instead of the old one - ie from zero again.


COUNTING TIME DOWN

Counting down is essentially the same as counting up, so if say you want to give the user of your program a set amount of time to complete a task, then a slightly modified version of the first example is all that is required:



The only differences in this example are the use of a variable called Seconds which contains the number of seconds to count down and the line 'TimeLeft=Seconds-Elapsed' which subtracts the elapsed time from the number of seconds in the level, placing the result in the variable 'TimeLeft'.

For example, when 10 seconds have elapsed, TimeLeft equals 30-10, or 20 seconds. When Timeleft gets to zero then the example program ends.


A PROPER CLOCK DISPLAY

If you want a digital clock display, then it really is quite simple and only needs a single timer.



In this example, the timer is used simply to get the seconds elapsed. When the value of the variable 'seconds' hits 60, it is reset to zero and the variable 'minutes' is incremented. In the same way, when 'minutes' hits 60, it too is reset to zero and 'hours' is incremented. 'Hours' is reset to zero when it hits 24 (not 60) as there are 24 hours in the day.

The rest of the program converts the numeric variables to strings with STR$() and formats the strings with a leading "0" if less than 10. The time is then printed to the screen.

If you have been able to follow the examples in this tutorial, you should have a good working knowledge of how timers work and can go away and implement your own ideas using Timer().

There are many more tutorials and example code snippets on TGPF - my game programming forums. Click on the link below - it's free to join and everyone's welcome!

Visit TDK_Man's Forums: TGPF

TDK_Man

John H
Retired Moderator
22
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 22nd Aug 2005 08:27
Hes ALIVE!!!!!!!!!!!

Nice post TDK ^_^ Stickatized.


Click here to join our forums and get updates on game progress sooner!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Aug 2005 13:42 Edited at: 22nd Aug 2005 13:44
Not taking anything away from the tutorial above, which is a very good resource (nice one TDK )...just to let you know there is also a Timer tutorial in last month's newsletter...

http://www.thegamecreators.com/data/newsletter/newsletter_issue_31.html



Between them, you should have no excuse to get timing wrong again...ever!

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 22nd Aug 2005 16:28 Edited at: 22nd Aug 2005 16:30
RPGamer:

Yep - 'fraid so. And back to stay now my Delphi stuff is finished.

BatVink:

Oh bugger - I didn't know that.

Until very recently, I was still registered with my old MatEdit.com e-mail address for the newsletters so I haven't received any for months.

I'm going through my old CD archives and just passing on anything I find that might be useful to newcomers. Guess I should update myself with the recent newsletters before I post anything else...

Too late now I suppose.

TDK_Man

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 23rd Aug 2005 01:26
The Newsletters are fantastic these days, they've really improved over this past year. You should take a look at the archives if you've missed them. They even have articles by "industry professionals".

John H
Retired Moderator
22
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 28th Aug 2005 10:16
TDK: Its a good thing your back silly pants


Click here to join our forums and get updates on game progress sooner!
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 29th Aug 2005 02:12
Now, now RPG - you'll have people talking!...

TDK_Man

CaMeRoN H
19
Years of Service
User Offline
Joined: 21st Aug 2005
Location: Who knows?
Posted: 30th Aug 2005 02:26
hahhahahaha
Quote: " TDK: Its a good thing your back silly pants "

haha u guys crack me up
Quote: " Now, now RPG - you'll have people talking!... "



Current Project: Stealth Operations (Fps)
Krimzon DestinE
19
Years of Service
User Offline
Joined: 18th Sep 2005
Location:
Posted: 18th Sep 2005 20:51
cool
UFO
19
Years of Service
User Offline
Joined: 11th Oct 2005
Location:
Posted: 15th Oct 2005 23:00
That really helped. Thank you.
Mnemonix
21
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: Skaro
Posted: 1st Nov 2005 10:21
I used my new tutorial compilation system on your tutorial as a test. It produced this file (see attachment). This is the tutorial in a nice .pdf file.

TDK: If you object to my uploading this file then just tell me and I will remove it.

WE SHALL BECOME ALL POWERFUL! CRUSH THE LESSER RACES! CONQUER THE GALAXY! UNIMAGINABLE POWER! UNLIMITED RICE PUDDING ! ! ! ETC. ! ! ! ETC.! ! !

Attachments

Login to view attachments
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 3rd Nov 2005 06:28
Quote: "TDK: If you object to my uploading this file then just tell me and I will remove it."


No objections at all!

TDK_Man

Darkness
19
Years of Service
User Offline
Joined: 17th Nov 2005
Location:
Posted: 22nd Nov 2005 17:56
hey ppl
Bluestar4
18
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 19th Dec 2005 15:27
heres the base code for a timer that will change a variable every second in place of every millisecond and provides a solution for those who need to make a game with either a printable timer that only changes every second, or set up a timer that decrements every second for those games with missions that require a level be completed in a certain time. As you can see from the code, you could alternativly replace the do-loop statment with a repeat-until missionfailtime=0 statement and remove the last if statement. the workhorse of the program is the newb=val(right$(get time$(),2)) statement. I have found this is the most effective way to create a timer



newb=0 : oldb=0 : missionfailtime=200
`main loop begins
DO

newb=val(right$(get time$(),2))

if newb<>oldb
oldb=newb
` your code goes here .......
cls : dec missionfailtime
print "mission failure in " missionfailtime
` end of your code......
ENDIF

IF MSSIONFAIL=0
`your code goes here .....
ENDIF

LOOP

Bluestar4~

Login to post a reply

Server time is: 2024-11-27 14:34:15
Your offset time is: 2024-11-27 14:34:15