Loop not working online

URL of Experiment: Sign in · GitLab
(I don’t how to share it, without making it public. I made it public once and then all the codes disappeared).

Description of the Problem:

I am running an experiment involving a movie-watching and keypress task. The experiment includes watching three movies, which are repeated 2-3 times based on specific conditions. While the experiment works perfectly fine locally, I am encountering issues when running it online on Pavlovia. There are two loops with same routines. First loop works but the second doesn’t.

Experiment Setup:

  • The experiment has two conditions. Both conditions share the same stimuli and routine flow, with only the instructions differing between them.
  • Each condition starts with an instructions routine, followed by a practice round. During the practice round:
    • A practice video is played (file size: 350 MB).
    • Based on the number of keypresses recorded during the video, participants receive feedback.
    • If the keypresses are either below or above a predefined range, the participant is required to repeat the video. This is communicated via a feedback message.
    • The video can only be repeated once.

Implementation Details:

  • To achieve the behavior described above, I used custom code components and a loop:
    • The custom code counts keypresses and uses if-else statements to display feedback messages.
    • Another if-else structure determines whether to repeat the video routine or proceed to the next part of the experiment.

The Issue:

  • In Condition 1, this loop works as expected:
    • If the keypresses meet the criteria, the experiment proceeds without repeating the video.
    • If the keypresses do not meet the criteria, the video repeats once and then exits the loop, continuing the experiment (the codes for this are mentioned in additional information below)
  • However, in Condition 2 (which follows two additional movie routines after Condition 1), the same loop does not work. Specifically:
    • During the first viewing of the video in the loop, the experiment stops at a specific point and does not restart.

    • Initially, I suspected the issue was related to the video file size (350 MB). However, replacing it with a smaller video file (25 MB) did not resolve the problem. The loop still works in Condition 1 but fails in Condition 2.

  • All three videos are preloaded in the experiment.

Additional Information:

  • I have attached a screenshot of the experiment flow and included the custom code components below for reference.
  • Within the loop there are 2 routines, each of which has a custom code component to conditionally run the loop. The screenshots for those routines are also attached. The ‘practice_loop_event’ one is Condition 1 loop, which works but ‘practice_loop_goal’ doesn’t work.

The custom code in ‘practice_event’ routine in is:

“Begin experiment”:
practice_event_reps = 0
“Begin routine”:

key_press_count_event = 0
feedback_message_event = "No feedback available"
upper_limit_event = 10  
lower_limit_event = 2   

repeat_practice_event = False

“End Routine”:

# Count total keypresses recorded during the routine
if practice_movie_key_resp_event.keys:  # Check if any keys were pressed
    key_press_count_event = len(practice_movie_key_resp_event.keys)
else:
    key_press_count_event = 0

# Set the feedback message based on the count
if key_press_count_event > upper_limit_event:
    feedback_message_event = "Too many keypresses! Follow the instructions more carefully."
    repeat_practice_event = True
elif key_press_count_event < lower_limit_event:
    feedback_message_event = "Too few keypresses! Be more engaged."
    repeat_practice_event = True
else:
    feedback_message_event = "Good job! Your data has been recorded."
    repeat_practice_event = False

# Save keypress data for logging
thisExp.addData('key_press_count_event', key_press_count_event)
thisExp.addData('practice_movie_key_resp_event.keys', practice_movie_key_resp_event.keys)

The custom code in ‘Practice_Feedback_Event’ routine is:

“End Routine”:

# Increment the practice repetition counter
practice_event_reps += 1

# Allow only one additional repetition if feedback is bad
if repeat_practice_event and practice_event_reps < 2:
    practice_loop_event.finished = False  # Continue the loop
else:
    practice_loop_event.finished = True   # End the loop

The codes are similar for the second loop ‘practice_loop_goal’, except that everywhere event is replaced with goal in naming routines, components and variables in code. This loop is the one which is not running the movie properly.

Does anyone know why the loop functions correctly in Condition 1 but not in Condition 2? Any advice on debugging or potential issues with online execution would be greatly appreciated.

Are you re-using practice_event_reps?

Do you need to reset the value to 0?

No, I renamed all the variables for the second loop, so it’s practice_goal_reps there. Also, both loops work fine locally. However, during the online experiment, the video stops in the middle.

In Begin Experiment you have
practice_event_reps = 0

but not
practice_goal_reps = 0

Is that correct? Where/how do you initialise practice_goal_reps?

Oh so all the custom code i posted is for the first loop. All the code for second loop ‘practice_loop_goal’ is the same except that the variables and routine names have goal instead of event.

So practice_goal_reps is initialised as 0 in second loop.

So everything with the same loop logic works in loop 1 but not 2.

Here are the code components for the second loop ‘practice_loop_goal’ which was not working well.

code_goal component in the Practice_goal routine:

“Begin experiment”:

practice_goal_reps = 0

“Begin routine”:

key_press_count_goal = 0
feedback_message_goal = "No feedback available"
upper_limit_goal = 10  
lower_limit_goal = 2   

repeat_practice_goal = False

“End routine”:

# Count total keypresses recorded during the routine
if practice_movie_key_resp_goal.keys:  # Check if any keys were pressed
    key_press_count_goal = len(practice_movie_key_resp_goal.keys)
else:
    key_press_count_goal = 0

# Set the feedback message based on the count
if key_press_count_goal > upper_limit_goal:
    feedback_message_goal = "Too many keypresses! Follow the instructions more carefully."
    repeat_practice_goal = True
elif key_press_count_goal < lower_limit_goal:
    feedback_message_goal = "Too few keypresses! Be more engaged."
    repeat_practice_goal = True
else:
    feedback_message_goal = "Good job! Your data has been recorded."
    repeat_practice_goal = False

# Save keypress data for logging
thisExp.addData('key_press_count_goal', key_press_count_goal)
thisExp.addData('practice_movie_key_resp_goal.keys', practice_movie_key_resp_goal.keys)

Similar to loop 1, custom code ‘code_feedback_goal’ in ‘Practice_feedback_goal’ routine is:

“End routine”:

# Increment the practice repetition counter
practice_goal_reps += 1

# Allow only one additional repetition if feedback is bad
if repeat_practice_goal and practice_goal_reps < 2:
    practice_loop_goal.finished = False  # Continue the loop
else:
    practice_loop_goal.finished = True   # End the loop


If your first loop and routines are working and your second one is failing with similar routines, could you try reusing the routines from the first loop?

Thanks for the suggestion.

I reused the first routine from practice_event_loop. Initially, it didn’t work, but then it suddenly did. I also followed the advice to reuse the mov_s_1_event and mov_s_2_event routines later in the experiment. However, I repeatedly encountered the following warnings:

WARNING: Stopping key buffers, but this could be dangerous if other keyboards rely on the same.

WARNING: Parameters not recognized by this version of PsychoPy have been loaded from your experiment file: plCompanionCameraCalibration. This experiment may not run correctly in the current version.

Additionally, at one point, I pressed the spacebar multiple times during the movie routine of the second loop (originally the problematic loop), and the experiment unexpectedly ended. This also happened during the re-used Mov_1_S_event routine movie playback. I wonder if this issue is related to the first warning above or if it’s with re-using of routines. Since I am recording spacebar keypresses during each movie, it should log them without terminating the experiment. Although this issue did not occur again after multiple re-runs, I am concerned it might happen with participants. Overall, I feel the experiment remains somewhat unstable.

Hello @Avisha_Gaur

You can delete this parameter in the relevant routine. It should be listed in the routine settings of the routine.

I guess we need to see how you implemented the multiple spacebar keypresses.

Best wishes Jens