Builder JS Compilation Error (undeclared variable)

macOS Sequoia 15.3.2, macBook Air M1
PsychoPy v2024.2.4, python 3.12.1 (macOS terminal: python3 --version)
Standard Standalone Installation? yes
Do you want it to also run online? yes

Hi there,
i have created an experiment for a project at my university. It runs fine locally but won’t run on pavlovia. In the browser Debugger it says Uncaught ReferenceError: assignment to undeclared variable partid. In the first routine i have custom code (Auto->JS):
partid = randint(1,999999) which gets translated to partid = util.randint(1, 999999);. ChatGPT says that it should be var partid = util.randint(1, 999999);. I searched here for compilation error and found nothing fitting for my case, mostly things about other parts of other routines having faulty code. While that may be the case also, i created a new empty experiment, added the custom code ARandomVar = "Hi There" and it still compiles without the var statement, so compiling seems somewhat broken on my machine.

I also looked for requirememts to run/compile code, but couldn’t find any on the installation page.

wkr

// Edit: The Code is in the “Before Experiment” Tab of the custom code.

// Edit: As is understand from Issues With Debugging PsychoJS files - #6 by dvbridges, by not compiling with a var statement, psychopy tries to declare the variables globally. I tried with newest Firefox and Edge versions now locally and with pavlovia, they both are upset with the missing var.

Dear Tegg,

If you could include your code that would help in terms of figuring out what’s wrong.

Without it though, it seem like you forgot to create the variable of “partid”.

You need a line of

var partid

partid = util.randint(1,9999);

See the photo for what I mean:

Best regards,

Issac

Hi Isaac,
thanks for your reply. The first occurence of the error looks like this:

As i said, i wrote it in regular python and the code component is set to “Auto->JS” which should translate the code to JS fine, as per the documentation.

The error it produces is:
grafik

Greetings

Move your code to Begin Experiment.

Give df_cond a default value.

Thanks wakecarter, i think this cleared up my understanding of the tabs in the code component. I moved most variables to “Begin experiment”.

I need partid for the name of the data file, so i switched the code component to “Both” and declared it “Before Experiment” with var. This seems to work fine, partid is available throughout the experiment.