Expected Behavior? Code in "Before Experiment" now gets executed AFTER definition of global variables

Hi everyone,
I just upgraded my installation from 2023.1.2 to 2023.2.1.

In 2023.1.2 everything you put into the “before experiment” tab of a code component was executed BEFORE most of the auto generated python code (pretty much right after the imports). Now it gets executed AFTER the auto generated global variable setup:

# --- Setup global variables (available in all functions) ---
# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
# Store info about the experiment session
psychopyVersion = '2023.2.1'
expName = 'test'  # from the Builder filename that created this script
expInfo = {
    'participant': f"{randint(0, 999999):06.0f}",
    'session': '001',
    'date': data.getDateStr(),  # add a simple timestamp
    'expName': expName,
    'psychopyVersion': psychopyVersion,
}

# Run 'Before Experiment' code from c_start
# ...

Was this change intentionally? Sadly it breaks all of my experiments as I controlled the contents of some of the fields of the experiment info dialogue with the “before experiment” code.
Maybe @TParsons? It’s not that bad, I’ll tell psychopy to use the old version. But I am interessted if that’s a bug or intentional :slight_smile:

This change came because of Python scripts for experiments now being modular - allowing just a particular part of an experiment (e.g. just running the flow, just showing the expInfo dialog, just setting up the window) to be run without having to run the whole script. This necessitated significant changes to how scripts were written - we wanted these variables to be available as soon as possible so put them at the top of the script, but that does mean variables defined in Before Experiment aren’t available… So I think you’re right that these definitions should happen after Before Experiment code.

As a temporary fix, you could use a placeholder value in expInfo from the experiment settings and then set it in the same Code component instead, like so:

myValue = <whatever>
expInfo['myParam'] = myValue
1 Like