MilliSecs ( )
Parameters
| None. |
Description
|
Returns a steadily increasing millisecond counter. MilliSecs is the timing command you will use most. It counts up from when the machine started, not from when your program did, so the absolute number means very little - what matters is the difference between two readings. The standard pattern is to remember a time and compare against it later. Store start=MilliSecs() before a load, subtract afterwards, and you know how long it took. Keep next_shot=MilliSecs()+250 and refuse to fire until MilliSecs() passes it, and you have a weapon cooldown that behaves the same whatever the frame rate. Frame-rate independent movement works the same way: measure the milliseconds since last frame and scale your movement by it. It is also the usual source of a changing random seed - SeedRnd MilliSecs() at startup gives you a different sequence every run. One gotcha. The counter is a 32-bit value taken from the system uptime, and it eventually wraps back round to zero - after roughly 24.9 days of uptime on the modern runtime. Comparing two readings with subtraction survives the wrap; storing an absolute deadline and testing MilliSecs() greater than it does not. Always write your checks as MilliSecs()-start >= duration. Resolution is about a millisecond, which is plenty for gameplay. For finer measurements use CPUTimer. See also: CPUTimer, Delay, CreateTimer, SeedRnd, CurrentTime. |
Example
; MilliSecs Example ; ----------------- Graphics 640,480,0,2 SetBuffer BackBuffer() ; MilliSecs() returns the system timer in milliseconds. Read it at two ; moments and subtract to get elapsed time - the basis of stopwatches, ; cooldowns and frame-rate independent movement. start=MilliSecs() While Not KeyDown(1) Cls ; Restart the stopwatch with Space If KeyHit(57) Then start=MilliSecs() ; Elapsed milliseconds since the stopwatch started elapsed=MilliSecs()-start ; Time-based motion: position comes from elapsed time, not from a ; frame count, so the square moves at the same speed on any computer x=(elapsed/4) Mod 640 Rect x,220,40,40,True Text 0,0,"Space: restart stopwatch Esc: exit" Text 0,20,"MilliSecs() = "+MilliSecs() Text 0,40,"Stopwatch: "+(elapsed/1000)+"."+((elapsed Mod 1000)/100)+" seconds" Flip Wend End
Index