Page 1 of 2

Needs a 'wait' to work, Why?

Posted: Fri Dec 06, 2013 10:51 pm
by chris25
I was messing around with a simple button and a start button, I wrote this and spent half an hour wondering why the hell it would not work UNTIL I put the "wait 0.5" into this script. I have absolutely no idea what I was doing and no idea why suddenly this script moves the button as planned? What exactly does this wait accomplish please? Thanks. Chris.

Code: Select all

on mouseUp
   put the loc of btn "mine1" into startLoc
   repeat until intersect (btn "mine1" , btn "start") = true
      get the loc of btn "mine1"
      set the loc of btn "mine1" to (item 2 of it + 1) & "," & (item 1 of it)
      wait 0.5
   end repeat
   set the loc of btn "mine1" to startLoc
end mouseUp

Re: Needs a 'wait' to work, Why?

Posted: Fri Dec 06, 2013 11:06 pm
by SparkOut
better still would be

Code: Select all

wait 0 milliseconds with messages
A tight repeat loop will hit the end of the loop and start again, hogging the CPU cycles and leaving the computer unresponsive. A "wait" command, even for 0 specified time (in particular "with messages") will stop the runaway action and give the CPU control back for that moment of "next to zero" duration (as far as can be measured). This means that usual housekeeping tasks, keyboard polling, screen redrawing and the like can be accomplished without the repeat loop demanding every cycle of processor activity.

Re: Needs a 'wait' to work, Why?

Posted: Fri Dec 06, 2013 11:44 pm
by chris25
Thanks sparkout. Just one more question, and I did know tis but have lost the notes. I used to be able to type something into the message box so that wherever I moved the mouse within a stack the message box provided me with the location co-ordinates in real time. Darn it I have tried everything but have forgotten the precise wording for this.

Re: Needs a 'wait' to work, Why?

Posted: Sat Dec 07, 2013 12:29 am
by dunbarx
Chris.

If you check out the "mouseMove" message, can you make this happen? Where in the hierarchy would be the lowest object you could place this?

Craig

Re: Needs a 'wait' to work, Why?

Posted: Sat Dec 07, 2013 12:43 am
by chris25
Craig, obviously in the button, that is the first object in the hiearchy, but on mousedown put mouseloc into msg does not work amongst other things I have tried. I just want to type the command in the message box, I had it there before,j ust can not remember the syntax. To be honest your question confuses me actually, sorry.

EDIT, sorry should have mentioned that I have this ok:
on mousemove
put the mouseloc into msg
end mousemove

but I still do not know how to put this in the message box, which is why I said I did not understand your question - otherwise I am going to appear too dumb, and we can't have that can we :)

Sparkout, your script does not work at all. Mine does. Have no idea why, it's only a button and another button that moves that's it.

Re: Needs a 'wait' to work, Why?

Posted: Sat Dec 07, 2013 3:19 am
by SparkOut
Looking closer, do you want the mine to fall vertically from the top to meet the start button at the bottom?
You should probably

Code: Select all

set the loc of btn "mine1" to (item 1of it ) & "," & (item 2 of it + 1)
if so.
Your repeat loop may well be acting too fast to show the repositioned image without your wait 0.5 - waiting half a tick (approx. 8 milliseconds) is delaying the loop long enough to show each repositioning. Without the delay the whole movement occurs in a blink, which you've probably missed. If you change

Code: Select all

wait 0 milliseconds with messages
to 8 milliseconds (approximately half a tick) do you get the same result?

Re: Needs a 'wait' to work, Why?

Posted: Sat Dec 07, 2013 12:08 pm
by chris25
Hallo Sparkout (keep wanting to type sparky everytime :D ), Firstly my sub game? I still have has the same problems, can't get it to work, too inexperienced, not enough knowledge about working with loops and synchronisation and that true false variable idea that you kept suggestingI can not script it within that particular loop. It simply does not come together for me.

No, this button thing was an experiment to document all the directions with the different calculations, see how I could incorporate this into using this manually with the keyboard like the arrowkey. That's all, just bored! but wanting to learn learn.

The 8 milliseconds runs as expected, like an old 1985 pc rather jumpy. the 0 locked the whole computer as previously tried. An 0.1 has it running very nicely indeed. waiting with messages or leaving it out seems to make no difference at all, but waiting without messages is destructive.
wait 0 milliseconds ============good
wait 0 milliseconds with messages ======good
wait 0.1 milliseconds with messages ======good
wait 0.5 milliseconds with messages ======good
wait 0.1 milliseconds ======good
wait 8 milliseconds ======1985 PC after 10 pints

What I noticed with this and on other occasions is how sometimes a lot of editing (of certain code in certain circumstances whatever they are) seems to upset LC. Last night for example upon closing LC and then re-starting your code worked. As is confirmed this morning now that I have actually slept and am concentrating. This has happened before when once or twice closing and re-starting has re-instated original code that I thought was broken, not a concern really - but just to say why the sudden change.
Hope this answers the questions.

kind regards
chris

Re: Needs a 'wait' to work, Why?

Posted: Sat Dec 07, 2013 12:41 pm
by SparkOut
Sometimes you might need to make sure that edits to scripts are applied and saved before they will take effect? On odd occasions it might need more than one click, or if there is a slight code error it may not apply.

Re: Needs a 'wait' to work, Why?

