Help with experimental code?

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
beejer123
Posts: 4
Joined: Mon Apr 16, 2012 12:24 am

Help with experimental code?

Post by beejer123 » Mon Apr 16, 2012 12:50 am

Hey all!
I am trying to program a reaction time experiment:
I want the card background to change colors, the color of the target + to change colors and the stimulus oval to change colors according to the colors set out in my field. I want the participants to press r, y, or b depending on the color of the oval and record the reaction times and export them to a data file. In addition, I want there to be 4 blocks, 25 trials per block but I am not sure how to do this.

Here's what I have so far, and I would attach the file but it won't accept .livecode extensions. I know many, if not all, could code this in about 5 minutes, but as I am a beginner, it has taken me several hours. This is my script for card 3

on keydown w
put tab & the milliseconds into rtime #The first thing we do is record the milliseconds into a variable
if w is in "r b y" then
put rtime & tab & return after field data of card 3 #if it is, we record the milliseconds into the data field, then include a return to start the next line
hide field tdisplay of card 3 #we immediately hide the target display field
wait 500 milliseconds
send mouseUp to button Begin #then, have the computer trigger the next trial by sending a click to the start button
end if
hide button Begin
end keydown

set "+" to target
set Oval to stimulus

set word 1 of each line in field bgcolorandtcolor of card 3 to backgroundColor
set word 2 of each line in field bgcolorandtcolor of card 3 to target color
set word 3 of each line in field bgcolorandtcolor of card 3 to stimulus color

put "+" in field tdisplay
wait 500 milliseconds
put Oval in field tdisplay
wait 500 milliseconds
hide Oval

repeat 4 times
sort lines of field bgcolorandtcolor by random(the number of lines in field bgcolorandtcolor)
put field bgcolorandtcolor & return after field trials of card 3
end repeat

on MouseUp
put the filename of this stack into fpath
set the itemdelimiter to "/"
delete the last item of fpath
put word 1 of field sname of card 1 into sname
put field data of card 3 into thedata
put thedata into url("file:"&fpath&"/data/"&sname&".txt")
end MouseUp

This is what's in my field bgcolorandtcolor
blue red blue
blue red yellow
blue red red
blue yellow yellow
red blue red
red blue blue
red blue yellow
red yellow yellow
yellow blue blue
yellow red red
yellow blue red
yellow blue blue

Thanks! :D I hope that someone could help with this and get it functional in the next 18 hours or so from the time of this post?!

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

Re: Help with experimental code?

Post by dunbarx » Mon Apr 16, 2012 3:07 am

I would have to build a model of this to debug it, but one thing I notice right off the bat is that the middle part of your code resides in no handler. You have a keyDown and mouseUp handler, but where does that segment live?

Is it too much to hope that this is the problem? You never actually said what was going wrong.

Write back...

Craig Newman

beejer123
Posts: 4
Joined: Mon Apr 16, 2012 12:24 am

Re: Help with experimental code?

Post by beejer123 » Mon Apr 16, 2012 3:27 am

Hey! Thanks for your reply.

So basically, the experiment does not run as the target and stimulus does not appear etc. The colors don't change. I placed the keydown and mouseup in the card script of card 3. Is that what you mean? The keyDown refers to when I want the participant to respond to r b or y on card 3.

Is there any way I can upload my livecode so you could inspect it? The basic way I've written it, if those two elements had a handler, do you think it would function? I know that I don't have blocks or trials scripted yet either.

Thanks,
Genevieve
Sorry for the long response time, I lost wifi for a bit.

p.s. how long does it take an experienced coder like you to code a functioning model of this experiment?

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

Re: Help with experimental code?

Post by dunbarx » Mon Apr 16, 2012 3:57 pm

I can't find the milk in the refrigerator.

What I meant was that the snippet you sent had code within a "mouseUp" handler at the bottom, and code within a "keyDown" handler at the top, BUT many lines in the middle that were not contained in any handler at all.

These lines have no basis for the engine to access them. No message will "see" them if they are not part of a handler.

You seem to have a very good grasp of many LC language elements. Some of your lines, though, are wrong (set "+" to target). Many new users try to leapFrog the learning process, (with varying degrees of success) so I will ask you: Do you know what a script is, and what a handler is? A message?

If you do, then tell me why there is none for that middle section of your code. If not, then, well, tell me that, too.

Craig Newman

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Help with experimental code?

Post by sturgis » Mon Apr 16, 2012 4:20 pm

Just to make what craig is asking slightly more clear see the following.

This all goes together. On keydown has an ending keydown. The engine knows to fire this off when a key press occurs.

Code: Select all

