Code Component not working when running in Pavlovia

I have built the experiment in the builder (Latest Psychopy version) and have the attached code component sitting in the Pause Routine.

In the code component i have used the code in begin routine (where it is now and that works in psychopy on the laptop).

when uploading the experiment with code component in Begin Routine into Pavlovia and then running, it runs like the component isn’t there and doesn’t insert the pause where required.

I have tried using the code component by putting the code in Each Frame also and uploaded that into Pavlovia but that did not work either as it just inserted the pause every frame.

My experiment structure (builder view) is attached also.

the Builder auto creates the JS however it is just not working.

Does anyone have any idea how i can get this to work correctly please?

Any advice or help would be greatly appreciated.

1 Like

Do you get an error message? Can you post the JS code which was automatically translated? Some functions of Psychopy do not work in JS.

i don’t get an error message at all, the experiment just runs and doesn’t put the pause in until the end of all the trials, rather than at the intervals mentioned in the code…below is the JS element auto generated in the code component

var _pj;
function _pj_snippets(container) {
function in_es6(left, right) {
if (((right instanceof Array) || ((typeof right) === “string”))) {
return (right.indexOf(left) > (- 1));
} else {
if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
return right.has(left);
} else {
return (left in right);
}
}
}
container[“in_es6”] = in_es6;
return container;
}
_pj = {};
_pj_snippets(_pj);
if ((! _pj.in_es6(ImageConditions.thisN, [67, 105, 203]))) {
continueRoutine = false;
}

thank you for taking a look

can you try clicking on the drop-down list after code and select BOTH (instead of PY)?

i had selected both, i was just showing what the original PY was…

the below is what is auto generated when select Auto > JS using the PY mentioned before.

var _pj;
function _pj_snippets(container) {
function in_es6(left, right) {
if (((right instanceof Array) || ((typeof right) === “string”))) {
return (right.indexOf(left) > (- 1));
} else {
if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
return right.has(left);
} else {
return (left in right);
}
}
}
container[“in_es6”] = in_es6;
return container;
}
_pj = {};
_pj_snippets(_pj);
if ((! _pj.in_es6(ImageConditions.thisN, [67, 105, 203]))) {
continueRoutine = false;
}

thank you for taking a look

How about varying the length of the pause instead?

Alternatively, put a loop around the pause routine with nReps being a variable 0 or 1 based on whether you want a pause or not (which I how I deal with this situation)

Best wishes,

Wakefield

I want the pause every 68 trials (x4) the pause has no set length as it is until a key is pressed.

I tried the loop separately around the pause routine before I started with the code component, I had no success with that method. I will try as you have suggested though, thank you.

i have tried that suggestion with no luck. The PY code works perfectly in the builder, there must be a simple way of utilising the same logic in JS. It is just beyond me

Put continueRoutine=false in “Every frame” instead of “begin routine”, and try using trials.thisN or trials.thisTrialN instead of ImageConditions.thisN.

ImageConditions is the name of the file that contains the trial list.

when i put continueRoutine=false in ‘each frame’ and try using trials.thisN or trials.thisTrialN or even remove the countineRoutine=false to ‘each frame’ and leave the if not code in ‘Begin Routine’ i get

/* Syntax Error: Fix Python code */

when it coverts the PY in ‘Begin Routine’ to JS it says the above

That’s odd. I’d have to see the full code to figure out why, though. Can you post the full content of your code component, from each tab? When you post it on the forum, if you put ``` above and below it, it will render as formatted code, which will make it easier to see indentation and stuff.

It might be as simple as the fact that False needs to be capitalized in Python (which is my bad, I gave you straight JS, not thinking about the auto->JS setup).

There are several users having similar issues. The auto-JS does not translate this python code correctly. It took me a while to figure this out. Let’s start with this–> Leave the python code as it is. Enable Both in the code dropdown menu. On the right side which is the JS code put this (On Each Frame Tab) :

if (ImageConditions !==67 && ImageConditions !==105 && ImageConditions !==203){
    continueRoutine=false;
}

If this does not fix the problem, just let me know there is one more thing you could try out.

thank you. I will try this now. just to clarify, the PY Code stays in begin routine as posted in original post, remove what is translated to JS in that tab, then insert the code you suggest into the JS window on the right in the each frame tab?

Exactly.

just made those changes in the code component in Psychopy Builder (Screen shots attached)…

synced the experiment with Pavlovia and then tried to run online but the pause happens still after each trial (like the code component is not there) … it took ages getting it ok in builder and i was pleased with that effort but this JS element to the online running is really stumping me.

All help to everyone who has messaged is really appreciated, hopefully can get to the bottom of this somehow :slight_smile:

Hi,
I have a similar problem and I didn’t manage to implement your solution, although it seems as though it’s the exact solution I need.
I inserted a randomization code and set the method as sequential.
My python code in the builder is:
##Begin Experiment:

oneback = None
twoback = None
threeback = None

##Begin Routine:

if sit_trials.thisN == 0:
    stimulusList = [i for i in sit_trials.trialList]
    shuffle(stimulusList)
    Stimulus = stimulusList[0]['Audio'] 
    condition = stimulusList[0]['condition']
    sound.Sound(Stimulus)
    sit_trials.addData('Audio', Stimulus)
    sit_trials.addData('condition', Condition)
    sit_trials.trialList.pop(Stimulus)
if oneback == twoback == threeback == condition: # if three following trials have belonged to the same condition
    stimulusList = [i for i in sit_trials.trialList if i['condition'] != oneback and i['condition'] != 'Catch']
    shuffle(stimulusList)
    Stimulus = stimulusList[0]['Audio'] 
    condition = stimulusList[0]['condition']
    sound.Sound(Stimulus)
    sit_trials.addData('Audio', Stimulus)
    sit_trials.addData('condition', Condition)
    sit_trials.trialList.pop(Stimulus) 
if sit_trials.thisN != 0 and sit_trials.thisN % 10  == 0:
    catchList = [i for i in sit_trials.trialList if i['condition'] == 'Catch']
    shuffle(catchList)
    Stimulus = catchList[0]['Audio']
    condition = catchList[0]['condition']
    sound.Sound(Stimulus)
    sit_trials.addData('Audio', Stimulus)
    sit_trials.addData('condition', Condition)
    sit_trials.trialList.pop(Stimulus)

When syncing it with Pavlovia the code seems to be omitted and the stimuli are appearing sequentially.

I tried translating it manually but without any success.
Thank you very much!
Nitzan

@Stuart_Furnell In your JavaScript, you need ImageConditions.thisN or ImageConditions.thisTrialN instead of just “ImageConditions”, and if that doesn’t work, use trials.thisTrialN (“trials” always refers to the current loop, and seems more reliable than using the name of the loop for some reason).

@N_T can you share your JavaScript as well as your Python? Note that you need Util.shuffle() in JS instead of just “shuffle”.

@jonathan.kominsky is that referring to the most recent screen shot of the JS in the each frame element with the PY code still in the Begin Routine?

below is what i have put in the JS element of the code component for Each Frame and the PY in begin routine is still as it was…is this what you meant?

‘’’
if (ImageConditions.thisN !==67 && ImageConditions.thisN !==105 && ImageConditions.thisN !==203){
continueRoutine=false;
}

‘’’

Yes, the JS. It’s in the right place, but “ImageConditions” itself doesn’t have a numeric value, it has the numeric properties thisN or thisTrialN, hence ImageConditions.thisTrialN

I’d also recommend using != instead of !== but that shouldn’t make too much difference.

ahh sorry didn’t see you speedy response thank you :slight_smile: