URL of experiment: V1.1_shorttest_JS [PsychoPy]
Description of the problem:
I have the following JS code executed early on in the experiment (where ‘variability’ is specified via conditions in the Excel file):
function generateGaussian(mean, std) {
var _2PI = Math.PI * 2;
var u1 = Math.random();
var u2 = Math.random();
var z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(_2PI * u2);
var z1 = Math.sqrt(-2.0 * Math.log(u1)) * Math.sin(_2PI * u2);
return z0 * std + mean;
}
function varInstance(variability, scaling, shift) {
gg = -999;
do {
gg = generateGaussian(0, variability);
}
while (-3 > gg > 3);
return gg * scaling + shift;
}
Later on, the varInstance function is used in various bits of code throughout the experiment to define stimulus parameters, e.g.
widVar = varInstance(variability, 0.167, 1);
These then throw a ReferenceError: varInstance is not defined.
Try putting your function definitions in Before Experiment
or assigning them to a variable name
varInstance = function(variability, scaling, shift)
Thanks @wakecarter
I already had the function assigned to a variable name BUT I had not defined the function at before experiment (but at begin experiment).
I now moved that to Before Experiment, and when I try to synch the project, I get a Psychopy error:
Traceback (most recent call last):
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/app/builder/builder.py”, line 1381, in onPavloviaSync
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/app/pavlovia_ui/project.py”, line 741, in syncProject
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/app/pavlovia_ui/functions.py”, line 94, in showCommitDialog
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/projects/pavlovia.py”, line 1017, in getChanges
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/projects/pavlovia.py”, line 869, in repo
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/projects/pavlovia.py”, line 993, in configGitLocal
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/projects/pavlovia.py”, line 415, in user
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/projects/pavlovia.py”, line 144, in __init__
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/requests/models.py”, line 900, in json
File “json/__init__.pyc”, line 357, in loads
File “json/decoder.pyc”, line 337, in decode
File “json/decoder.pyc”, line 355, in raw_decode
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Do you mean differently from how you showed the definition in your post?
I’m now wondering whether you have defined the same function twice and have an imbalance of brackets.
I think I am being thick then.
When you wrote
varInstance = function(variability, scaling, shift)
Did you mean to literally use ‘function’?
I took it to mean that I name my function varInstance, and then call that function when I want to use it to assign variables, as in
I meant literally as per my older crib sheet when defining the function, not when calling it.
Hmm. So I found the problem (I had used var as a variable name, not realising that this is a JS command), and the other SNAFUs were associated with missing brackets
But I also used your suggestion of how to define the function and that led to errors (the function name not being recognised). What is strange is that I copied and pasted code to this effect from working JS experiment authored by a colleague and it also produced the error. but, main thing is that it is working now!
Update to previous post: I have figured out that if you want to use the format
varInstance = function(variability, scaling, shift)
then this cannot be in the "before experiment’ tab. It has to be in the ‘begin experiment’ tab
Sorry I hadn’t made that aspect clear. That was why I linked to both versions of my crib sheet. The Before Experiment tab didn’t exist in 2020.