Wait statement help with until and or

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Wait statement help with until and or

Post by mrcoollion » Sat Jan 13, 2024 12:47 pm

Hello Fellow LC developers,

I need some help.
I cannot seem to get the wait statement to work as I need it to.
What I need is for the application to wait until the variable tResult has data in it or until 10 seconds have passed.

Tried the below but it throws an error during runtime.

Code: Select all

wait until tResult is not empty with number (600) //  (60 ticks = 1 second)
Although I have searched for information I could not find a good example.
Can anyone help me to achieve this?

regards,

Paul

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Wait statement help with until and or

Post by stam » Sat Jan 13, 2024 1:14 pm

I might be wrong, but as I read the dictionary it doesn't seem likely you can both wait until and wait for. It looks like 2 forms of wait and you can do one or the other.

perhaps this works though? (completely untested ;) )

Code: Select all

repeat with x = 1 to 10 // 10 seconds
   if tResult is not empty or x = 10 then exit repeat
   wait 1 second with messages
end repeat

-- code continues as planned after the 'wait' command
Obviously if you need the process to block the interface until tMessage is not empty or until 10 secs have elapsed, just remove the with messages qualifier.
Last edited by stam on Sat Jan 13, 2024 1:20 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Wait statement help with until and or

Post by richmond62 » Sat Jan 13, 2024 1:20 pm

Well: fooling around I made a stack with a button and a field "fBUCKET", and put this into the button:

Code: Select all

on mouseUp
   wait until fld "FBUCKET" is not empty
   repeat until XXX  = 600
      add 1 to XXX
      put XXX into fld "fCOUNT"
      if XXX > 599 then
         put "X" into fld "fBUCKET"
      end if
   end repeat
   put "Here We Go!"
end mouseUp
AND, of course, nothing happened (except I had to force-quit LC) because (although the documentation states that 'wait' is non-blocking) the code AFTER the 'wait' line never happens.

So, trying to be clever (rarely a good idea) I put this into my button:

Code: Select all

on mouseUp
   wait until fld "FBUCKET" is not empty
   put "Here We Go!"
end mouseUp
and, desperately tried to insert a character into field "fBUCKET": not a sausage!

So, stopped using the 'wait' thing and tried this:

Code: Select all

on mouseUp
   repeat until fld "fBUCKET" contains "600"
      add 1 to fld "fBUCKET"
      if fld "fBUCKET" contains "600" then
         put "Here We Go!"
      end if
   end repeat
end mouseUp
So THAT sorted out the 'seconds' bit: obviously you'll need to adjust the repat loop to make sure it cycles every second (which will be a pain because of differing speeds of processors used by end-users).
Last edited by richmond62 on Sat Jan 13, 2024 1:29 pm, edited 1 time in total.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Wait statement help with until and or

Post by stam » Sat Jan 13, 2024 1:27 pm

richmond62 wrote:
Sat Jan 13, 2024 1:20 pm
Well: fooling around I made a stack with a button and a field "fBUCKET", and put this into the button:
That won't do what he OP wants which is to continue code either if tResult is not empty OR if 10 seconds have elapsed.

With your handler, everything will be on hold until field "FBUCKET" is not empty, then start counting time to put something in field "FBUCKET" (seriously dude, WTH is it with your naming conventions...).

EDIT: This was in response to your initial post, but is true of any handler that starts with wait and then starts counting stuff.
Why don't you instead try the handler I posted just before you?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Wait statement help with until and or

Post by richmond62 » Sat Jan 13, 2024 1:32 pm

WTH is it with your naming conventions...
My naming conventions are determined by the fact that it is the weekend, after lunch, and I am on my own, unpaid time.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Wait statement help with until and or

Post by richmond62 » Sat Jan 13, 2024 1:35 pm

As to counting 10 seconds, that is dead easy:

Code: Select all

on mouseUp
   put the seconds into SEX
   repeat until fld "fCOUNT" contains "10"
      put the ((seconds) - SEX) into fld "fCOUNT"
   end repeat
end mouseUp
This:

Code: Select all

on mouseUp
   put the seconds into SEX
   put empty into fld "fCOUNT"
   repeat until fld "fCOUNT" contains "10"
      put the ((seconds) - SEX) into fld "fCOUNT"
      if fld "fBUCKET" is not empty then put 10 into fld "fCOUNT"
   end repeat
end mouseUp
is problematic insofar as while the repeat loop is looping it is NOT possible to put anything into field "fBUCKET".

somewhere way back in the dark crevices of my mind I have a feeling that 'with messages' might be used: but I cannot work out how.
Last edited by richmond62 on Sat Jan 13, 2024 1:44 pm, edited 1 time in total.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Wait statement help with until and or

Post by stam » Sat Jan 13, 2024 1:41 pm

richmond62 wrote:
Sat Jan 13, 2024 1:35 pm
As to counting 10 seconds, that is dead easy:

Code: Select all

on mouseUp
   put the seconds into SEX
   repeat until fld "fCOUNT" contains "10"
      put the ((seconds) - SEX) into fld "fCOUNT"
   end repeat
end mouseUp

Again - and because you've posted so much my initial post has been drowned out with 'SEX' and 'FBUCKETS' (seriously dude...):
It's not about counting time. It's not about checking a condition within a timeframe. It's about doing both. A single wait command can't do that so it needs a loop.


Simplest option:

Code: Select all

repeat with x = 1 to 10 // 10 seconds
   if tResult is not empty or x = 10 then exit repeat
   wait 1 second with messages
end repeat
If blocking interface required, remove the 'with messages' qualifier

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Wait statement help with until and or

Post by richmond62 » Sat Jan 13, 2024 1:48 pm

'SEX' and 'FBUCKETS'
Hmm:

