psychopy.org | Reference | Downloads | Github

My code component doesn't work in the online experiment, though works fine on desktop psychopy version


#1

versiontest.psyexp (14.7 KB)

Hi,

I have a simple code component works fine in the desktop version psychopy, but doesn’t work when it become online. Does code component current not supported in online version?

I attached my psyexp file. basically, I just want to use like ‘if key_resp.corr: loop.finished = 1’ ; ‘if key_resp.corr: continueRoutine = False’…

Really appreciated if anyone could help?..


#2

ah…Just find the information that code component currently is not supported in online version.


#3

@Chengyang, the code component does work in the recent versions of Psychopy. Which version are you using?


#4

Hey, I am using 3.00b9…I mean the online experiment, not the desktop version.
Do you mean code component work in online version? o.o


#5

The code component now has options to write JS and Python code, which you select from a drop-down menu in the code component dialog. The code you enter into the tabs when JS is selected from the drop-down menu will be automatically written into your JS code. However, the code must be written in JS as there is currently no interpreter that will translate your Python code into JS code.


#6

That’s great news!! Thanks. I almost give up, but now I have hope!
However, I have limited knowledge about JS. Is there a way to translate py code to JS? Below is the only thing I need to translate…
if key_resp_1.corr:
loop_1.finished = 1

if key_resp_2.corr:
continueRoutine = False

nCorr = trials_2.data[‘key_resp_8.corr’].sum()
msg = “You got %i out of 30 trials correct!” %(nCorr)

face_counter = random()
if face_counter>0.5:
continueRoutine = False


#7

For the first part of the code (If key_resp_1.corr: loop_1.finished = True;), I made it to be: If (key_resp_1.corr) {loop_1.finished = True;}
It said True undefined… I am confused… could I get some help please? If someone knows.


#8

I’m not familiar with JavaScript but I think the equivalent of True is true (i.e. paying attention to case is often important in programming).


#9

Hi Michael,
Thanks for your suggestion.
I tried low case ‘true’ before. But the code function still didn’t work, just like there is no code…I mean the loop_1.finished = true have no effects to experiment…though the error msg: True undefined don’t come anymore.


#10

Thanks for your last answer.
I spend long time to figure out how to translate my simple Python code to JS code.
I think I partly success
From Py

If key_resp_2.corr:
trials.finished = 1

to JS

If (key_resp_2.corr >0) {
trials.finished = true;
}

However, it still doesn’t work. I think the problem is at “trials.finished = true”, do you know how to code that in JS? or could you give some hint? so I can continue exploring. Really don’t want to give up.
Thanks in advance!


#11

HI @Chengyang, in future, could you please surround your code in backticks (e.g., var = 1? That way it is easier to read.

So, try this instead. In your presentation routine, add the following to the end routine tab of the code component:

if (key_resp_2.corr == 1) {
    trials.finished = true;
} else {
    trials.finished = false;
}

Then in your feedback routine , add the following to the “every frame” tab of the code component:

if (trials.finished) {
    continueRoutine = false;
}

#12

Hi @dvbridges Thanks a lot!
I tried what you suggested. The second one works, but the first one that I quote doesn’t work. No matter I put it into end routine tab or even every frame tab, and I think someone have the exactly same problem. (Online study with loops not progressing correctly)
Besides, I use windows 10 and psychopy 3.00b9…

BTW, thanks. I learned how to proper post code now. :smile: