Show a counter that resets at the beginning of each loop

I have a simple experiment with four identical loops of 12 trials, with only one routine per loop which was copied/pasted so identical for the four loops. I’d like to show a counter in a text that says " 1 out of 12", where the first number updates for each trial.

So far I have created a code component with
trialN = 0
in Begin experiment and
trialN +=1
in Begin routine.

In the text component I have $str(trialN) + ' out of 12'
That works except that it keeps adding up in the 2nd, 3rd and 4th loop so that I get 13 out of 12, 14 out of 12 etc.

How can I make it reset to 1 at the beginning of each loop, ideally without having to re-create a different routine in each loop?

Thank you!

Hello Icrible

you could test for the value of trialN and reset it whenever it reaches your limit:

if trialN  == 12:
    trialN = 1
    trialN +=1

or you add a routine following your routine in which you reset trialN in a code-element.

Best wishes Jens

Perfect, thank you @JensBoelte !