| Reference | Downloads | Github

nReps variable not updating

Continuing the discussion from Can't break out of loops using a variable in Pavlovia:

URL of experiment: Pavlovia

Description of the problem: Below is a screenshot of my flow. nReps for the EffortLoop is set to a variable I created that is updated (EffortRepN = EffortRepN + 5) in a code component in the Choice routine (occuring before the EffortLoop), and the EffortLoop repetitions function as they should.

For the TrialsLoop, I have set nReps to variable TrialsRepN, which is conditionally updated (TrialsRepN = TrialsRepN + 1) in a code component in the Choice routine (occuring within the TrialsLoop), and the TrialsLoop ends after 1 repetition (TrialsRepN is set to 1 in a beginning of experiment code component), so it appears the TrialsRepN value is not being updated (although other aspects of the conditional code it is nested within are working). Is it possible to update the value of an nReps variable within the loop that references it?

I have not used the $ for the nReps variables. Much appreciation for any suggestions.

No. A loop has to know how many times it will run before it starts, because it has a bit of setup to do. You’ve done exactly the right process with the other loop, by setting its nReps value in a routine that occurs before the loop starts.

Its not really clear why one would want to alter nReps within a loop, so perhaps you should explain what you want to achieve (i.e. this sounds like a classic XY problem).

At a guess, I’m thinking you don’t know in advance how many iterations of the outer loop you want to run, so you’re trying to update it on the fly. If so, the way to approach this is from the opposite direction: give the loop a starting nReps value that is very high, and then simply terminate the loop early if needed:

1 Like

Thank you Michael! Yes, going into the TrialsLoop, I do not know how many times it needs to execute; the number of repeats of the TrialsLoop is based on participant’s choice. Setting the TrialsLoop nReps to 100 and then exiting the loop based on the participant’s choice worked perfectly!!!