Button use while script is running

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Bennett
Posts: 3
Joined: Wed Nov 09, 2016 4:47 am

Button use while script is running

Post by Bennett » Wed Nov 09, 2016 7:19 am

I can't seem to be able to use the buttons that I programmed to stop a script from running. I have a certain script that runs numbers starting for example at 1 and going to 100 I have tried to create a button to stop the script from running when the player of the game desires. However, the button is only active when the script is not running the numbers. How can I fix this so that I can press the button to stop the script while its running? :(

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Button use while script is running

Post by [-hh] » Wed Nov 09, 2016 8:03 am

Make a start/stop button that starts on mouseUp a 'self-calling action' and stops it on mouseDown, simply by setting a true/false variable.
The mouseDown/mouseUp technique is here for me better than putting it in one of mouseDown or mouseUp, what is, of course, also an option.

Code: Select all

local iWantToStop=true
on mouseDown
   put not iWantToStop into iWantToStop
end mouseDown
on mouseUp
   if iWantToStop then exit mouseUp
   else doAction 1
end mouseUp
on doAction x
    if iWantToStop or x > 100 then exit doAction
    put x into fld "numbers" -- test
    add 1 to x; send "doAction x" to me in 8 millisecs -- or slower
end doAction
Thus you give here to others, to LC or your OS or your additional buttons, 8 millisecs time to get clicks and receive messages after each of your repeats in a 'long' loop.
shiftLock happens

Klaus
Posts: 13866
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Button use while script is running

Post by Klaus » Wed Nov 09, 2016 12:56 pm

Hi Bennet,

1. welcome to the forum! :D
2. I deleted your double posting!


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Button use while script is running

Post by dunbarx » Wed Nov 09, 2016 3:26 pm

Hi.

Can't you simply insert a line like this:

Code: Select all

if the mouseLoc is within the rect of btn "stopHandler" and the mouse is down the exit runningHandler
Assuming that your running handler is called "runningHandler". Oh, and you make a button "stopHandler" :wink:

Now this will use just a little overhead, but is compact and readable.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Button use while script is running

Post by [-hh] » Wed Nov 09, 2016 5:02 pm

Hi Craig.
You are twice polling the mouse. I bet that jacque will criticise you ;-)
Hermann
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Button use while script is running

Post by dunbarx » Wed Nov 09, 2016 6:39 pm

Hermann.

Jacque is used to that.

I did warn of a slight bit of overhead, and besides, that sort of exploitation of the xTalk UI is so old fashioned, you have to love it.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7258
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Button use while script is running

Post by jacque » Thu Nov 10, 2016 6:03 pm

I see I have trained an accomplice so the task of criticism is no longer only up to me. :)

However, I will mention that unless the mouse is down at the exact moment that line of script runs, it will fail. LC doesn't buffer mouse states.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Button use while script is running

Post by dunbarx » Thu Nov 10, 2016 8:07 pm

All true, true.

(Sob)

I was led down the garden path by the 8 mS in the OP handler. Of course, it was a bit optimistic that would stick around.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7258
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Button use while script is running

Post by jacque » Thu Nov 10, 2016 8:25 pm

Aw. Here Craig, have a tissue. And think of all the good work you've done in the forums. Wouldn't be the same without you.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Button use while script is running

Post by dunbarx » Thu Nov 10, 2016 9:26 pm

HONK.

Thanks.

So now I would do the usual practice in these cases in a "stop" button, which is what the OP actually wanted. Of course, Hermann's solution could be easily extended to that button by simply making a global out of the local.

Code: Select all

on mouseUp
repeat for each line thisMessageID in the pendingMessages
 cancel item 1 of thisMessageID
end repeat
end mouseUp
But what if there were other things pending? Then you would have to collect (with "the result") only those pendingMessages that pertained to this particular "send" command, and only cancel those.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7258
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Button use while script is running

Post by jacque » Fri Nov 11, 2016 4:39 pm

To cancel only some of the pendingMessages, l usually just do this:

Code: Select all

if "doAction" is in thisMessageID then
  cancel item 1 of thisMessageID
end if
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Button use while script is running

Post by dunbarx » Fri Nov 11, 2016 6:26 pm

Jacque.

Much nicer.

You know, I never actually looked carefully at a printout of the current pendingMessages, and never realized that the name of the handler attached to a "send" command was included in item 4 in each string. Makes sense of course.

I always just collected "the result" which gives item 1 alone.

Craig

Bennett
Posts: 3
Joined: Wed Nov 09, 2016 4:47 am

Re: Button use while script is running

Post by Bennett » Fri Nov 11, 2016 7:57 pm

The problem is not that the start or stop button doesn't work, it's that I can't click anything on the stack "the other button to pause the script for enough time for an answer to be entered" while the script is active. it will let me use the button after the script has run its course, but I would like to be able to interactively work with the script while it's running. If this makes any sense, please reply and tell me how I can fix it.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Button use while script is running

Post by mwieder » Fri Nov 11, 2016 9:53 pm

Well, since you haven't posted the code we can only respond in general terms. So...

assuming you've got something like this

Code: Select all

put 1 into x
repeat forever
   put x into field numberField
   wait 1 second
   add 1 to x
end repeat
then changing the 'wait' line to

Code: Select all

wait 1 second with messages
should allow other events to happen.
Then in your 'stop' button you should be able to cancel things.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Button use while script is running

Post by dunbarx » Fri Nov 11, 2016 10:13 pm

Benneet.

Now I am not sure what you are asking.

Did you mean that you are navigating to another stack and want to halt that handler?
k anything on the stack "the other button...
Doesn't matter. Make a new stack with a button and a field. In the button script:

Code: Select all

global stopRandoms
on mouseUp
   put "false" into stopRandoms
   makeRandoms
end mouseUp

on makeRandoms
   put random(999) into line 1 of fld 1
if stopRandoms is "false"  then send "makeRandoms" to me in 30
end makeRandoms
Now make another stack with two buttons in it. Arrange so you can see both stacks. Put this into the script of btn 1 (the stop button) :

Code: Select all

global stopRandoms

on mouseUp
   put "true" into stopRandoms
end mouseUp
Put this into the script of the other button ( the silly button):

Code: Select all

on mouseUp
   put random(999) into line 3 of fld 1 of stack "untitled 1"
end mouseUp
Now when you start the random making process, you can mosey over to the other stack at your leisure and either place random numbers into line 3 of stack 1, or stop the random maker entirely.

Both are silly.

Craig

Post Reply

Return to “Talking LiveCode”