psychopy.org | Reference | Downloads | Github

Getting a ReferenceError after creating an if-statement that calls on the conditions file

URL of experiment: https://gitlab.pavlovia.org/S4HRI/object_selection

Description of the problem: I am creating the experiment in PsychoPy 2020.2 on a Mac. In the task, the participant selects one of two objects with the mouse, and then the face in the middle either looks at the same object or the other one. To achieve this, I added a code component that determines the gaze-direction presented in the subsequent routine, which calls on a column in my conditions file (csv) called “Following” with the values “followed, unfollowed”:

if mouse.clicked_name[0] == 'image_left':
    if Following == 'followed':
        icubgaze = 'leftcropped300.png'
    elif Following == 'unfollowed':
        icubgaze = 'rightcropped300.png'
    else:
        print('selection error left')
        #continue
elif mouse.clicked_name[0] == 'image_right':
    if Following == 'followed':
        icubgaze = 'rightcropped300.png'
    elif Following == 'unfollowed':
        icubgaze = 'leftcropped300.png'
    else:
        print('selection error right')
        #continue
else:
    print('selection error or code issue')
    #continue

(1. I blocked out the continue part as it threw the notorious ReferenceError: frameDur is not defined. The print statements are not of much use, but for now I’m still in the middle of designing.
2. I got an imagestim error, which I managed to resolve by manually copying my stimuli to the resources folder. As a result, there are duplicate files in separate locations, but I reckon that shouldn’t be an issue for the time being).

Long story short, the exact error I get online now, and that I need a little push with, is ReferenceError: Following is not defined. The earlier imagestim error made me think that something was wrong with the conditions file, but that’s all properly loaded (and so is the mouse.clicked_name at the top of the code). So, I suspect that I need to call the trialhandler more explicitly in JS. Could that be it, and more importantly, how would one do that?

Minor update but hardly: using the more explicit trial hander method in PsychoPy works locally (i.e. if thisTrial["Following"] == 'followed': etc.), but still returns the following error on Pavlovia: ReferenceError: thisTrial is not defined. Hello square 1, long time no see… :smile:

Just a small update, I fiddled around a little and on my latest run I got a TypeError instead of a ReferenceError: TypeError: Cannot read property ‘Following’ of undefined.

In the meantime I figured out that it could either be that the way Python dictionaries (such as .thisTrial are accessed doesn’t work the same as JS… errr… Objects(?); or the different way in which JS schedules task. Unfortunately my JS skills are very limited so I was wondering if somebody could have a quick look at my auto-translated code?

if ((mouse.clicked_name[0] === "image_left")) {
    if ((trials.thisTrial["Following"] === "followed")) {
        icubgaze = "leftcropped300.png";
    } else {
        if ((trials.thisTrial["Following"] === "unfollowed")) {
            icubgaze = "rightcropped300.png";
        } else {
            console.log("selection error left");
        }
    }
} else {
    if ((mouse.clicked_name[0] === "image_right")) {
        if ((trials.thisTrial["Following"] === "followed")) {
            icubgaze = "rightcropped300.png";
        } else {
            if ((trials.thisTrial["Following"] === "unfollowed")) {
                icubgaze = "leftcropped300.png";
            } else {
                console.log("selection error right");
            }
        }
    } else {
        console.log("selection error or code issue");
    }
}

(Ignore the log statements, old habit to debug everything in Python).

@Cesco, if “Following” is a variable in your conditions file, and you are interesting in the current value for that variable, then I think you should be able to use the actual variable name, rather than attempting to access it from the trialhandler:

if ((mouse.clicked_name[0] === "image_left")) {
    if (Following === "followed") {
        icubgaze = "leftcropped300.png";
    // etc...

I appreciate that @dvbridges. It is indeed a variable in my conditions file, and I am only interested in the current value in the routine. Unfortunately, that method was how I attempted it in my opening post. I tried it again (JS posted below, as I forgot that first time around), but I still get the a “ReferenceError: Following is not defined”. Other variables in the conditions file, such as image files and their dimensions in pixels are accessed properly. Right now I’m thinking: Is there a way to add this to the scheduler? I have seen similar issues involving `.thisTrialN’, but I’m not interested in that specifically.

if ((mouse.clicked_name[0] === "image_left")) {
    if (Following === "followed") {
        icubgaze = "leftcropped300.png";
    } else {
        if (Following === "unfollowed") {
            icubgaze = "rightcropped300.png";
        } else {
            console.log("selection error left");
        }
    }
} else {
    if ((mouse.clicked_name[0] === "image_right")) {
        if (Following === "followed") {
            icubgaze = "rightcropped300.png";
        } else {
            if (Following === "unfollowed") {
                icubgaze = "leftcropped300.png";
            } else {
                console.log("selection error right");
            }
        }
    } else {
        console.log("selection error or code issue");
    }
}

@Cesco, here appears to be working MouseLeading_v0_2 [PsychoPy]

The csv conditions file was using semicolons as separators, this may have affected how the file was parsed, locally at least, so I changed the separators to commas. Then, I had to add the file extensions of the stimuli to the csv file, so that PsychoPy recognises these entries as files and adds them as resources. Here is the conditions file. This also means you have to change how you access the filenames in your builder components i.e., remove the concatenation of “.png”.
conditions50-50.csv (1.7 KB)

@dvbridges, that worked an absolute charm, thank you!

(Excel does that when you save files as csv and I forgot about that. Their extension should be .scsv :wink: )