| Reference | Downloads | Github

Conditionally repeat trial in touchscreen task

Hi all,

Back again! So I’m working my way through a number of issues I bump into with my task which I’ve been posting about on here. To give some overview, I’m using a Windows 10 laptop/tablet hybrid. I’ve created a touchscreen task to be used with children where they tab on different stim to give their answer. Because of the multitouch aspect of my screen, I had to use the code component provided by Michael in this thread to establish when stim was tapped on:

Now I need a way to keep repeating a trial until a participant gives the correct answer. I also need a way of playing conditional audio files dependent on whether or not a correct or incorrect answer was given.

So to clarify:

  • Incorrect answer given. Play “incorrect answer” audio. Repeat this trial.
  • Correct answer given. Play “correct answer” audio. Move onto the next trial.

I’ve already got the trial ending only on a “correct” answer tap but have been failing to proceed with the rest of this.

I’m still pretty new to PsychoPy and coding languages so any advice would be appreciated :slight_smile:

OK, you need to surround that trial with another loop (i.e. nested inside your main trial loop, only encompassing this particular trial, and not linked to a conditions file). Give that inner loop a relatively high nRep value, which is higher than any likely number of failed trials (e.g. 99). Uncheck the is trials setting on that loop dialog box (as effectively this loop is running within your main trial structure, presumably controlled via one repetition of your outer loop). If you don’t do this, the data output structure will be very confusingly structured (i.e. you won’t get one row per trial, but some variable number).

A successful click will terminate the current routine as well as end the loop. An incorrect click will just end the trial (sending the participant back to another repetition of that trial). On a correct response, you should also record the iteration number of that inner loop, so you know how many incorrect responses there were before the eventual correct one (will be 0 if there is a correct response on the first iteration):

# run on every frame:
for stimulus in stimuli:
    if stimulus.contains(mouse): 
        if == corrAns: # correct response
            thisExp.addData('n_incorrect_responses', your_inner_loop_name.thisN)
            continueRoutine = False
            your_inner_loop_name.finished = True
        else: # incorrect response
            continueRoutine = False
        break # stop checking after the first detected stimulus (in case of overlaps)

Hi Michael,

Thanks for your quick response. I’ve integrated your suggestion (tried something similar myself earlier but not as neatly!) but the issue is that the whole experiment just ends regardless of whether I give an incorrect or correct response. I’m not receiving any errors though.

I’ve attached some printscreens just in case there’s something glaringly obvious I’m doing wrong (temporarily leaving the audio out until the conditional ending is fixed).

Thanks again!

  • You don’t have the mouse component set to end the routine, do you? That could conflict with the custom code.
  • We probably need to see the dialog box settings for the inner loop.
  • It’s not clear what you mean by " the whole experiment just ends". Do you mean that only one trial is ever presented?
  • perhaps insert some debugging code, like:

Hi Michael,

In regards to the mouse, I have it set like so:

The inner looks like this:

By “the whole experiment” I mean that only one trial is presented and that regardless of response, it ends after only presenting that one trial.

Ahh, this took some thinking about. I suspect that the inner loop is indeed repeating, but after you move the mouse into either an incorrect stimulus, the next iteration immediately occurs, but the mouse is still in same location, so the next iteration occurs and so on. 99 iterations would happen in about 1.65 s on a 60 Hz screen.

But that should still take you on to the next iteration of the outer loop, which should also happen immediately for the correct responses. So I’m not sure if this is the issue. If it is, then I suspect we need to alter the code to detect when the mouse is not pressed in the stimulus. This is straightforward with a mouse, but not necessarily with a touchscreen. i.e. what happens to the mouse location when a press is not longer detected? Does it reset to some null value, or does it stay at its last value.

Actually for your task, I think we need to do things differently. Do you have a stimulus that can serve as a start point? i.e. the trial will only proceed once the participant has pressed within a “start” stimulus. That way, we wouldn’t get multiple successive target presses from a single touch. Does that make sense?

Appreciate you taking time thinking about this. The touchscreen element of this task has been a challenge…

Actually, I can introduce something like a start point stimulus. I have a couple of options that I can put in that shouldn’t change too much. 1) There is an example stimulus (not currently clickable) that is in the centre of the screen that participants are trying to match to. It could be they have to click this first. 2) I could create a “START” text that kids would have to click on to the start the trial.

Either that, or I was wondering if there is a way to reset the location of the mouse at the start of every routine or frame to a certain location on the screen? Like I said, the central stimuli isn’t clickable so this could be a good location. Not sure if this would work (or even makes sense!) or if your suggestion is an easier way of achieving this.