psychopy.org | Reference | Downloads | Github

'Begin Experiment' code not working – Online Builder with Code components

Description of the problem:

I’ve been trying to move one of my studies online, but I’ve been having issues with the JS code only working when its set Begin Routine, not when its been left - where it needs to be - in Begin Experiment. I’m completely new to trying to run studies online, but this is a problem I’ve not got a work around for.

The code component itself is for two arrays, one which will eventually be used to randomise stimuli and another to pair stimuli into compounds participants will be shown, and a counter initially set to 0.

this code permits me to have my image component directed toward the right images to display

This is the code competent:

var stim = [
    [Math.random(),'Images/A.jpg'],
    [Math.random(),'Images/B.jpg'],
    [Math.random(),'Images/C.jpg'],
    [Math.random(),'Images/D.jpg'],
    [Math.random(),'Images/E.jpg'],
    [Math.random(),'Images/F.jpg'],
    [Math.random(),'Images/G.jpg']
];


var P2_trial = 0;

var P2 = [
    [stim[0][1], stim[1][1], "Images/Good.jpg", "P2.0.ED.ET"],
    [stim[1][1], stim[0][1], "Images/Good.jpg", "P2.0.ET.ED"],
    [stim[2][1], stim[3][1], "Images/Good.jpg", "P2.0.R1.R2"],
    [stim[3][1], stim[2][1], "Images/Good.jpg", "P2.0.R2.R1"],
    [stim[4][1], stim[5][1], "Images/Bad.jpg", "P2.0.N1.N2"],
    [stim[5][1], stim[4][1], "Images/Bad.jpg", "P2.0.N2.N1"]
];

and this is used in the image component

$P2[P2_trial][0]

this code works produces a ‘ReferenceError: X is not defined’ crash if i set the code component to Begin experiment, but appears to work if its Begin Routine. not being able to set variables and then work with them in latter routines would be a real issue. any assistance would be highly appreciated

Many thanks

Does it persist if you edit the file on Gitlab?

Probably a scope issue. Remove the word “var” from before each variable so they will be declared as implicit globals See here: Variable not defined

Thanks for the replies

It looks like the var trick may have done the job