Improper recording of clicks when using a timer

OS : MacOS Sonoma
PsychoPy version: 2022.2.4
Standard Standalone? (y/n) Yes
What are you trying to achieve?:
I am creating a task in which a set of stimuli must be clicked in a specific order. If the user clicks the correct stimulus (from a set of clickable stimuli displayed on the screen), then the stimulus just clicked should be set to .5 opacity and the mouse should not accept any input for 3 seconds, after which opacity is reset and the mouse is again able to click. If the user clicks the incorrect stimulus, the routine should end. This must be repeated for a total of 4 stimuli.
The issue I am having is that if users rapidly and repeatedly click the stimuli during the 3 second delay or immediately after it, the clicks are not recorded when they should be. The stimulus will still reduce in opacity and nothing will be clickable, but the click is not actually logged in mouse.clicked_name, so the routine ends incorrectly.

What did you try to make it work?:
The code I am using currently is:

#Begin routine:
clickableStims = [pinkOne, pinkTwo, pinkThree, pinkFour] #clickable stimuli for mouse is set equal to this array
for stim in clickableStims:
    stim.opacity = 1

clickedStims = []

timer = core.Clock()

#Each frame:
#record clicks
for stim in clickableStims:
    if mouse.isPressedIn(stim):
        clickedStims.append(stim.name)
        timer.reset()
        if timer.getTime() < 3:
            clickableStims = []
            stim.opacity = .5

if timer.getTime() >= 3:
    clickableStims = [pinkOne, pinkTwo, pinkThree, pinkFour]
    for stim in clickableStims:
        stim.opacity = 1
    #playFirstTouch = []

#Check correctness
if len(mouse.clicked_name) == 1 and mouse.clicked_name[0] != 'pinkOne':
    incorrect_at_first += 1
    number_incorrect +=1
    for stim in clickableStims:
        stim.opacity = 1
    continueRoutine = False

#correct check for stims 2 and 3 removed for brevity, see github for full

#end
if len(mouse.clicked_name) == 4 and mouse.clicked_name[3] != 'pinkFour':
    continueRoutine = False
    for stim in clickableStims:
        stim.opacity = 1
elif len(mouse.clicked_name) == 4 and mouse.clicked_name[3] == 'pinkFour':
    continueRoutine = False
    for stim in clickableStims:
        stim.opacity = 1

I will also share a link to the code on github here.
(Green numbers should be clicked in ascending order, pink in descending)

I have tried adding a flag that is set to true/false so that the mouse being pressed is tied to it, which shouldn’t allow it to continue without registering the click; putting the if statements for timer.getTime() in different places; switching to while loops.
What specifically went wrong when you tried that?:
With each of these attempts, the error has persisted. The experiment runs exactly the same as before.

Hi @E_T,

I downloaded your experiment (thanks for sharing a github link!) and took a look.
When I tried your experiment without changing anything (although note I am running v2024.1.5) I got the following error the first time I clicked an item:
UnboundLocalError: local variable 'incorrect_at_first' referenced before assignment

Is this the error you are getting as well? If so, I think the fix is simply to move all the code in the startScreen routine’s startScreencode from Before Experiment to Begin Experiment. Generally, it is recommended to initialize variables in Begin Experiment (see Code Component — PsychoPy v2024.2.3).

You may still need to make other changes to the code to get the full behavior you want, but that will hopefully get you past the first error.

Hope that helps,
shabkr

1 Like