If this template helps then use it. If not then just delete and start from scratch.
OS (e.g. Win10): Windows 11 Pro PsychoPy version (e.g. 1.84.x): PsychoPy 2023.1.3 What are you trying to achieve?: To access in a routine a value from a previous routine
What did you try to make it work?: I’ve tried created a variable “routine = 2000” within every tab of my code component in my first routine (trial 1) and then tried to display it in my second routine (trial 2).
What specifically went wrong when you tried that?:
Only variables defined in the tabs “before experiment” and “begin experiment” can be accessed by trial 2. However, I would like to access variables that are defined either in the “each frame” or in the “end routine” (what I actually wanna do is to count the number of slider choices they make to find the correct value and displaying the number of slider choices they made in the next trial)
Maybe you can initialize your variable before the experiment with None (or somethin different), (re)set it in your first routine and read it in your second routine.
By initializing the variable you mean defining the variable before the experiment, right? That part I’ve done (I’ve created “record = 3000” in the before experiment tab of trial 1 just to check whether I could access it in trial 2). However, not sure what you mean by “(re)setting it” - so maybe that is what is missing. What do you mean by it?
(just a correction on my post, the variable I created was “record = 2000” and not “routine = 2000”); I guess it doesn’t really matter, though. Just the fact that I’ve defined the same variable across all tabs in trial 1 to see which of them could be accessed by trial 2)
I just tried it and psychopy lets me access variables defined from any of the different code tabs. When looking at the code produced, they are all created in the global scope. So there shouldnt be any problem accessing them from anywhere in your experiment.
Maybe lets start over: What exactly went wrong? What error message do you get when trying to access the variable record from routine trial 2?
Thanks for taking the time to try it out. I am attaching my .psyexp file and resources folder with the exp images just in case you want to take a look (but ofc, you don’t have to!).
In my experiment, unless the variable “record” is defined in the tab “before experiment” or “begin experiment” of trial 1, trial 2 cannot access it and the experiment does not run. The error message it yields is below:
I am not sure why this is happening since you said that you tried it yourself and it worked. It is either something very dumb from my side or maybe a bug of some sort?
Just one thing: I am running it locally via python script, not via JS.
Hello,
Instead of assigning the displayRecord at the start of the experiment, set it at the beginning of your routine (displayRecord = record).
What happens is that you are trying to assign the record variable, which is not yet available at the start of the experiment.
Also, you don’t need to set the record to 2000 many times; you can remove it from everywhere except the Begin Routine tab.
Thank you very much for your help! You helped me change my focus to “displayRecord” and solve problem. It was not exactly what you suggested, so I will explain how I solved it so others in the future can be helped by this post.
The variable “record” was fine, but the experiment was not running because the text component responsible for displaying the value in “displayRecord = record” was not configured to “set every frame” but to “constant”. In this situation, the experiment only ran if displayRecord was placed in “Before Experiment” or “Begin Experiment”.
PsychoPy was telling me that the variable “displayRecord” was not defined if I only defined it in Begin Routine or Each Frame. I just could not understand what was going on
Anyhow, in short: do not forget to configure the component to “set every frame”.
Thank you @umb and @Chen for your time trying to help me here!