| Reference | Downloads | Github

Referencing conditions file in code component

Hi all,

I am a new convert to Builder (for the purpose of running on Pavlovia). I have always used Coder to program my experiments full from scratch, so I am in a weird space of not knowing how to do anything in a program with which I am generally familiar.

I am using standalone PsychoPy 3 v. 2020.1.3 on a Windows 10.

So my question is rather simple, but I was not able to find the right query to find an answer:
How do I reference variables in my conditions file in a code component?

Here is a description of the task and what I’ve done:

The task is a gambling task in which people choose their preference for one of two gambles. So, on the left might be a gamble like “100% chance of winning $69 (0% chance of winning nothing)” and on the right might be a gamble like “75% chance of winning $1374 (25% chance of winning nothing)”
Participants hit either the left or right arrow key to make their choice. The values for the percent chance of winning (e.g., 100 and 75), the amount to win (e.g., 69 and 1374), and the win nothing percentage (e.g., 0 and 25) are all referenced in a conditions file with the variable names ProbLeft, ProbRight, ValueLeft, ValueRight, Alt_ProbLeft, and Alt_ProbRight.

I inherited the program I am trying to get to work from a colleague in another lab and it works. However, it was written such that there are separate text components for the different aspects of each gamble. Consider again the left gamble: 75% chance of winning 1374 (25% chance of winning nothing). This sentence is shown as 4 separate text components that are all drawn concurrently. One that draws ProbLeft, one that draws, Alt_ProbLeft, one that draws ValueRight, and one that draws " % chance of winning ( % chance of winning nothing)" My colleagues worked the spacing out such that the text component that draw variables are drawn in the appropriate positions to make the sentence readable.

I do not like this solution because I expect that running this experiment on Pavlovia, as is, would result in wonky formatting issues dependent on my participants’ screen size. So, instead, I would like to construct a sentence to be shown on the left and a sentence to be shown on the right.

I created a code component on my trial in which I could create a variable to concatenate all the individual pieces of the gamble. So, in Begin Routine I wrote:

leftText = $ProbLeft+"% chance of winning"+$ValueLeft

This resulted in a syntax error. So, what is the proper way to use the values from those variables in my conditions file to achieve my goal?

Thank you so much for your time :slight_smile:

1 Like

You should try to run it with the $ signs removed from the code snippet you posted. You don’t need to use $ to reference the variables from your conditions file within a code component.

Also, when you post in the future you might consider withholding the detailed description of your task when it is not necessary to understand the question.


@adkinsty is correct. To explain: the $ is nothing to do with Python syntax. It is just a Builder-specific cue that you want it to treat what follows in a text field as a Python expression, rather than as literal text. All of the variable names from the column labels in your conditions files are directly available in the namespace for you to use in your Python code, with no special handling required.

Thanks to you both.

The code worked great without the dollar signs (once I converted all the values to strings).

It just didn’t occur to me that the code components are pure python. I haven’t used builder in a long time and need to brush up on the basics.

Cheers and I hope you both are safe in these weird times.