Help Request: ReferenceError: image1 is not defined

Description of the problem:
Problem Description:

I am developing an experiment and running it on the Pavlovia platform. The experiment involves dynamically updating two image components (image1 and image2) during the task. I attempted to update the image paths using the following code:

javascript

image1.image = "reward.bmp";
image2.image = "punishment.bmp";

However, the following error appears in the browser console during execution:

arduino

PsychoJS.js:873 ReferenceError: image1 is not defined
    at Scheduler._currentTask (igt.js:1579:10)
    at Scheduler._runNextTasks (Scheduler.js:225:24)
    at async Scheduler._runNextTasks (Scheduler.js:232:13)
    at async Scheduler._runNextTasks (Scheduler.js:232:13)
    at async update (Scheduler.js:139:18)

Relevant Code Snippet:

Here is the key part of my code where the error occurs:

javascript

if (_pj.in_es6(choice_resp.keys, choices)) {
    let [show1, show2, win_list, lose_list] = choices[choice_resp.keys];

    // Attempt to update the image components
    image1.image = "reward.bmp";
    image2.image = "punishment.bmp";
}

Could you please advise on how to resolve this issue?

Please could you show the Python code (assuming that it’s an Auto translate code component).

I don’t think bmp image files work online.

I tend to use code of the form image1.setImage("reward.png")

However, if your error is image1 is not defined then the main fix will be to put image1= something in Begin Experiment.

Thank you for your suggestion. I have already replaced the image format with PNG as recommended. Below is the complete Python code I’m using, but I’m still encountering errors. Could you please take a look and advise?

choices = {
    '1': (show1, show1_1, Awin_list, Alose_list),
    '2': (show2, show2_1, Bwin_list, Blose_list),
    '3': (show3, show3_1, Cwin_list, Close_list),
    '4': (show4, show4_1, Dwin_list, Dlose_list)
}


if choice_resp.keys in choices:
    image1, image2, win_list, lose_list = choices[choice_resp.keys]
    actual_win = win_list.pop(0)  
    actual_lose = lose_list.pop(0)  
    winscore += actual_win
    losescore += actual_lose
    wins=min(winscore / max_score, 1) 
    losses=min(losescore / max_score, 1) 
   
    image1.setImage("reward.png")
    if actual_win  <= 60:
        aud_win = 'winsound1.wav'
    elif  actual_win  <= 80:   
        aud_win = 'winsound2.wav'
    elif  actual_win  <= 100:   
        aud_win = 'winsound3.wav'
    elif  actual_win  <= 130:   
        aud_win = 'winsound4.wav'
    elif  actual_win  <= 159:   
        aud_win = 'winsound5.wav'
    elif  actual_win  > 159:   
        aud_win = 'winsound6.wav'
        
    if actual_lose == 0:
        aud_lose = 'blank.wav'
        t1=4
        t2=0
    else:
        if actual_lose <= 100:
            lose_rank = 'Minor'  
            aud_lose = 'losesound1.wav'
        elif actual_lose <= 200:
            lose_rank = 'Moderate' 
            aud_lose = 'losesound2.wav'
        elif actual_lose <= 300:
            lose_rank = 'Serious'  
            aud_lose = 'losesound3.wav'
        elif actual_lose <= 400:
            lose_rank = 'Severe'   
            aud_lose = 'losesound4.wav'
        elif actual_lose > 400:   
            lose_rank = 'Critical'
            aud_lose = 'losesound5.wav'
   
        image1.setImage=("punishment.png")
        t1=2
        t2=2
        
    print(wins)
    print(losses)
    fd_win = f'You got {actual_win} points !'
    fd_lose = f'You lose {actual_lose} points !'
    thisExp.addData('actual_win', actual_win)
    thisExp.addData('actual_lose', actual_lose)

Is your error still image1 is not defined?

Since image1 is an image component then image1 is not defined either means that your code is in Begin or Before Experiment or you are also using image1 for something else as well.

Thank you for your suggestion. The error “image1 is not defined” is indeed appearing, but my code is in the “End Routine” section, not in “Begin” or “Before Experiment.” Also, I am not using image1 for anything else. Could there be another reason for this issue?

I can’t think of another possibility unless you’ve manually edited your JavaScript code.

Please could you upload a copy of your psyexp file?

The link contains all the files for my experiment.

Thanks for your help.

Thank you.

image1 is not the name of an image component. It is a variable that you try to set to be an image object. It therefore probably needs to be defined in the same way as other variables. My crossed out answer in Help Request: ReferenceError: image1 is not defined - #3 by wakecarter still stands.

Try image1 = 0 in Begin Experiment. I think it should be okay not to be set to be an image object. The important thing is to give the variable image1 scope across the whole experiment.