1. I used 'SEX' as if I had used 'SECS' that would have caused problems.

2. I used "fBUCKET" because "f" indicates a field' and "BUCKET" a container.

Your complaints on this front say more about your mind than mine. 8)

AND; when push come to shove, no-one's choice of naming convention should matter: what SHOULD matter is the code that gets the job done.

----

If you want to "pick a fight with me", be my guest, but pick a fight about something less trivial and worth fighting about. :D

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Wait statement help with until and or

Post by stam » Sat Jan 13, 2024 3:37 pm

richmond62 wrote:
Sat Jan 13, 2024 1:48 pm
If you want to "pick a fight with me", be my guest, but pick a fight about something less trivial and worth fighting about. :D
I have exactly zero interest in 'picking a fight with you' as it's not possible to ascribe a negative number to this.
On the other hand, filling a page with weird code does bother me, especially it doesn't actually address the question asked.

But since you raise this as the issue, instead of the solution provided (your code does not actually do what he OP wanted):
It might be better to be more explicit and less 'clever' when sharing code.
It's like 0.5 seconds more effort to write tSeconds instead of SEX, and the former is much clearer in what it means.

Naming conventions DO matter. If you write a book only you will ever read you can use any jumbled scrawls you like as long as you understand them. If the intention is for others to read as well, you have to use the commonly agreed alphabet, syntax and grammar, or there will be confusion.

LiveCode recommends a naming convention in the User Guide. I use the naming convention published by Richard here https://fourthworld.com/embassy/article ... style.html:

Code: Select all

Char          Meaning                Example

g             Global variable	     gMyGlobal
t             Local variable	     tMyVar
s             Script-local var       sMyVar
p             Parameter              pMyParam
k             Constant*	             kMyNumber
u             custom prop	     uMyProp
I doubt the importance of naming conventions can be overstated...
Should I even bother mentioning the recommended capitalisation? (hint: search LiveCode's User Guide for 'camelCase')

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Wait statement help with until and or

Post by richmond62 » Sat Jan 13, 2024 3:49 pm

Probably an effect of teaching primary children programming: we often make physical models of functionality before actually doing some programming on a computer: so what IS a cup/tin/bowl/bucket on the table become a field called "fCUP" and so on in a stack.

Interestingly enough, in that list of naming conventions referenced about, fields are not mentioned.
-
Screenshot 2024-01-13 at 16.51.15.png
-
filling a page with weird code
Apart from my choice of names for a variable and a couple of fields there was NOTHING weird about my code:

unless of course your prejudice about my naming conventions was enough to throw you 'off' completely. 8)

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Wait statement help with until and or

Post by stam » Sat Jan 13, 2024 4:25 pm

Your code is weird because you fixated on one aspect of the ailing wait command given by the OP: counting time - but not checking if a condition was true as well. The OP wanted his code to halt until either a variable was not empty or 10 seconds had elapsed.

30 years ago programmers had to use short variable names like FF - but that is a thing of the ancient past now. Practically every language has guides and they universally recommend you assign names that will make immediate sense when you return to your code a year later, even if they are very lengthy and verbose.

As for the list you copied from Richard's page - yes that is for variables, not fields. The only convention regarding field naming I'm aware of is that it a) it's meaningful and b) capitalisation is camelCase.

The comment of naming conventions was for using SEX instead of something like tSeconds which would have been instantly understandable.
With respect to field naming, like "FBUCKET" - I don't know your stack, all I know is your code. What does this field do or hold? The name provides no context as to what the field is for (or the variable or whatever). The point being that I shouldn't have to figure out what each name means. If you have a field that holds a username, you'll call it username, not myToyCar...

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Wait statement help with until and or

Post by richmond62 » Sat Jan 13, 2024 5:28 pm

Right.
-
HETL.jpg
HETL.jpg (10.9 KiB) Viewed 451311 times
-
Naming Conventions have been discussed several times over the last 20-odd years: NEVER with complete agreement: which is aukward (Oh, and that's the way Henry Fielding spelt 'aukward'), especially if you are hung up on canons and rigid standards.

Now every time I have builded (interestingly enough the form 'builded' was fairly universal in English until the Great War of 1914-18) a stack that is for a bit more than an Andy Warhol Moment on the Forums I have adhered to a naming convention that I, at least, have managed to understand looking at code 15 years later. I have also annotated my code in case, as a rather insensitive lecturer at the University of Abertay (whose Visual BASIC was so bad that several students, including myself, had to keep correcting her) said, "In case you get run over by a bus."

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

Re: Wait statement help with until and or

Post by SparkOut » Sat Jan 13, 2024 7:52 pm

Naming conventions aside, this isn't that big a deal is it?

Code: Select all

   put the seconds into tTime
   wait until (the seconds - tTime >= 10) or (field 1 is not empty) with messages
   answer "Either you put some content into field 1, or 10 seconds have elapsed

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Wait statement help with until and or

Post by richmond62 » Sat Jan 13, 2024 9:21 pm

No, it isn't a big deal (well, big for me as I couldn't work out how to do it).

And my attempts were 'squished' because someone got all hot and sweaty because I did not adhere to a fairly informal naming convention.

But, as I live in the Balkans I know that Balkan people have a tendency not to see the wood because they are looking at the trees.

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

Re: Wait statement help with until and or

Post by SparkOut » Sun Jan 14, 2024 5:49 am

Well, there is cause to wonder about the test involved in the OP's problem.
I put the check "field 1 is not empty" in my snippet, and that works fine. I'm not sure how the OP is trying to obtain tResult. LiveCode is not multithreaded and so the wait (even with messages) may be blocking whatever process the OP uses to obtain tResult.
I am sure there is a better way, but we need to understand what the OP is trying to achieve.

Post Reply

Return to “Talking LiveCode”