| Reference | Downloads | Github

Code Component not working when running in Pavlovia

You did it right, this is the error I mentioned earlier when I said that sound.Sound doesn’t really work in a code component in JavaScript. See here: Type Error: Cannot read property '_psychoJS' of undefined

Sound objects really need to be created in the builder, and while you can start or stop them or change their order in a code component, it is sadly not possible to create a new one from a code component.

Oh, OK, so do you have any suggestion how to randomize by the conditions I mentioned above, given that the sound object is already created in the builder? I just want the shuffling to be conditional, and to insert items of a certain condition every 10th trial. So to look at the first part as an example, should it be something like:

if (sit_trials.thisTrialN === 0){
    while (sit_trials.trialList[0]['condition'] === 'Catch'){
    Stimulus = sit_trials.trialList[0]['Audio'];
    condition = sit_trials.trialList[0]['condition'];;
    sit_trials.addData('Audio', Stimulus);
    sit_trials.addData('condition', condition);

I’m new to Pavlovia and PsychoPy, I’m very sorry for the trouble but I just can’t figure this out completely.
Thanks again!

When you go to “view code” for your experiment in Pavlovia, it should take you to a complete gitlab repository that includes both the python and the JS code, as well as the psyexp file and any stimulus files and data files. Basically the whole experiment folder. You will need to make that repository public, go to “settings” then “general” and “permissions”. You can then link the repository, which will have a URL like but with your username and project name after the slashes.

@N_T IS the basic idea that you are shuffling a larger list of trials and want it to be mostly random, but also to have certain non-random trials? In that case I would suggest an approach like the one I proposed in this thread (read to end): Randomising types of trials so the same stimuli do not repeat one after the other

Long story short you will have a file with all of your trials in it, you will have some code which manually shuffles arrays that represent the contents of those trials, and you will then use that array to reference the relevant stimuli. You are basically making your whole order as a separate list and then using it as a key to your actual stimuli, but trying to let PsychoPy do all of the loading and presentation with its built-in systems.

@jonathan.kominsky That is pretty much the basic idea. I want trials to be randomized but to prevent trials from the same condition to appear more than 3 consecutive times, and also to have some trials in fixed locations in the loop. Does your proposal allow this?
I will read this thread anyway, thank you very much!

Thanks, i am not sure i can share the whole experiment folder due to peoples personal images being used etc, the pavlovia experiment wasn’t going to be made public. I can certainly get the code minus the stimuli etc though.

i am just rebuilding the builder and putting the elements you previously mentioned in there again from afresh and see if that helps, as i synced so many changed versions it may not have synced correctly.

@Stuart_Furnell I also assume you have read through this whole thread: "Take a break" works locally but not online

Your case is actually slightly more difficult because you are targeting specific trials that don’t have an obvious regular structure. That said, the logging I suggested there is also worth doing here, it will let you see the values you are working with and when the conditions are or are not being met.

Thank you, i had read through that and other posts regarding JS and PY in pavlovia, feels like i am banging my head against the wall. I will keep trying and utilise the logging also.

The trials are set to random, but the structure is pretty regular, each loop is a new random line from the cond file and when it gets to the 68th loop show the pause routine, and then another 68 and show the pause routine and finally another 68 and then the pause routine and then 68 and then end (thank you) routine. I could do it by breaking the cond file into 4 parts but it will lose the full randomisation

Right now it looks like the JS doesn’t have the code component at all. Make sure the code component is set to auto->JS or both, so that the code actually appears in the file Pavlovia will ultimately run. When you sync your study to pavlovia, the running window in PsychoPy should tell you it’s compiling the JS script. Is it throwing any errors when it does this?

Ah, sorry, found it. Interesting. So the current behavior is that it always skips the trial, meaning that this condition is always true, even on trials 68 etc.

Let’s try the following JS code:

if (ImageConditions.thisN != 67){

This is basically just an attempt to isolate, one piece at a time, all the things that could be going wrong, and create a little paper trail while we’re at it. If you open the browser’s JS console (in Chrome, it’s under view -> developer), the console.log statement should display the trial number on each trial. If it does not, that means ImageConditions.thisN isn’t what you should use, so you can try it with trials.thisTrialN and such as well. Having it only focus on trial 67 is just to make sure the basic conditional logic works at all. Once we get that working, we worry about trials 105 and 203.

thank you, that is strange behaviour, i will try as you have suggested and get back to you.

Again, your help is greatly appreciated.

forgive me, where will i see the trial numbers in the console log view? I have a lot of what is attached…I amended the code to 3 rather than 67 for the purposes of testing, make it quicker

Hmm…You were supposed to get sth like this (the trial number +1):
Could you please post screenshots of your imagetrials excel or csv condition file and your imageconditions excel or csv file?
Another possible fix that worked for me when I had a similar issue (I am saying similar because i didn’t have two loops, trials+conditions, but just one, a trial loop) is to:
1.Make your own custom varibales which you create in the begin experiment tab:


2.You increment them by one in begin routine tab:


3.Then you use the code I mentioned on my previous post but this time referring to your own variable:

if (my_condition!==67 &&.....){

This is how I managed to solve it and this is how i run my experiment at the moment.

But please do post your excel or csv files, so to rule out the possibility of sth not working as expected because of them.

What is sth? I had never used psychopy before so apologies if i seem clueless. what does sth mean? My conditions file is huge, what exactly did you need to see on it? It is CSV, it contains the names of other CSV files that are selected at random, that then contain image 1, and image 2 (stimuli and stimuli 2) that are shown in the experiment. that is one loop, then it does it again. The Conditions file contains the CSV file names, the onset times for each image the end time for each image, onset time for mask and fixation cross and end time for these too.

Conditions file header row screen shot, and screen shot of one of the headers in the CSV files listed in the first column of the conditions file.

Screenshot 2020-04-22 at 10.48.49

@Jonathan asked you to console.log(ImageConditions.thisN); and from what you posted you were getting only 272, 272, 272… So it seems like no increment was taking place. What is this 272? Do you have 272 conditions (which means rows if we speak for a csv file) in your ImageConditions file? If yes, then it does not increment as it should through your various conditions.

yes there are 272 rows in the conditions file. it is meant to select randomly each time 272 times, the image sets it brings up when testing the study are indeed random and when i run through it all there are all 272 lines used randomly.

Didn’t see the further replies initially. As @phoenix says, the numbers in purple are the console.log output. So, ImageConditions.thisN is just reporting the total number of trials. Change the console.log to tell you ImageConditions.thisTrialN or trials.thisTrialN and see if they increment appropriately.

i have tried replacing ImageConditions.thisN with ImageConditions.thisTrialN and with trials.thisTrialN and i get 0 now where it was 272 previously

Sorry, didn’t read the comment all the way. trials.thisTrialN failing to update is weird, that one should be reliable.

At this point it might be worth creating your own variable to keep track of trial number, as annoying as that is. In beginExperiment, you would put:

trialCounter = 0;

And in begin routine (yes, even in the JS), you would put:

trialCounter = trialCounter + 1;

Try basing the conditional on the value of trialCounter, and logging the value of trialCounter as well.

1 Like

so instead of ImageConditions.trialN or variables use

if (trialCounter != 3){


Yes, exactly. We’re just creating a parallel counter structure.