Code Component not working when running in Pavlovia

@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:

consoel.log(ImageConditions.thisN);
if (ImageConditions.thisN != 67){
    continueRoutine=false;
} 

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):
image
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:

my_condition=0;

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

my_condition+=1;

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

if (my_condition!==67 &&.....){
continueRoutine=false;
}

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

‘’’
console.log(trialCounter);
if (trialCounter != 3){
continueRoutine=false;
}

‘’’

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

brilliant. So attached are the changes to the code component made, and the console.

i got the pause as needed after 3 trials! Amazing.

so this indicates that there was something wrong with how i wrote the code originally? So now all i need to do is add in the 2 other additional pause instances to the now existing code…? (3 , 6 , 9 would be the 3 pauses i actually want if testing works…

‘’’
console.log(trialCounter);
if (trialCounter!= 3 && trialCounter != 6 && trialCounter != 9){
continueRoutine=false;
}

‘’’

1 Like

More something weird about the behavior of [loop].thisN or .thisTrialN, which aren’t working for you, for reasons I don’t understand. Notably, people have gotten this to work online with trials.thisTrialN in other threads, so I’m not sure why that didn’t work here. In any case, we just needed something that worked.

Yes, that’s a perfect next step. If that works you should be good to go.

1 Like

amazing, thank you ever so much for your assistance

Good that at least you figure out a working solution. The trialhandler’s behavior is really odd sometimes in JS,so bare in mind the safe side to go is to create your own custom variables which most of the times do the job right!

thank you for your help :slight_smile: it is weird how it was behaving and it my first foray into the pavlovia world, and the assistance was amazing. Hopefully i have no issues with output etc which is the next part of the journey and then linking the study with academic prolific etc. It is fun but frustrating also.