| Reference | Downloads | Github

Including Block/Loop Number in Text

I’m programming an experiment in which sets of trials are broken up into blocks, just to reduce participant fatigue a little. I’ve created a set of loops for trials within a loop for blocks. I’d like to include some text for participants at the end of each block to let them know how many blocks they’ve completed, and how many are left to go. Any recommendations?

Put something like this in a text field, set to update every routine:

$'Number of blocks completed = ' + 
str(name_of_your_block_loop.thisN + 1)  +
', \n number to go = ' + 

Not sure if you will need to add 1 to .nRemaining.

Thank you! This approach worked great! Some minor comments in case other beginners have the same question as me-- I had better success using:

str(name_of_block.thisRepN + 1)

Also, I discovered I couldn’t copy and paste this into the text field, as the line breaks caused some problems, but re-typing it without the line breaks worked fine.

Again, this is probably just basics of using Psychopy that I’m still learning since I only picked up the program last week!

Hi, I am trying to use this for an online study. Using the JS code for this (name_of_block_loop.nRemaining.toString()), it keeps showing the same value instead of decrementing every block… Does someone have a solution for this? Thank you!

Search the forum for my crib sheet.

Online all loops are referred to as trials which will be affecting your function. You might need to have a bespoke variable that you change each iteration.

Ok thank you! So I’m new to JS, I’m not sure where I should put the code for this incrementing variable… in the definition of the blocks loop (blocksLoopBegin)? Or can I use the name of my condition file for the block (it already contains the number of the block)? Can I access the name of the file inside a routine? Thank you!!

If you selected the conditions file using a variable then yes you can definitely access that variable inside the loop.

Yes it works! Thank you

I’m attempting to do this same thing in Pavlovia but my blocks are not file names. How would I go about updating a count variable each block in javascript? All my attempts have resulted in getting stuck on “initializing experiment”.

Do you have any imports?

I have a conditions spreadsheet with paths to 32 video files (32 trials) and other information (presented on left/right, duration, etc.). The trials occur in an inner loop with a routine for the stimuli and a routine for the response. After 32 trials, the participant can take a break. This break is in an outer loop that repeats 8 times (8 blocks). I want to display “Round: x/8” on each break.

I think I managed to get some code working, based on the last post in this thread: Use thisRepN correct - Online experiment created with builder. I can at least get past “initializing the experiment”. The issue seems to arise from using a variable in the text component. So I haven’t actually been able to display this information yet to check if the code is working.

What does your code look like?

I have this:

block_count = 0;

In the Begin Experiment tab of the response routine in the inner loop.

And this:

 block_count += 1;

In the Begin Routine tab of the rest routine in the outer loop. I’ve also tried some variation of a “for i in range(8): count = count + 1” loop but the JS syntax is different from python and I thought my errors might have been causing the experiment to get stuck.

I try to display this in the body of the text component (made in builder, though I can code one and it might work better):

Round: $block_count / 8

And I think this is what was causing the experiment to get stuck.

In a set every repeat text box that should be: ‘Round : ‘+$blockCount+’ / 8’

Thanks! I actually figured it out with

txt1 = "Round: ";
txt2 = "/8";
msg = txt1 + block_count + txt2;

in the Begin Routine code and $msg in the text component (set every repeat).

I also just wanted to say how much I appreciate your crib sheet and forum responses! I don’t think I would have a master’s thesis without them!

1 Like

You probably need str(block_count) for it to work locally

Hi Michael, when I add this piece of code to the textbox as per figure attached, I get an error that the Loop call control is not defined.
I just want to add the number of loop iteration I am in every time the loop makes a pass. My loop is call Control and it set to 16 reps.

Any thoughts??

Please always provide the exact text of the error message, rather than a synopsis: I’m not sure what “call control” means. But in this case, the error is almost certainly due to you having the field set as “constant” rather than to update on every repeat. Fields that are set to be constant are defined at the start of the experiment, as they don’t need to be updated subsequently. But if you are trying to refer to a loop at that point, the loop doesn’t yet exist, and an error will therefore occur.

Thank you Michael for your prompt response.
I am sure that you are correct on the cause of the error.
So how can I get to add iteration number of a loop in component text in builder?
Sorry for not providing the exact error!

I gave the answer above: simply set the Text field to update on every repeat, rather than be constant.