Author Topic: Could _EXIT be made to ignore SLEEP and _DELAY?  (Read 2476 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Could _EXIT be made to ignore SLEEP and _DELAY?
« on: January 26, 2021, 03:28:19 pm »
I don't need this, but someone might.

So this example works okay, but only if it has ON TIMER in it...

Code: QB64: [Select]
  1. PRINT "Hold down Esc key until program ends.": PRINT
  2.  
  3. ON TIMER(1) GOSUB quit
  4.  
  5. KEY 15, CHR$(1) ' Esc key
  6. ON KEY(15) GOSUB break_program
  7.  
  8. KEY(15) ON
  9.  
  10.     ' Do stuff here...
  11.     _DELAY 600 ' Take a break...
  12.  
  13. break_program:
  14.  
  15. quit:
  16. IF _EXIT THEN CALL exit_warning
  17.  
  18. SUB exit_warning
  19.     DO
  20.         LINE INPUT "Do you want to exit. Y/N ? ", a$
  21.         IF UCASE$(a$) = "Y" THEN SYSTEM
  22.         IF UCASE$(a$) = "N" OR a$ = "" THEN EXIT DO
  23.     LOOP
  24.  

If _EXIT just ignored the SLEEP and _DELAY statements, if that's possible, you would not need the ON TIMER part at all.

Also, there was another thread where the author was trying to decide on using SLEEP vs _DELAY for very long pauses. _DELAY uses a little less resources, but what if he had to break into the program. SLEEP would allow a keypress to break the delay, but _DELAY would require the ON KEY() approach, as posted above, but without the added _EXIT function.

The otehr thing I wanted to mention is the process coded above is clunky. It appears ON TIMER fights a bit with ON KEY(), causing a noticeable delay in executing the key input. That's why I printed the instructions to hold the Esc down, instead of just pressing it. Is there any method to smooth it out?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Could _EXIT be made to ignore SLEEP and _DELAY?
« Reply #1 on: January 26, 2021, 03:37:33 pm »
Isn't this just a simple case of Write your own function for your specific needs?

Code: QB64: [Select]
  1. PRINT "Hold down Esc key until program ends.": PRINT
  2.  
  3. ON TIMER(1) GOSUB quit
  4.  
  5. KEY 15, CHR$(1) ' Esc key
  6. ON KEY(15) GOSUB break_program
  7.  
  8. KEY(15) ON
  9.  
  10.     ' Do stuff here...
  11.     Delay 600 ' Take a break...
  12.  
  13. break_program:
  14.  
  15. quit:
  16. IF _EXIT THEN CALL exit_warning
  17.  
  18. SUB exit_warning
  19.     DO
  20.         LINE INPUT "Do you want to exit. Y/N ? ", a$
  21.         IF UCASE$(a$) = "Y" THEN SYSTEM
  22.         IF UCASE$(a$) = "N" OR a$ = "" THEN EXIT DO
  23.     LOOP
  24.  
  25. SUB Delay (time AS _FLOAT)
  26.     DIM t AS _FLOAT
  27.     t = TIMER + time
  28.     DO
  29.         _DELAY .001
  30.     LOOP UNTIL TIMER > t

Of course, to do this type of thing without midnight overflow, I'd suggest making use of my ExtendedTimer routine, but you can always code around it the OldSkool(tm) way, which you seem to prefer...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Could _EXIT be made to ignore SLEEP and _DELAY?
« Reply #2 on: January 26, 2021, 03:49:52 pm »
Nice, and yes, I have plenty of those timer routines, midnight 86400 adjustment included. So for me, I'd never use _DELAY for anything else that short pauses for display effect.

I'm just thinking the QB64 language could be improved by having the ON TIMER with ON KEY() play better together, without the lag issues with key input, or the _EXIT could be improved by ignoring SLEEP and _DELAY statements, if that's even possible.

So this is not for my personal use, just in regards to the language, for anyone here who still works in that area.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/