Posted: Sun Dec 08, 2013 2:41 am
by Simon
Hi Chris,
Last night for example upon closing LC and then re-starting your code worked.
Keep an eye on your pendingMessages, they will happily keep running without your knowledge and keep stacking up.

Code: Select all

   repeat until the pendingMessages is empty 
      cancel item 1 of line 1 of the pendingMessages 
   end repeat
Make sure that is everywhere including closeStackRequest and closeCard (if you want things to stop when you move to another card).

Can you see how the above quote may be related?

Simon

Re: Needs a 'wait' to work, Why?

Posted: Sun Dec 08, 2013 2:56 am
by FourthWorld
chris25 wrote:wait 8 milliseconds ======1985 PC after 10 pints
That's the best description of relative performance I've seen yet. I may steal that for my next article on benchmarking. :)

Re: Needs a 'wait' to work, Why?

Posted: Sun Dec 08, 2013 10:07 am
by chris25
Hi Richard - feel free.

Simon, I knew logically that messages needed to be cancelled, but never knew how. And to be honest after looking up cancel in the dictionary that did not really inspire either. Seeing this in the dictionary just made things worse. Is tMSG the mesaaageID I know it's a parameter, why do you need a repeat here? and so on and so so many questions about this script, if someone could break it down for me I would appreciate it. I understand it yet don't!
ON cancelThisMsg tMsg
put the pendingmessages into tPendingMsgs
IF tMsg is in tPendingMsgs THEN
repeat FOR each line x in tPendingMsgs
if tMsg is in x THEN cancel item 1 of x
end REPEAT
END IF
END cancelThisMsg


repeat until the pendingMessages is empty
cancel item 1 of line 1 of the pendingMessages
end repeat

Now I have to ask Simon, because I just take it for granted, and no where is it really explained, why is everything an item why not just cancel pending messages why the bloated description about lines and items?

Re: Needs a 'wait' to work, Why?

Posted: Sun Dec 08, 2013 10:23 am
by Simon

Code: Select all

ON cancelThisMsg tMsg
See that tMsg, it indicates a specific message.

My code cancels all messages without regard, sometimes you might want to keep some messages going.

Simon

Re: Needs a 'wait' to work, Why?

Posted: Sun Dec 08, 2013 10:24 am
by SparkOut
ON cancelThisMsg tMsg

indicates this is a handler, that you call by the handler name "cancelThisMsg" and you pass it a parameter - the name of the message that you want to cancel, ie you may want to cancel all the "myRepeatLoop" messages that are queued up with cancelThisMsg "myRepeatLoop".
The cancel statement takes a message id.
The pendingmessages returns a list of all the pending messages, line by line. Each line contains comma separated items of information about the message that is pending. Item 1 of the line is the message id.
So the cancelThisMsg handler fetches the pending messages list, then loops through it to check if the "myRepeatLoop" message is in the line, and if it is, it gets the id from item 1 of the line, so the message can be cancelled.

The other script snippet

Code: Select all

repeat until the pendingMessages is empty 
cancel item 1 of line 1 of the pendingMessages 
end repeat
is one which doesn't distinguish the messages you want to cancel, it just flushes out the lot, so you would put something like that in a closeStack handler (for instance, as Simon noted above).
You could make this a handler of its own for general reuse as a library function:

Code: Select all

on flushAllMessages
   repeat until the pendingMessages is empty
      cancel item 1 of line 1 of the pendingMessages 
   end repeat
end flushAllMessages
Then the closeStack (etc) handler could just have the statement flushAllMessages.

Re: Needs a 'wait' to work, Why?

Posted: Sun Dec 08, 2013 11:14 am
by chris25
Hi Simon,
repeat until the pendingMessages is empty
cancel item 1 of line 1 of the pendingMessages
end repeat
I was asking when, if you are cancelling All messages, why do you need to add: cancel item 1 of line 1. I know that the ID of a message is Item 1 in that line, though there are two sets of numbers separated by a comma, but if All messages are being cancelled, then why identify item 1 line 1 when there well may be several lines to be canclled?


Sparkout, now I can see the visual flow of things here much clearer, but that dictionary snippet is different. I follow the dictionary code except for that 'X', why : repeat for each line X in tPendingMessages, when if you are cancelling only one particular message, and the message ID is already stated and given and provided in tMsgs, what is the use of the repeat line? I would have logically seen this as perfectly reasonable with that line omitted, then it makes sense, the addition of that line makes it unintelligible to me.
ON cancelThisMsg tMsg
put the pendingmessages into tPendingMsgs
IF tMsg is in tPendingMsgs THEN
repeat FOR each line x in tPendingMsgs
if tMsg is in x THEN cancel item 1 of x (this line removed makes sense, if you are cancelling only one specific message, because the message to be deleted/cancelled in already in tPendingMsgs)
end REPEAT
END IF
END cancelThisMsg

Chris

Re: Needs a 'wait' to work, Why?

Posted: Sun Dec 08, 2013 11:47 am
by chris25
Ok, maybe my questioning seems dumb and stupid. So allow me to explain what I need to understand with a bit of pseudocode:

ON cancelThisMsg stopAskingQuestions
put the pendingmessages into tPendingMsgs
IF stopAskingQuestions is (amongst the lines of) (to be found in) tPendingMsgs THEN
cancel this line/this message/item
end if
end cancelThis Msg (and chris will go away)


kind regards
chris