Ooops
January 15, 2021, 06:29:57 PM

Author Topic: [bb] Asynchronous timers by BlitzSupport [ 1+ years ago ]  (Read 422 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Asynchronous timers by BlitzSupport [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Asynchronous timers
Author : BlitzSupport
Posted : 1+ years ago

Description : This stuff lets you add asynchronous timers and execute code when they expire. If you set them again when they expire, the code can therefore be executed on a regular basis, while your game continues as normal (the normal timer functions pause execution of the whole program until they run out).

Code :
Code: BlitzBasic
  1. ; Timer structure:
  2.  
  3. Type Timer
  4.         Field start
  5.         Field timeOut
  6. End Type
  7.  
  8. ; Set a timer:
  9.  
  10. Function SetTimer.Timer (timeOut)
  11.         t.Timer = New Timer
  12.         tstart   = MilliSecs ()
  13.         t       imeOut = tstart + timeOut
  14.         Return t
  15. End Function
  16.  
  17. ; Check for timeout:
  18.  
  19. Function TimeOut (test.Timer)
  20.         If test <> Null
  21.                 If test imeOut < MilliSecs ()
  22.                         Delete test
  23.                         Return 1
  24.                 EndIf
  25.         EndIf
  26. End Function
  27.  
  28. Graphics 640, 480
  29. SetBuffer BackBuffer ()
  30.  
  31. ; Set timer before main loop:
  32. t.Timer = SetTimer (1000)
  33.  
  34. Repeat
  35.  
  36.         Cls
  37.  
  38.         ; If timer runs out, add another "o" and reset the timer:
  39.         If TimeOut (t)
  40.                 a$ = a$ + "o"
  41.                 t = SetTimer (1000)
  42.         EndIf
  43.        
  44.     ; This stuff carries on regardless:
  45.         Rect MouseX (), MouseY (), 20, 20
  46.         Text 0, 0, a$
  47.        
  48.         Flip
  49.        
  50. Until KeyHit (1)
  51.  
  52. End


Comments :


Mikorians(Posted 1+ years ago)

 Be aware of the flaw:Millisecs() scrolls to 0 at midnight.I always use:If Abs(millisecs()-recordedtime)>desirednummillisecs then ...It's not perfect, but will keep you from a midnight stall.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal