What is the best way to implement Wait Mode?

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
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

What is the best way to implement Wait Mode?

Post by sritcp » Thu Jun 28, 2012 6:16 pm

I am writing a children's app. When the child clicks (touches) one of the images on the screen (i.e., the transparent buttons on the images), things happen. When there is no activity from the child for a time, the program prompts by blinking each of the clickable objects in sequence. The way I have programmed it is as follows:

The main loop is:
on mouseUp
respondToTheClick
waitMode
end mouseUp

and the wait loop is:

on waitMode
blinkIfACertainTimeHasElapsed
if the mouseClick then exit waitMode
end waitMode

As you can see, the program is stuck in one mouseUp handler until the next mouseClick. I worry if this could lead to the program hanging. My question is, is there a standard way of implementing the wait mode? (As you can tell, I don't have a background in programming).

Thanks for your time,
Sri.

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

Re: What is the best way to implement Wait Mode?

Post by dunbarx » Thu Jun 28, 2012 6:45 pm

Use the "send" command, with its "in time" variant. This is much more robust than using wait, as it frees the system for other tasks. Never use "wait" unless absolutely required.

on mouseUp
set the noClick of this stack to "" --see below
waitMode
end mouseUp

on waitMode
respondToTheClick --need to set some sort of flag here. See below
doYourBlinkThing --but no waiting anymore, just blink as you think fit
If the noClick of this stack = "" then send "waitMode" to this stack in blinkTime seconds -- BlinkTime is your blink interval. Send "waitMode" wherever you do now,
end waitMode

Now, you have to be able to get out of the successive "waitmode" messages. Perhaps set a custom property (noClick) upon a successful "respondToTheClick", so that the conditional will not fire.Maybe set it to "Clicked" if there was one, and of course empty if not.

You no longer have to exit with a mouseClick, as the handler will simply end if it sees that a click was made.

Untested, but should get you going. Untested means there are conceptual bugs.

Craig Newman

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

Re: What is the best way to implement Wait Mode?

Post by jmburnod » Thu Jun 28, 2012 7:00 pm

Hi Sri

As faster Graig said you need a "send" command.
This script (for a card script) do an answer dialog after 10 seconds without activity. In this case i consider the mousemove event like an activity, but you can use an other event.

Sorry for my english if i'm not clear

Best regards

Jean-Marc

Code: Select all

local sDepTime,sDuration

on opencard
   put the seconds into sDepTime
   put 10 into sDuration
   ShtempsPasse
end opencard

on closecard
   finShtempsPasse
end closecard

on mousemove 
   put the seconds into sDepTime
end mousemove

on ShtempsPasse
   if the seconds > sDepTime + sDuration then
      answer "Time is over" -- or what you want
      put the seconds into sDepTime
   end if
      send ShtempsPasse to me in 1000 milliseconds
end ShtempsPasse

on finShtempsPasse -- stop the pendinmessage
   repeat for each line aLine in the pendingMessages
      if aLine contains "ShtempsPasse"  then 
         cancel item 1 of aLine
      end if
   end repeat
end finShtempsPasse
https://alternatic.ch

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: What is the best way to implement Wait Mode?

Post by sritcp » Thu Jun 28, 2012 9:33 pm

Hi Craig and Jean-Marc:

Thank you both for your responses.
Craig, your code
If the noClick of this stack = "" then send "waitMode" to this stack in blinkTime seconds
checks the condition of no click and then waits, say, 30 seconds before sending the waitMode message to the stack; what happens if there is a click in those 30 seconds? The noClick would have updated, but IF clause won't check it again, so the code won't work (or am I wrong here?).

I agree with you about avoiding the use of the "wait" command. Here's a short piece of code incorporating the "send" command:

Code: Select all

on waitMode
   if the seconds - gLastClickTime > 30 then  -- if more than 30 seconds since the last click
      repeat with i = 1 to the number of buttons of this card
         blinkObject button i of this card -- blinks each card once
         if the mouseClick is true then exit waitMode -- if user clicked anytime, stop serial blinking and respond
      end repeat
      put the seconds into gLastClickTime -- reset the clock
   end if
   if the mouseClick is true then exit waitMode -- this checks for clicks between blinking episodes
   send "waitMode" to this stack
end waitMode
Jean-Marc,
thanks for your code. I have a question. You are cleaning up the message cache on closeCard. Why is it necessary? Wouldn't all the card messages running automatically terminate and message cache empty when you close a card?

Thanks,
Sri.

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

Re: What is the best way to implement Wait Mode?

Post by jmburnod » Thu Jun 28, 2012 9:42 pm

Hi
You are cleaning up the message cache on closeCard. Why is it necessary?
I'm not sur it is necessary.
Thanks for your time
Welcome. I try to give what i have received from this forum
https://alternatic.ch

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: What is the best way to implement Wait Mode?

Post by townsend » Thu Jun 28, 2012 10:18 pm

I don't know if this will help or not, but I've got something like this in my app.
Of course I made a few modifications to reflect you situation.

Basically you've got two global fields" sec.cntr & timer.on.
When you want to start waiting, you put true in timer.on,
and then call on.blinktimer. Then after 60 seconds,
you simply pass the image.name, where you use a single
function to set the visible on and off, to the blinking,
for any image, as need be.

Code: Select all

do blink.timer
     global sec.cntr, timer.on
     add 1 to sec.cntr
     if sec.cntr >= 60 then --wait 60 seconds before blinking
          put zero into sec.cntr
          call start.blinking(image.name)
     end if
     if timer.on then
          send "blink.timer" to me in 1 second
     end if
end blink.timer
Last edited by townsend on Fri Jun 29, 2012 12:51 am, edited 1 time in total.

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: What is the best way to implement Wait Mode?

Post by sritcp » Thu Jun 28, 2012 10:39 pm

Hi Townsend:

It is not just waiting, but waiting while there is no click. So, the routine has to exit when there is a click.
In any case, in the code I have given above, I, too, have used a global variable as timer. It seems to work.

Regards,
Sri.

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

Re: What is the best way to implement Wait Mode?

Post by dunbarx » Fri Jun 29, 2012 2:10 pm

Quite.

Why not send the "waitMode" command in 0 seconds, and create a timer?

Conceptually, the "send in time" command creates a "repeat loop", but through an entire handler as a whole. If you pass the seconds as a parameter each time you send, and check that value with a preset time, you can send continuously. You can probably do this the other way around, sending the preset, and checking the seconds. The blink would only occur after the preset time was passed. In the clickChecking and blink handlers, you can reset the timer.

Untested, so guaranteed to have conceptual issues.

Craig Newman

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

Re: What is the best way to implement Wait Mode?

Post by dunbarx » Fri Jun 29, 2012 4:20 pm

I extracted this from a gadget I made a while ago.

Code: Select all

  on waitMode baseTime
  repeat until the seconds > baseTime + 5 --your value here
            checkForClick
         end repeat
    doYourBlinkThing
      put the seconds into baseTime
   send "waitMode baseTime" to this stack in 0 millisec
Untested, as usual. This should be considered pseudocode.

Craig Newman

Post Reply