Page 1 of 1

LC crashing with repeat

Posted: Thu Jul 09, 2015 10:43 pm
by sinep
I am trying to make a repeat or looping function and every time I've tried, Livecode has crashed.

I have it so that if a user hits a button it puts "1" to a field. I'd like it to repeat (add 1) every 3 seconds until the user clicks another button called "stop."

Every time I try just a simple:

repeat forever OR repeat until hilite of button "stop"
put "1"into field "score"
wait 3 seconds
end repeat

the program crashes and I'm unable to fool around with the code. I have no idea where to start and haven't been able to tinker simply because it keeps crashing....

anything will help :)

Re: LC crashing with repeat

Posted: Thu Jul 09, 2015 10:58 pm
by Klaus
Hi sinep,

you created a "loop of death", welcome to the club! :D

Here is why:
1. "repeat forever" is highly dangerous unless you create any IF THEN condition to get out of the loop!

2. "wait X" will also not leave any room for any user interaction. If you use wait in this way add "with messages"!

3. "the hilite of btn xyz" is a property and NOT a message, so "repeat until hilite of button "stop""
will also create an endless loop because the script will not get notified if the user clicks button "stop"

I presume button "stop" is a checkbox?
If yes, you can do something like this in the button script:

Code: Select all

...
repeat
  if the hilte of btn "stop" then
     exit repeat
  end if
  add "1" to field "score"
  wait 3 seconds with messages
end repeat
...
If not I will need to come up of a more elaborate solution. 8)


Best

Klaus

Re: LC crashing with repeat

Posted: Fri Jul 10, 2015 1:25 am
by sturgis
This does actually work..
repeat until (the hilite of button "stop")
It even works with regular buttons if the mouse is down during the check, but of course would be unreliable under many circumstances unless you just click and hold.

The wait with messages is rather important. :)

I did this..
repeat until (the hilite of button "stop")
if the shiftkey is down then exit repeat
put the hilite of button "stop" & random(100000)
wait 0 with messages
end repeat

Notice I added a second out just in case. Loop of death.. ew.

Oh, and you can try and maybe get lucky and hit control-. (control period) to try and break the loop. Klaus, has the engine officially moved to a separate thread yet? If so, control-. should work better than it used to. (or command-. on mac)

Re: LC crashing with repeat

Posted: Fri Jul 10, 2015 1:34 am
by dunbarx
It might be important to know that this is standard abbreviated or shorthand form:

hilite of button "stop"

is the same as:

hilite of button "stop" = "true".

I consider this lazy coding, but the parser does not complain; never did.

Craig Newman

Re: LC crashing with repeat

Posted: Fri Jul 10, 2015 12:35 pm
by Klaus
Hi all,
sturgis wrote:This does actually work..
repeat until (the hilite of button "stop")
It even works with regular buttons if the mouse is down during the check, but of course would be unreliable under many circumstances unless you just click and hold.
OH! :shock:
Sorry, I wasn't aware of this!
sturgis wrote:Klaus, has the engine officially moved to a separate thread yet?
No, not yet.

Best

Klaus

Re: LC crashing with repeat

Posted: Fri Jul 10, 2015 4:21 pm
by jacque
An infinite loop should lock up the script forever but it shouldn't crash. I think the crash is because of this incorrect syntax:

Code: Select all

repeat forever OR repeat until hilite of button "stop"
In this case you don't need two conditions, but when you do, the correct syntax is:

Code: Select all

repeat until (condition 1) or (condition 2)

Re: LC crashing with repeat

Posted: Fri Jul 10, 2015 4:27 pm
by sturgis
Wow, I didn't even see that. (slaps forehead)

Re: LC crashing with repeat

Posted: Fri Jul 10, 2015 4:29 pm
by Klaus
Yesp, I also thought this was just "pseudo code" :D

Re: LC crashing with repeat

Posted: Fri Jul 10, 2015 7:23 pm
by jacque
LC should never crash, but I'm trying to decide if this should be reported or just considered a learning experience.

Re: LC crashing with repeat

Posted: Sat Jul 11, 2015 9:58 am
by SparkOut
I suspect "crash" in this situation is to be interpreted as "locked up application which is unresponsive to keyboard and mouse actions, requiring to be force closed" so unless there is something else going on, I don't believe it is a bug.

Re: LC crashing with repeat

Posted: Sat Jul 11, 2015 10:49 am
by SparkOut
Also for the OP,

in the statement

Code: Select all

repeat forever or until the hilite of button "stop" 
the forever is redundant

Code: Select all

repeat until the hilite of button "stop"
implies that until the condition is met, it will repeat forever. If you need to check for more than one condition, jacque has shown the syntax.

Re: LC crashing with repeat

Posted: Sat Jul 11, 2015 10:56 am
by SparkOut
for the purposes of discussion,

Code: Select all

repeat forever or until the hilite of button "stop"
compiles without error, and will repeat forever, as you might expect "forever or the hilite of ..." to be a boolean test which "forever" naturally resolves to true.

Code: Select all

repeat true
does not compile, however.

Re: LC crashing with repeat

Posted: Sat Jul 11, 2015 11:12 am
by SparkOut
Back to the OP's original question

Code: Select all

repeat until the hilite of button "stop"
      add 1 to field "score"
      wait 3 seconds with messages
end repeat
does exactly what you told it to do.
But that will mean the user is just sitting there waiting for the score to mount up, which it will do until the stop checkbox button is hilited. Processing of any other activity will be interrupted by the repeat loop containing the wait. Wait implies suspension of activity. I believe you will need to use a "send in <time>" construction to do what you need.
In a suitable place (eg the card) have the handler:

Code: Select all

on updateScore
  if not the hilite of button "stop" then
     add 1 to field "score"
     send "updateScore" to me in 3 seconds
  end if
end updateScore
Kick this off in (say) a start button by

Code: Select all

on mouseUp
  send "updateScore" to this card in 3 seconds
end mouseUp
Then your score will update every three seconds as desired, but leave your game actions uninterrupted so the player can actually play it.

not the hilite of button "stop" is "lazy code" for
the hilite of button "stop" is false