Page 1 of 1

I need help with repeat

Posted: Fri Sep 11, 2020 4:06 pm
by Fuenzy
Hey so I'm trying to make a repeat so it lowers a score by 1 every ,5 seconds but resets back to 30 when a button is clicked When the repeat is active, nothing can be used, you just watch the countdown drop from 30 to 0
Any way to use repeat and be able to use buttons and stuff without it freezing everything?
on eggtimer
repeat until field "numbers" < 1
wait ,1 seconds
subtract ,1 from field "numbers"
wait 4 seconds
end repeat

end eggtimer

Re: I need help with repeat

Posted: Fri Sep 11, 2020 4:22 pm
by dunbarx
Hi.

This is a case for the "send in time" variant. Read about "send" in the dictionary.

On a new card make a button and a field. Put this into the button script:

Code: Select all

on mouseUp
   put 31 into fld 1
   startTimer
end mouseUp

on startTimer
   if the optionKey is down or fld 1 = 0  then exit to top
   subtract 1 from fld 1
   send "startTimer" to me in 30
end startTimer
The timer will start counting, and you are completely free to do whatever you want to on that card while this is going on. Try it. Make another button that does something else, and you can work that as often as you like.

The command '"send in time" is by nature not blocking, because new messages are generated automatically by the engine. When you use a repeat loop, all activity stops until the current handler ends. Fortunately, there are other methods that do not suffer from that,

Craig

EDIT.

This is a bit more advanced, and you need not worry about it now, but there is an issue with re-clicking the button. A better solution is:

Code: Select all

on mouseUp
   repeat for each line tLine in  the PendingMessages --fixes issue with clicking multiple times
      cancel item 1 of tLine
   end repeat
   
   put 31 into fld 1
   startTimer
end mouseUp

Re: I need help with repeat

Posted: Fri Sep 11, 2020 4:30 pm
by dunbarx
This is a perfect time for homework.

Why start with 31? Is there a cleaner way?
What is the story with the "30" in the 'send" line?
What stops the count? What else does, and why include it?
Currently, if you click the button while the counter is running, the process restarts. Is this a good thing? Should we not be able to do that?

Do we see how this flows? Any questions?

Watch it, Jacque.

Craig

EDIT. See the edit in the preceding post. It relates to the homework above, clicking the button while counting.

Re: I need help with repeat

Posted: Sat Sep 12, 2020 12:47 am
by stam
Hi Fuenzy,

If i've understood correctly you want a score of 30 to decrement by 1 every 5 seconds and stop at 0 - and a reset button that stops this and puts the score back to 30?

You can also do this with wait with messages (for non-blocking interface that allows interface to work normally while waiting, so you can reset the score/stop the timer)

Pseudocode for the essence of this (and i'm sure there are better ways...):

Code: Select all

 repeat until tScore is 0
     subtract 1 from tScore
     put tScore into field "scoreField"
     wait 5 seconds with messages
     if cancelled then exit repeat
end repeat
I've thrown together a rough and ready example: timer.livecode
All the code is in the card script to include a start/stop/reset for the above. The score and a boolean to indicate if countdown is active or cancelled are set as script variables...

Hope that helps!
Stam