Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] Using variables to set loop repeat

edited August 2011 in OpenSesame
Hi:

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

  • edited August 2011

    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.

    self.experiment.items["my_loop"].repeat = 4

    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

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 9:38AM

    Hi Sebastiaan:


    Works like a charm! 

    Thanks!
    Per

  • edited 9:38AM

    Me again!


    When are loop counts updated?  Now that the I can set the number of repeats for a loop (the cycles also seems to work), I'd like to use the current loop count to present different stimuli.  The problem is that my loop count available to me in the __prepare__ of an inline script at the start of the sequence inside the loop does not seem to iterate beyond 0.

    For example, I'm trying something like this:

    p, li { white-space: pre-wrap; }

    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...


    Any thoughts?

    Thanks,
    Per

  • edited 9:38AM

    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

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 9:38AM

    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 var object. So you can still programmatically change the repeat variable of a loop item, but it would work like this:

    items['my_loop'].var.repeat = 4
    

    An alternative would be to edit the script of the loop item, and define the variable repeat in terms of another variable (my_repeat, say) which you could then define in a loop at a higher level of the structure.

    set repeat [my_repeat]
    

    Cheers!
    Sebastiaan

    Thanked by 1hanzh

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • 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

Sign In or Register to comment.