Author Topic: On Timer only allows SUB with Integer argument error?  (Read 5895 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: On Timer only allows SUB with Integer argument error?
« Reply #15 on: September 20, 2019, 05:02:45 pm »
Its useful in games programming. Interrupts are kinda mysterious. Think of two or more programs running concurrently. I think about this hot wheel timer I want to write, the two events you can't miss are when the car enters the first gate, and when it enters the 2nd gate and how much time has elapsed. Basic alone might be too slow to see these events if you need accuracy to 1/1000th a second, interrupts can help with that.  I'm still not to where I have done one yet tho.

I'm sure the mind of a juggler appreciates them. So many tasks going on simultaneously :-)

Well for timing cars like a stop watch, just use start! = TIMER(.001) and stopTime! = TIMER(.001) and subtract Stop - Start.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: On Timer only allows SUB with Integer argument error?
« Reply #16 on: September 20, 2019, 06:55:53 pm »
I doubt the whole concept of passing arguments to a timer called SUB works well at all for a senseful use.
The passed variable must be initialized prior the ON TIMER statement, otherwise it won't work, hence the concept is pretty useless for passing any variable which changes during runtime...

Code: QB64: [Select]
  1.  
  2. 'x% = 1 'this works
  3. ON TIMER(1) TimerSub (x%)
  4. x% = 1 'doesn't work
  5.  
  6. PRINT "Next five seconds in main module, TimerSub() should print 1..."
  7. xWait
  8.  
  9. SUB xWait
  10.     PRINT "Now five seconds in a SUB, TimerSub() should still print 1..."
  11.     SLEEP 1
  12.     SLEEP 1
  13.     SLEEP 1
  14.     SLEEP 1
  15.     SLEEP 1
  16.  
  17. SUB TimerSub (arg%)
  18.     PRINT arg%
  19.  
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: On Timer only allows SUB with Integer argument error?
« Reply #17 on: September 21, 2019, 11:43:54 am »
Now think about the nature of ON TIMER, you define a time interval for the function call, but who says that your program is right executing anywhere in your main module when the interval is elapsed and the DoSomething(X) function is called.

What happens, when your program is executing within any of your SUBs or FUNCTIONs when the timer is ready to call DoSomething(X)? What is passed then? Because variable X is not known in your SUBs/FUNCs unless SHARED or maybe defined as a local X within the SUB/FUNC.

That should be up to the programmer to keep track of, at that point it is their responsibility. And I have ran into that, where I didn't want a TIMER event to run while certain SUBs were running, and had to turn TIMER off anytime those SUBs were running then back on when they returned.

That is all on the Programmer.

Another situation is where X could be different in each SUB or FUNCTION too and maybe you want that to be part of the program so the TIMER CALLed routine actually does something different depending on where in the program its happening.

If everything has to be SHARED then nothing in your program would work outside that program, so you couldn't make a Library of code for use in multiple programs. each one would have to be tailor made and that is not very helpful or fast.

TIMERs are actually very powerful tools, and need to be used responsibly to begin with.
Granted after becoming radioactive I only have a half-life!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: On Timer only allows SUB with Integer argument error?
« Reply #18 on: September 21, 2019, 02:12:19 pm »
I agree with Cobalt. If ON TIMER does not reject the SUB in the example above, it is sufficient to turn off the timer at the beginning of the called SUB and to switch it on again before ending this SUB. The argument check for the called SUB by ON TMER - i don't understand what it is for. After all, the task of ON TIMER is to run SUB. It is not his task to check the parameters with which the SUB is triggered.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: On Timer only allows SUB with Integer argument error?
« Reply #19 on: September 21, 2019, 02:33:11 pm »
Quote
RhoSigma wrote:
I doubt the whole concept of passing arguments to a timer called SUB works well at all for a senseful use.
The passed variable must be initialized prior the ON TIMER statement, otherwise it won't work, hence the concept is pretty useless for passing any variable which changes during runtime...

Big true. In this must be some bug. I add BEEP to your SUB TimerSUB and row arg% = arg% + 1. WORK NOT. But BEEPs. If is this used in SUB, which is not called by ON TIMER this works.

Code: QB64: [Select]
  1.  
  2. 'x% = 1 'this works
  3. ON TIMER(1) TimerSub x%
  4. x% = 1 'doesn't work
  5.  
  6. PRINT "Next five seconds in main module, TimerSub() should print 1..."
  7. TimerSub x% 'if is the same sub called "normally", x% is returned as bigger. If is this sub called by ON TIMER, is x% returned as zero
  8. TimerSub x%
  9. TimerSub x%
  10. TimerSub x%
  11. TimerSub x%
  12. xWait
  13.  
  14. SUB xWait
  15.     PRINT "Now five seconds in a SUB, TimerSub() should still print 1..."
  16.     SLEEP 1
  17.     SLEEP 1
  18.     SLEEP 1
  19.     SLEEP 1
  20.     SLEEP 1
  21.  
  22. SUB TimerSub (arg%)
  23.     arg% = arg% + 1
  24.     PRINT arg%
  25.     BEEP
  26.  
  27.  
  28.  


and BEEP signalize, that this SUB is not end before increase the value of arg%.