Including Block/Loop Number in Text

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!
Thanks!!!

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

This thread has been extremely helpful in tackling my very similar issue, so many thanks to everyone who has contributed! However, my code is flat out not working to show incremental blocks as one of the examples in this thread outlined. Here is a snapshot of my routine:

Within Instructions_3, I have a code object executing the following at Beginning of Experiment:

blockCounter = 0;

For Block_Increment, I have a code object executing the following at End Routine:

blockCounter += 1;

I also have a text object in Block_Increment, $"Block N: " + str(blockCounter)

The experiment runs smoothly, but the blockCounter is not working and only displays “Block N: 0” each time through. Would anyone know what the issue is? I have tried inserting the codes at different points in and outside the loops with no luck.

Is this object set to update each repeat?

Thanks for the reply. I thought the value of block increment should be set up to increase in value each loop. The text object in Block Increment should just read the updated value, correct? I have a feeling this is not the case because it has not worked for me though.