on keydown w
   put tab & the milliseconds into rtime   #The first thing we do is record the milliseconds into a variable
   if w is in "r b y" then                     
      put rtime & tab & return after field data of card 3   #if it is, we record the milliseconds into the data field, then include a return to start the next line
      hide field tdisplay of card 3                 #we immediately hide the target display field  
      wait 500 milliseconds            
      send mouseUp to button Begin     #then, have the computer trigger the next trial by sending a click to the start button
   end if
   hide button Begin
end keydown
This is just code floating around. The engine has no clue what to do with it. No begin, no end, how is the engine supposed to know when to execute these lines of code?

Code: Select all

set "+" to target
set Oval to stimulus

set word 1 of each line in field bgcolorandtcolor of card 3 to backgroundColor 
set word 2 of each line in field bgcolorandtcolor of card 3 to target color
set word 3 of each line in field bgcolorandtcolor of card 3 to stimulus color

put "+" in field tdisplay
wait 500 milliseconds
put Oval in field tdisplay
wait 500 milliseconds
hide Oval

repeat 4 times
      sort lines of field bgcolorandtcolor by random(the number of lines in field bgcolorandtcolor)
      put field bgcolorandtcolor & return after field trials of card 3
end repeat
   
Then of course this section goes together like the top section. The engine knows to execute this when the mouse is released.

Code: Select all

on MouseUp
   put the filename of this stack into fpath
   set the itemdelimiter to "/"
   delete the last item of fpath
   put word 1 of field sname of card 1 into sname 
   put field data of card 3 into thedata
   put thedata into url("file:"&fpath&"/data/"&sname&".txt")
end MouseUp

beejer123
Posts: 4
Joined: Mon Apr 16, 2012 12:24 am

Re: Help with experimental code?

Post by beejer123 » Mon Apr 16, 2012 4:50 pm

Okay I think I understand a little better. I do know what a script is, a decent understanding of a handler, and a message that tells the handler what to do with a specific event. I've read the manual to get a better sense of things. I initially had started learning python, and then for a class had to switch to try to learn LC. I don't have much practice with it yet.

So yes, some of my script doesn't have a home. Thanks for clarifying. Would you group the stimulus presentation together on something like on openCard or something? And do you know how I can set a symbol like + to a target or is that not possible?

Thanks again for your help!

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

Re: Help with experimental code?

Post by Mark » Mon Apr 16, 2012 4:55 pm

Hi,

As Craig has pointed out already, the engine will never execute the code

Code: Select all

    set "+" to target
    set Oval to stimulus

    set word 1 of each line in field bgcolorandtcolor of card 3 to backgroundColor
    set word 2 of each line in field bgcolorandtcolor of card 3 to target color
    set word 3 of each line in field bgcolorandtcolor of card 3 to stimulus color

    put "+" in field tdisplay
    wait 500 milliseconds
    put Oval in field tdisplay
    wait 500 milliseconds
    hide Oval

    repeat 4 times
          sort lines of field bgcolorandtcolor by random(the number of lines in field bgcolorandtcolor)
          put field bgcolorandtcolor & return after field trials of card 3
    end repeat

because it is not inside a handler. You must write all code always inside a handler. Handlers are command handlers (on, command), function handlers, getProp handlers or setProp handlers. If you don't put your code inside any of these, then your code will never run.

You can create a handler to catch a mouseUp message with

Code: Select all

on mouseUp
  -- put your code here
end mouseUp
You can create a custom handler by giving it your own name

Code: Select all

on customHandlerBlaBla
  -- put your code here
end customHandlerBlaBla
or

Code: Select all

getProp customPropHandlerFooBar
  -- put your code here
end customPropHandlerFooBar
et cetera. You can call your custom handlers from other custom handlers or from handlers that catch system messages:

Code: Select all

on mouseUp
  customHandlerBlaBla
end mouseUp
or e.g.

Code: Select all

on anotherCustomHandler
  get the custompropHandlerFooBar of me
end anotherCustomHandler
To answer your latest questions, you can put your code inside a custom handler, e.g. in the stack script, and call that handler from any object in that stack.

Words such as "target" (without "the" in front of it), "target color" and "stimulus color" will cause compilation or execution errors. Your code won't run as it is.

Kind regards,

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

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Help with experimental code?

Post by Klaus » Mon Apr 16, 2012 5:17 pm

Hi beejer123,

welcome to the forum! :D

Please check these stack to get the basics of LiveCode:
http://www.runrev.com/developers/lesson ... nferences/


Best

Klaus

beejer123
Posts: 4
Joined: Mon Apr 16, 2012 12:24 am

Re: Help with experimental code?

Post by beejer123 » Mon Apr 16, 2012 6:06 pm

Thanks for all y'alls help. I didn't get it fully working but good enough for now. Thanks, Genevieve

Post Reply