send command with two tasks

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
JoshW
Posts: 2
Joined: Mon Oct 22, 2012 8:33 pm

send command with two tasks

Post by JoshW » Mon Oct 22, 2012 8:48 pm

Hello! I have been using LiveCode for about 6 months and it has been going really well. I have programmed several types of programs, but I recently ran into a problem (that I eventually solved) but the solution seems to be a less than efficient way to do it.

I need to run two tasks in an experiment simultaneously. In one task subjects are presented a list of words, one word at a time every 3 seconds. At the exact same time subjects are hearing single digits spoken every 1 second through the "say" command for applescript (they have to press a key if they hear 3 odd digits in a row).

Here is a simplified version of the method that I did that currently "works" but may not be the best method:

on task
send sayone to me in 1000 milliseconds
send saytwo to me in 2000 milliseconds
send "put factory into me" to me in 3000 milliseconds
send saythree to me in 3001 milliseconds
send sayfour to me in 4000 milliseconds
send sayfive to me in 5000 milliseconds
send "put tower into me" to me in 6000 milliseconds
end task

on sayone
do item 1 of line 1 of fld "Say Digits" as applescript
end sayone

on saytwo
do item 1 of line 2 of fld "Say Digits" as applescript
end saytwo

....

Note that here "me" = a fld, and that I am calling the "do" commands from another field with a list of the applescript commands

Any ideas on how to improve this method please let me know, otherwise it's going to be a while before I get this program done haha!

Thanks!
Josh

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: send command with two tasks

Post by mwieder » Mon Oct 22, 2012 9:10 pm

Josh-

What you've got will work, of course, but I think I'd approach it a little differently.
I'd start with two lists, one for the words to be spoken and one for the visible lists...

Code: Select all

local sCurrentDigit
local sCurrentWord

on task
  -- fill fields here if they're not already filled
  put 1 into sCurrentDigit
  put 1 into sCurrentWord
  sayDigit
  showWord
end task

on sayDigit
  if line sCurrentDigit of field "Say Digits" is empty then
    exit sayDigit
  end if
  do item p1 of line sCurrentDigit of field "Say Digits" as applescript
  add 1 to sCurrentDigit
  send "sayDigit" to me in 1 second
end sayDigit

on showWord
  if line sCurrentWord of field "fldWords" is empty then
    exit showWord
  end if
  put line sCurrentWord of field "fldWords" into me
  add 1 to sCurrentWord
  send "showWord" to me in 3 seconds
end showWord

JoshW
Posts: 2
Joined: Mon Oct 22, 2012 8:33 pm

Re: send command with two tasks

Post by JoshW » Tue Oct 23, 2012 3:19 pm

Great! After a couple of minor changes to your suggested code it worked perfectly.

Thanks so much!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: send command with two tasks

Post by mwieder » Tue Oct 23, 2012 5:04 pm

Cool. :D

I think this sort of situation (repeating asynchronous events) is where the "send in time" statement really comes into its own.

Post Reply