psychopy.org | Reference | Downloads | Github

Save value when participant clicks in polygon

Hi,

I want to build a Spot the difference trial. I tried to solve this by showing two pictures and on one picture i placed a circle over the differences like here: How to mark something during an experiment?

My problem is when i use a code element with

if mouse.isPressedIn(difference_1)
foundTarget1 = True
elif mouse.isPressedIn(difference_2)
foundTarget2 = True
elif mouse.isPressedIn(difference_3)
foundTarget3 = True

i don’t get anything in the outputfile. I Also tried:

if mouse.isPressedIn(difference_1)
addData(target1, True)

I generally don’t know how to save variables in the Code view, or how to tell psychopy to save data in the output file.
Do you have any examples?
And is it possible to Change the opacity of the circles when the participant klicks in it?

Thanks for your help!

spot_the_difference.py (13.6 KB)

You’re on the right track with addData - but the syntax is:

loopName.addData('name of the column', value) #column doesn't need to be predefined.

It will entirely depend on how you want your output file - so can they only click on one difference or should they be clicking on all difference?

To change opacity you would put $difference_1_Opac in the Opacity box in the polygon properties

At the start of the experiment you would have

difference_1_Opac = 0

You would put this in the end routine tab too so it resets.

Then a total example would look something like:

if mouse.isPressedIn(difference_1):
     foundTarget1 = True
     diff_1_Opac = 1
     trials.addData('Dif_1', foundTarget)

Hope this helps.

Oli

thanks for your answer, but the problem is still existing…

the participants should click on all differences.
i tried what you say, but there’s still no data in the outputfile (cvs or txt) and the oppacity isn’t changing when i click on the circles. i think the code element doesn’t place the code there where it should be…

But your answer helped me

spot_the_difference.py (13.8 KB)

Hmmmm - try moving the trials.addData part to the end of routine section of the core component - also make sure that trials is the name of your loop.

hey oli, thank you for your help!

i moved the addData part to the end (Experimenthandler)

at the beginning of the routine i defined foundtarget

Initialize components for Routine “trial”
foundtarget1 = 0
foundtarget2 = 0
foundtarget3 = 0

Then in the preparation of the trial theres the code for pressing in the circle

if mouse.isPressedIn(difference_1):
foundtarget1 = 1
if mouse.isPressedIn(difference_2):
foundtarget2 = 1
if mouse.isPressedIn(difference_3):
foundtarget3 = 1

after that, the routine trial starts

after ending the routine, psychopy saves data in the ExperimentHandler

store data for thisExp (ExperimentHandler)
x, y = mouse.getPos()
buttons = mouse.getPressed()
thisExp.addData(‘mouse.x’, x)
thisExp.addData(‘mouse.y’, y)
thisExp.addData(‘mouse.leftButton’, buttons[0])
thisExp.addData(‘mouse.midButton’, buttons[1])
thisExp.addData(‘mouse.rightButton’, buttons[2])
thisExp.addData(‘Target_1’, foundtarget1)
thisExp.addData(‘Target_2’, foundtarget2)
thisExp.addData(‘Target_3’, foundtarget3)
thisExp.nextEntry()

it’s still not working… in the outputfile (txt) i can’t find a variable named “Target”

i’m getting deperated :sweat_smile:

Thanks!

spot_the_difference.py (13.8 KB)

Hi @Benjamin_Mader, if you wrap your code in triple-backticks, it will be automatically formatted in a proper way.

For example,

```
if mouse.isPressedIn(difference_1):
    foundtarget1 = 1
if mouse.isPressedIn(difference_2):
    foundtarget2 = 1
``` 

will appear as

if mouse.isPressedIn(difference_1):
    foundtarget1 = 1
if mouse.isPressedIn(difference_2):
    foundtarget2 = 1

instead of

if mouse.isPressedIn(difference_1):
foundtarget1 = 1
if mouse.isPressedIn(difference_2):
foundtarget2 = 1

It would be helpful if you could revise your post accordingly. Thanks!

1 Like

Hello Benjamin, as Richard suggests, you should re-format your code so that we can see if it is something as simple as an indentation error.

But my suspicion is that you have some of the code in the wrong tabs of the code component. If you want to check if a stimulus has been clicked, this really needs to be in the Each frame tab, so it gets checked on every screen refresh during the routine (typically at 60 Hz). It seems you currently have it in the Begin routine tab, which means the check is only done once per routine, and in fact at a time before the stimuli have been displayed, so it isn’t possible for them to have been clicked at that stage.

You should also be saving the mouse button presses at the same time. Doing this again at the end of trial might miss any press, as it has already been detected earlier (at least, that is how keyboard checking works).

1 Like

thanks @richard for the tip with the tripple-backticks! i didn’t saw it in the reply box!

@Michael thanks! the problem was that it just cheked at the beginning! i put it in the every frame tab an it worked after some changes in the code:

i had to put the “foundtarget1 = 0” at the beginning of the experiment some rows up
and “thisExp.addData(‘Target_1’, foundtarget1)” above “thisExp.nextEntry()” because the code element puts these things a few rows too low

So thank you all for your Help! I’m so excited that this is working now!