Page 1 of 1

Continue code reading while a repeat loop is running.?

Posted: Tue Jul 19, 2011 8:39 pm
by doobox
Hi there,

This i am sure is pretty basic stuff, but something i don't yet know.

I want to run this loop while the condition is true... BUT..!
I want to continue on reading the next lines of code while the loop runs away on its merry dance so i can eventually
set the condition to false.

Code: Select all

      put true into tTheRingGirlIsOn
      repeat while tTheRingGirlIsOn = true with messages
         put random(320) into randX
         put random(200) into randy
         move graphic "flash" to randX,randy without waiting
         set the visible of graphic "flash" to true
         wait 10 milliseconds
         set the visible of graphic "flash" to false
         wait random(300) milliseconds
      end repeat
      ## so at the moment this line is never read, i am in a infinite loop
      play specialFolderPath("engine") & "/boxing/sounds/clapping.wav"
      wait 3 seconds
      play specialFolderPath("engine") & "/boxing/sounds/BoxingBell.wav"
      move group "ringgirl" from 154,252 to -70,252 in 30 ticks
      put false into tTheRingGirlIsOn  ## i need to get to here to break the loop

Re: Continue code reading while a repeat loop is running.?

Posted: Tue Jul 19, 2011 10:35 pm
by dunbarx
Hi.

You will not be able to get outside that loop by any means outside the loop. I hope that is not too convoluted. You have to test the condition inside the loop to exit it.

What changes the condition anyway? I don't see it. I do see that when the loop has finished, the variable is reset, and you are on your merry way, as you say.

In other words, say you could, from the outside, change tTheRingGirlIsOn to "false" while the loop is running on its own merits. Why can't you do whatever that is inside the loop itself?

Craig Newman

Re: Continue code reading while a repeat loop is running.?

Posted: Tue Jul 19, 2011 10:47 pm
by doobox
Hi Craig,

Nothing currently changes the condition, as the code does not continue reading past the loop.
My inexperience while writing that chunk, had thought the condition would change when the:
put false into tTheRingGirlIsOn was read thus breaking the loop.

I believe that this would be called Asynchronous.
I am guessing live code can't do this by your response..?

I am sure if i start thinking outside the box, i will be able to do this, but i sure would like a pointer in the right direction.
Because where i am at is with the mindset that two things can't be happening at the same time in Livecode.
Looping that function in any shape or form while conducting business with other code on the page.

Re: Continue code reading while a repeat loop is running.?

Posted: Tue Jul 19, 2011 10:50 pm
by doobox
In other words, say you could, from the outside, change tTheRingGirlIsOn to "false" while the loop is running on its own merits. Why can't you do whatever that is inside the loop itself?
Because that can't be be false until some other code has been run outside the loop after the loop has started.
Back to that Asynchronous thing again :-)

Re: Continue code reading while a repeat loop is running.?

Posted: Tue Jul 19, 2011 11:06 pm
by doobox
let me put this another way:

If i did this as an example:

repeat for 10 seconds
## too some crazy stuff here
end repeat
ask "how long did you wait" with "OK" ## when does this fire..? right away..? or in 10 seconds..?

Any way to make it fire right away..?

Re: Continue code reading while a repeat loop is running.?

Posted: Tue Jul 19, 2011 11:35 pm
by dunbarx
Hmmm.

I bet you can rewrite this to make your conditions available to the loop.

Please check out, and experiment with, the "send" command, especially its "in time" variant.

This technique is invaluable when one is trying to perform a repetitive task, yet leave control with a running handler. It can be used to create a pseudo repeat structure. As an example, create a button, and put this into its script:

Code: Select all

on mouseUp
   put 0 into msg
      startTimer
end mouseUp

on startTimer
   add 1 to msg
   if msg < 15 then
      send "startTimer" to me in 10
   end if
end startTimer
This is an example of a repetitive action that uses no repeat structure. It should be clear that a lot of other code can be placed here and there, and no conditions are explicitly required to be tested. This is a very powerful tool.

Craig Newman

Re: Continue code reading while a repeat loop is running.?

Posted: Tue Jul 19, 2011 11:42 pm
by SparkOut
Try moving the repeating stuff into its own handler and making recursive calls with "send in <time>"

Code: Select all

-- make tTheRingGirlIsOn a script local variable so it can be accessed from different handler on the same script
      
      local sTheRingGirlIsOn
      
      <blah> 
      put true into sTheRingGirlIsOn
      send doTheRingGirlThing to this <object> in 1 millisecond
      --substitute <object> with "me" or whatever object for which this script will be in the message path
      play specialFolderPath("engine") & "/boxing/sounds/clapping.wav"
      wait 3 seconds with messages
      play specialFolderPath("engine") & "/boxing/sounds/BoxingBell.wav"
      move group "ringgirl" from 154,252 to -70,252 in 30 ticks
      put false into tTheRingGirlIsOn
      <blah>
      
      on doTheRingGirlThing
         local randX, randY
         move graphic "flash" to randX,randY without waiting
         set the visible of graphic "flash" to true
         wait 10 milliseconds with messages
         set the visible of graphic "flash" to false
         if sTheRingGirlIsOn is true then
              send doTheRingGirlThing to me in random(300) milliseconds
         end if
      end doTheRingGirlThing
EDIT: a case of submitting without refreshing again, sorry Craig

Re: Continue code reading while a repeat loop is running.?

Posted: Wed Jul 20, 2011 4:04 am
by dunbarx
Sparkout:

LiveCode is never having to say you're sorry.

Craig

Re: Continue code reading while a repeat loop is running.?

Posted: Wed Jul 20, 2011 8:46 am
by doobox
Thank's guys,

This puts a great new powerful tool in my arsenal :-)

I just needed to tweak that last code a tiny bit:

Code: Select all

send doTheRingGirlThing to me in random(300) milliseconds
Needed to be:

Code: Select all

send doTheRingGirlThing to this card in random(300) milliseconds
Thanks again.

Re: Continue code reading while a repeat loop is running.?

Posted: Wed Jul 20, 2011 11:29 am
by Klaus
Hi all,
dunbarx wrote:Sparkout:
LiveCode is never having to say you're sorry.
Craig
is this the "Poetry today" forum? :lol: :lol: :lol:

Re: Continue code reading while a repeat loop is running.?

Posted: Wed Jul 20, 2011 4:45 pm
by dunbarx
Klaus.

You never saw the very first chick flick????

"Love Story". I won't spoil it for you.

Craig

Re: Continue code reading while a repeat loop is running.?

Posted: Wed Jul 20, 2011 5:02 pm
by Klaus
Craig,

I've watched EVERY (remarkable) movie since the 70s!
But that one is not necessarily my favourite 8)


Best

Klaus