exiting a long handler

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

exiting a long handler

Post by adventuresofgreg » Tue Jun 19, 2012 7:26 pm

Hi: I have a handler which controls a long sequence of animations, and I would like to be able to exit this handler (cancel it) from another script, anytime.

I can do it if I add: "if cancelme = 1 then exit animationHandler", but I have to place this line multiple times throughout the sequence in order to 'catch it' when I set the global cancelme to equal 1 from outside the animationHandler.

Is there a way in LiveCode to directly exit a handler from outside the handler without having to continuously look for a global setting?

Thanks,
greg

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: exiting a long handler

Post by jmburnod » Wed Jun 20, 2012 12:03 am

Hi Greg,

Do you have a loop (by repeat) in your handler animationHandler ?
If yes you can try

Code: Select all

wait 10 milliseconds with messages
in the prev line of "if cancelme = 1 then exit animationHandler"

and "cancelme" should be a local or global variable

Code: Select all

local cancelme

on StartCount
   put 0 into cancelme
   repeat with i = 1 to 10000
      put i
      wait 10 milliseconds with messages
      if cancelme = 1 then exit StartCount
   end repeat
end StartCount

on stopCount
   put 1 into cancelme
end stopCount
Best regards

Jean-Marc
https://alternatic.ch

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: exiting a long handler

Post by adventuresofgreg » Wed Jun 20, 2012 1:03 am

Thanks Jean-Marc

No, there is no loop in the long handler. It's a step by step list of instructions with arrows pointing to different things, and buttons being pressed, etc. Sort of like:

make this field slide in (with aemoveto animationEngine handlers)
wait 2 seconds
move a graphic of a hand to a button
click the button
move the hand away
move the field out
move a new field in
wait 5 seconds
etc, etc, etc

I need some way for the user to EXIT out of this long list of instructions. Currently the only way I can think of doing it is inserting "if global cancelme=1 then exit this handler" between each and every single line. It works, but it isn't very elegant, and won't work until the current "step" is completely finished.

greg

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: exiting a long handler

Post by dunbarx » Wed Jun 20, 2012 2:02 am

I don't see a way out of checking for some sort of state as often as required. I just finished a project where several different running handlers were possible. I had to include a "check" all over the place, whether or not the mouse had been clicked in an "abort" button. I had the advantage of needing many fast repeat loops, so the checking was done constantly.

Inelegant is in the eye of the beholder. Peppering these things throughout your project may seem like clutter, but I don't see it that way. Think of them as friends, watching your back.

Craig Newman

LittleGreyMan
Posts: 49
Joined: Sat Jun 16, 2012 7:57 pm

Re: exiting a long handler

Post by LittleGreyMan » Wed Jun 20, 2012 9:57 am

Hi Greg,

You could put your animation handler in a container, as an invisible field, or whatever suits you.

Then run instead something like that (pseudo-code, not tested):

Code: Select all

on animationHandler
repeat with i = 1 to the num of lines of animationCode -- the code container
get line i of animationCode
-- put exit statement here if you want to exit before the next step
do it
if cancelme = 1 then exit animationHandler -- exit after this step
end repeat
end animationHandler
It will automagically handle any number of steps, and the main handler can be modified at will.

HTH
Best regards,

Didier

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: exiting a long handler

Post by sturgis » Wed Jun 20, 2012 1:54 pm

This method should work well enough. You'll want to have a wait with messages in your repeat loop somewhere so there is enough breathing space to set your cancel variable.

You might also study your code and see if a generic handler could be written to handle the animations. Pass the object to be animated and enough info for a function or command to determine what to do with the object. Then similar to the do method, build up a list of action lines and pass them to your function 1 by one while checking the value of your cancelling variable each iteration.
LittleGreyMan wrote:Hi Greg,

You could put your animation handler in a container, as an invisible field, or whatever suits you.

Then run instead something like that (pseudo-code, not tested):

Code: Select all

on animationHandler
repeat with i = 1 to the num of lines of animationCode -- the code container
get line i of animationCode
-- put exit statement here if you want to exit before the next step
do it
if cancelme = 1 then exit animationHandler -- exit after this step
end repeat
end animationHandler
It will automagically handle any number of steps, and the main handler can be modified at will.

HTH

Post Reply