| Reference | Downloads | Github

PsychoPy resets global scope variables in loop?

Hello, please see the image below :


I have a loop called yelloPriorityLoop which runs twice the trial above. I used Code component in trial_2 routine and some global scope variables in Begin Experiment. I just wanted to know every time that loop is running, the global scope variables values will be changed to their default value?

Hi Ali,

We probably just need to see your actual code. But in Builder-generated code, there isn’t really a concept of “default values” for variables. If a variable is set in code that runs at the beginning of the experiment, then it simply retains that value, unless it gets changed at some later point. Code at the beginning of the experiment only runs once, so there is no way for variables to get “reset” to that initial value, unless you do it explicitly yourself.

1 Like

@Michael Thank you, I tried it and got there is no reset for variables in loop because they have been initialized in Begin Experiment.

You can get this behaviour if you want, by having code in the “begin routine” tab, but that is set to run only once per loop (i.e. only on the first trial (number 0)). e.g.

if your_loop_name.thisN == 0:
    some_variable = some_default_value
    some_other_variable = some_other_default_value
1 Like

@Michael It helps me very much to do it in my next projects. Thanks. :heart_eyes:

1 Like