LC crashing with repeat

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sinep
Posts: 12
Joined: Sat Jun 13, 2015 5:19 am

LC crashing with repeat

Post by sinep » Thu Jul 09, 2015 10:43 pm

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 :)

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: LC crashing with repeat

Post by Klaus » Thu Jul 09, 2015 10:58 pm

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

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

Re: LC crashing with repeat

Post by sturgis » Fri Jul 10, 2015 1:25 am

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)

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

Re: LC crashing with repeat

Post by dunbarx » Fri Jul 10, 2015 1:34 am

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: LC crashing with repeat

Post by Klaus » Fri Jul 10, 2015 12:35 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: LC crashing with repeat

Post by jacque » Fri Jul 10, 2015 4:21 pm

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)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: LC crashing with repeat

Post by sturgis » Fri Jul 10, 2015 4:27 pm

Wow, I didn't even see that. (slaps forehead)

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: LC crashing with repeat

Post by Klaus » Fri Jul 10, 2015 4:29 pm

Yesp, I also thought this was just "pseudo code" :D

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: LC crashing with repeat

Post by jacque » Fri Jul 10, 2015 7:23 pm

LC should never crash, but I'm trying to decide if this should be reported or just considered a learning experience.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: LC crashing with repeat

Post by SparkOut » Sat Jul 11, 2015 9:58 am

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.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: LC crashing with repeat

Post by SparkOut » Sat Jul 11, 2015 10:49 am

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.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: LC crashing with repeat

Post by SparkOut » Sat Jul 11, 2015 10:56 am

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.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: LC crashing with repeat

Post by SparkOut » Sat Jul 11, 2015 11:12 am

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

Post Reply