[solved] Using variables to set loop repeat
Hi:
p, li { white-space: pre-wrap; }
I'd like to use a variable set in a script to set the number of loop repeats. I have a script that does this:
self.experiment.set('num_lists', 6)
and then I try and set the loop repeat value to [num_lists] and it gives me the following error:
p, li { white-space: pre-wrap; }
'%s' is not a valid value for repeat. Expecting a positive numeric value.
Any ideas so that I don't have to hard code this stuff?
Thanks,
Per
Comments
Hi Per,
Thank you for christening the brand new shiny forum!
Yes, you can certainly do this, but in a non-documented hacky way, by accessing the OpenSesame internals. For example, to set the repeat value of a loop item called my_loop to 4, you do the following in an inline script before the loop item.
You can access the internals of all the items like this, through the experiments.items dictionary.
And I'll look into the error that you report, because that looks like a bug!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan:
Me again!
item_index = self.get('count_TrialLoop')
and then pick a word from a list based on that item_index, but it doesn't seem to change...
Hi Per,
The loop counts are updated every time that a loop is executed. So the counter is increased at the start of the run phase. The same applies to all items.
But I can understand your confusion. The thing is that you need the counter of the item that is called by the loop item (probably a sequence), rather than the counter of the loop item itself: The loop is executed only once, the sequence in it is executed multiple times.
So something like this should work:
item_index = self.get("count_my_sequence")Good luck!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Great! That did it! That also cleared up the loop confusion.
Best,
Per
Hi @sebastiaan,
I just wanted to confirm that the method mentioned here:
self.experiment.items["my_loop"].repeat = 4
is still working, since I have a similar problem. My experiment has 4 blocks with the same trial sequence, but the number of trials in each block is different. I was thinking to use this way to set up the number of repeats for each block. It seems that the method is not working in my case. I have tried to put the code into both the prepare and run phase.
Thanks!
Han
Hi Han,
In recent versions of OpenSesame, variables are properties of the
varobject. So you can still programmatically change therepeatvariable of aloopitem, but it would work like this:An alternative would be to edit the script of the
loopitem, and define the variablerepeatin terms of another variable (my_repeat, say) which you could then define in aloopat a higher level of the structure.Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi @sebastiaan,
I'm not sure if this is the right place for my question, but this topic was the most related one that I found.
I have a loop and a sequence inside with several sketchpads.
I want to skip some of the sketchpads in the first trial of the loop.
Do we have a variable as the "loop counter" so I can use it in the sequence "Run if" column?
I want to say "Run if the loop counter is not one!" (so I can skip this sketchpad in the first trial and then have it in the rest).
Thanks in advance,
Kian
Hi, there are the count variables.
they are labeled as "count_item_name", so if you have a sketchpad called, "stim_presentation", the variable would be called "count_stim_presentation". You can use that in the run if statement (run only if it is bigger than 0)
Hope this helps,
Eudard