| Reference | Downloads | Github

End routine when specified component is selected


I am attempting to use the Builder view to create a task. In each routine, the participant will see a question and have to click on ONE of three options (polygon components with text components inside - these change colour when clicked on). Once they have clicked on their answer (using the mouse component) they will have to click on a tick in the corner of the screen to move on to the next question (this method was chosen to stop them accidentally clicking on a response). The task uses a loop to go through the trials. So far, my code component looks like this:


choice_colours = ([-0.937,-0.937,-0.937], [0.812,0.804,0.804])   #The colours to change the polygon around a selected choice

choices = [
{'name': 'choice_1', 'polygon': Pass1_Q_ResponseA, 'text': Pass1_Q_ResponseA_text,'highlighted' : 0 }, 
{'name': 'choice_2', 'polygon': Pass1_Q_ResponseB, 'text': Pass1_Q_ResponseB_text,'highlighted': 0 },
{'name': 'choice_3', 'polygon': Pass1_Q_ResponseC, 'text': Pass1_Q_ResponseC_text,'highlighted': 0 }
] #This array will be used to loop through all possible answers.

mouse_clicked = Pass1_Q_mouse.getPressed()[0] == 1for choice in choices:
choice['polygon'].fillColor = choice_colours[0]
choice['text'].color = choice_colours[1] # Set visual highlights off.

mouse_previously_clicked = Pass1_Q_mouse.getPressed()[0] == 1 # Set whether mouse is clicked or not at the start of the routine.

highlight_record = [] # List to store the order in which choices are clicked, and their highlight status (see 'End Routine')


mouse_clicked = Pass1_Q_mouse.getPressed()[0] == 1

if mouse_clicked and not mouse_previously_clicked:
highlighted_total = 0 # If mouse button wasn't previously clicked, highlight polygons clicked.

    # Loop through each option available, checking if mouse was clicked within
    # that choice. If it was, toggle its highlighted status and line colour.
for choice_number, choice in enumerate(choices):
    if Pass1_Q_mouse.isPressedIn(choice['polygon']):
        if choice['highlighted'] == 1:
            choice['highlighted'] = 0
            choice['polygon'].fillColor = choice_colours[0]
            choice['text'].color = choice_colours[1]
            choice['highlighted'] = 1
            choice['polygon'].fillColor = choice_colours[1]
            choice['text'].color = choice_colours[0]
        highlight_record.append('{}:{}'.format(choice_number + 1, choice['highlighted'])) # Store order each choice was selected/deselected (see 'End Routine')

    highlighted_total += choice['highlighted']

mouse_previously_clicked = mouse_clicked # We need to check that any clicks are new clicks, otherwise PsychoPy will interpret a held-down mouse button as being a new click every frame. This line and the 'if' at the top fixes this.


for choice in choices:
thisExp.addData('choice_{}_selected'.format(choice['name']), choice['highlighted']) # stores which options were chosen

thisExp.addData('highlight.record.Pass1_Questions', ','.join(highlight_record)) ## Order in which choices were clicked, and their highlight status

thisExp.addData('trials.thisTrialTime.Pass1_Questions', t) # Time taken to complete trial.

if Pass1_Q_mouse.clicked_name[0] =='Pass1_Q_tick':
    P1_EnlargeLoop_1.finished= True # If click tick is clicked, stop the loop

In the above:

Pass1_Q_tick = the name of the tick image that, when clicked, should move the participant to the next question in the loop
P1_EnlargeLoop_1 = the name of the loop
Pass1_Q_mouse = the name of the mouse component
Pass1_Questions = the name of the routine where the questions are displayed

I’m wondering if there is a way that I can ensure the participant clicks on one and only one response? At the moment they can click multiple responses before clicking on the tick icon. They can also just click on the tick button and move on to the next trial without actually answering the question.

I’ve searched online for ways to do this, but I’m now at a bit on a loss - would someone be able to point me in the right direction, please? I THINK there needs to be something added to the End Routine tab…?

Thank you in advance for the help!

Is there a way of doing this? I’m experiencing a similar problem where I want to specify the number of selections respondents can click on before moving to the next trial by pressing a ‘next’ icon. While these selections are recorded in the datafile, ONLY the ‘next’ icon will take respondents to the next trial (regardless of how many buttons the code allows them to press).
I can find information in the help pages outlining how to use time or accuracy to move to the next trial, but not a specified button press.