this tutorial will only partially work with borland kylix
This is more of an introduction to the TTimer component than a tutorial really, but I felt it would be a good idea to explain the uses of this component. You may well have used this component already if you've done any of the tutorials after this one, but this tutorial should help iron out any questions you might have.
Well, often in a game it can be useful to have a set command happening at regular intervals throughout the game rather than always checking for something. In the case of the first DelphiX tutorial we only want to get the program to check if the user is using the joystick or keyboard cursor keys at small intervals. If the program was continually checking that it would become far too sensitive to each touch of the joystick or cursor keys.
That's one use, another is in tutorial seven. With artificial intelligence it can often be desirable to have a computer opponent check things at certain times to ascertain where the other player(s) are. Remember you can have many TTimers in one program, so you could in theory have different checks being made at different time intervals so that the computer opponent is constantly checking all sorts of parameters to decide if a player is within a distance of it, if there is an object in the way and so on. You get the idea. Not everything centres round just carrying out actions in regular intervals. You'll often see the TTimer component coming up in future tutorials, and it is because of these uses that it is used so often. Another thing to take into consideration is that like most components, you can alter the properties of it during the running of the program. In the case of the TTimer component the following line can simply alter the delay at that line, so you could for example speed up the running of a character when they pushed a particular button. The line below sets the interval to 100 from whatever you've set it on originally.
Timer1.Interval := 100;
Simple, yet effective. The TTimer does have its faults. You may for example want to just create a delay at any given point in your game and it can be quite messy to try and get the Timers timing just write if you want say a two second delay before the program continues. That's where the following helps out.
The Delay Command
Delphi, doesn't have a Delay command. Pascal did, most languages do but Delphi doesn't. You can use the Windows API call Sleep or SleepEX (as suggested by Jonas Dieckelmann) but this only works in certain situations for some reason. If you want to know more about either of these two calls, the Windows SDK help will give you them. For those of you who don't know, the Delay command does surprisingly, wait for a short period as required, before continuing on with something. This can be useful for placing a delay somewhere to give a player a head start or whatever else you can think of. But, if Delphi doesn't have a specific Delay command how do you get one? Well, the only solution is to write a procedure in your programs which can then be linked to elsewhere to produce a delay as required. As I've already said, you can use the TTimer component, but this code taken from issue 193 of the PC Plus magazine is much more effective:
procedure Delay(Num: longint);
until ((GetTickCount-tc) >= Num);
Make sure you place this procedure before any of your other procedures so that the Delay procedure is recognised in all procedures that use it. To actually call the delay, all you need to do is type in the following command on the relevant line:
What this does is create a one second delay as the time is measured in milliseconds. So how did the procedure work? Well basically it takes the number placed in the brackets after you type in Delay and calls it the variable Num. It then also takes a variable and calls it tc. This variable is then added onto repeatedly until it is equal to our Num variable. When that happens the program continues. The downside of this procedure is that it doesn't work with Kylix. I'm trying to find an equivalent, which will appear here shortly.
So there you go, not only did you learn about the uses of the TTimer component, but you also found out how to create a good Delay procedure (if you're just using Delphi anyway). The TTimer isn't ideal though as you can have wildly different times from one computer to another if they are not used correctly. In a future tutorials we'll cover a good alternative to this but for the moment TTimer will be just fine. If you have any problems let me know by e-mail or leave a message on the board.