Howdy, Stranger!

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

Supported by

[solved] How to generate loop variable values based on others within a loop cycle?

edited October 2013 in OpenSesame

I am using a loop item to set up values for 5 variables for a single cycle (called "0" in the OS script screen). One of the variables (a_one) is to be a randomly generated value in {0,1}, which gives setcycle 0 a_one "=randint(0,1)" in the script-view of the loop. Each successive variable value is set conditioned on a_one, a_two, etc, so that a_two =1-self.get('a_one'), and so on.

But after I try doing this daisy-chain for more than 2 variables, the order gets messed up in the script-view of the loop, which results in a variable being used before it is defined.

So, for just a_one and a_two, the script-view gives the following, which works:

set repeat "1"
set description "Repeatedly runs another item"
set item "sequence"
set column_order "a_one;a_two"
set cycles "1"
set order "random"
setcycle 0 a_one "=randint(0,1)"
setcycle 0 a_two "=1-self.get('a_one')"
run sequence

But if I try adding a third variable that depends on a_two, then I get an error. The following script-view shows up after I add the third variable:

set repeat "1"
set description "Repeatedly runs another item"
set item "sequence"
set column_order "a_one;a_two;a_three"
set cycles "1"
set order "random"
setcycle 0 a_three "=1-self.get('a_two')"
setcycle 0 a_one "=randint(0,1)"
setcycle 0 a_two "=1-self.get('a_one')"
run sequence

Notice above that a_three has a setcycle that occurs before a_one, even though the set column_order has a_one, followed by a_two, followed by a_three. Even if I put in some fixed number for a_three (e.g. '0'), it gets its setcycle performed first. Furthermore, if I rearrange the setcycle commands in the script-view of the loop, and press 'apply and close', and then look again at the script-view, the malformed order is back again.

Can anyone suggest how I would go about setting one variable after another in a loop, where each is based on one or more variables being set before it?

Thanks!

Comments

  • edited 12:38AM

    Can anyone suggest how I would go about setting one variable after another in a loop, where each is based on one or more variables being set before it?

    The order in which the columns are set is essentially random, so you cannot assume a specific order. The visible order of the columns is kept constant in the GUI, but that's just because it's annoying if the columns keep jumping around (which they used to).

    To have variables depend on other variables, the best strategy is simply to insert a short inline_script at the start of the trial_sequence. In your case, this would be something like this:

    exp.set('a_two', 1-self.get('a_one'))
    exp.set('a_three', 1-self.get('a_two'))
    

    Cheers!
    Sebastiaan

    (Btw, in your example a_three is always equal to a_one.)

  • edited 12:38AM

    Okay, I'll set it up in a script at the beginning of the sequence.

    Thank you for a very prompt follow-up!

    (Yes, I realize that a_three will be equal to a_one, but this was a simplified example of the behavior.)

    Cheers!

Sign In or Register to comment.