Concurrent actions

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Concurrent actions

Post by bjb007 » Wed Mar 19, 2008 1:27 am

I want to alert the user to certain conditions
by flashing the backgroundColor of a field.

As I've coded it everything else stops while
the field is flashing.

Can I let the script get on with other things
while the flashing goes on?

I have three fields and more than one might
need to flash at the same time.
Life is just a bowl of cherries.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Mar 19, 2008 2:39 am

Hi bjb007,

I think you are looking for something like this:

Code: Select all

on flash theCounter
  if theCounter is empty then
    put 1 into theCounter
  else
    add 1 to theCounter
  end if
  if even(theCounter) then
    set the backColor of me to red
  else if theCounter < 5 then
    set the backColor of me to green
  else
    set the backColor of me to white
  end if
  if theCounter < 5 then
    send "flash theCounter" to me in 1 sec
  end if
end flash
If I didn't make a mistake, this script will make a field change colour a few times and eventually return to white. Simply put this into the script of a field and use the following to execute it:

Code: Select all

send "flash" to field "Your Field"
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Concurrent actions

Post by bjb007 » Wed Mar 19, 2008 6:40 am

Mark

I get the field to flash with code like this:

repeat with i = 1 to 5
set backgroundColor of field lbl1 to 255,255,255
wait for 300 milliseconds
set backgroundColor of field lbl1 to 255,125,125
wait for 300 milliseconds
end repeat

Problem is that while this is happening field 2 is
waiting to flash or field 3 or any combination of
two fields.

Is there a simpler method that coding for all
possible combinations so that if two or three
fields are in "flash" condition they flash at
the same time?
Life is just a bowl of cherries.

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post by malte » Wed Mar 19, 2008 10:03 am

You issue a wait command in your code. That is a blocking operation, so no messages come through and the engine halts at the wait command until the specified time is over. Depending on how you call the code snipped you posted here it might work to use

wait 300 millisecs with messages

instead of a regular wait.

Hth,

Malte

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Mar 19, 2008 10:59 am

bjb007,

Any reason why you don't want to use the example I posted?

If really want to use wait with messages, you might do this:

Code: Select all

repeat with i = 1 to 5
set backgroundColor of field lbl1 to 255,255,255
set backgroundColor of field lbl1 to 255,125,125
wait for 300 milliseconds
end repeat 
but your fields would flash simultaneously, which doesn't look as nifty as independently flashing fields. Also, you can't continue to execute your script. It is so much more elegant to send a flash command to a field and not to worry about it anymore while your script does other tasks.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Concurrent actions

Post by bjb007 » Wed Mar 19, 2008 1:45 pm

Mark

Thanks for the nifty 'flash' code. I love it!

I didn't get the concept on first reading
but once I understood that the code went
into the field script.....I was away.

Works great except for the "if even(theCounter)..
which Rev doesn't like. I can't find "even" in
the docs so used a string of items.

Is there a way to use "if theCounter < 5 then"?
It gives the "double binary" error.

Now if I was really clever I might figure out
a way to have only one copy of the 'flash' code
instead of seven (and no doubt will be more).

There are so many places that code can be
put. Handy in this instance but trying to make
sense of someone else's stack can sometimes
be a pain.
Life is just a bowl of cherries.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Wed Mar 19, 2008 4:04 pm

Mark most likely has some function which evaluates to true if a number is even, for example like this:

Code: Select all

function even theNumber
  if theNumber mod 2 = 0 then
    return true
  else
    return false
  end if
end even
Make sure not to put anything but a number into theCounter, most likely that was your problem with "if theCounter < 5".
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Code in a field

Post by bjb007 » Thu Mar 20, 2008 12:07 am

Putting code into a field reminded me
of how sceptical I was the first time I
used a spreadsheet way back in the
days of Ashton-Tate.

Just didn't seem right, possible or
sensible then. It really was something
different. Wonder who came up with
that idea?
Life is just a bowl of cherries.

Post Reply