Cannot reset value of newVar

PennController for IBEX Forums Support Cannot reset value of newVar

Tagged: 

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #6876
    jherringt
    Participant

    Hi pcIBEX,

    I am trying to create an experiment where the result of a keypress event sets the value of a trial-wise variable (newVar). I’ve seen several examples that would suggest that the code below would accomplish this, but for some reason, the newVar never gets updated. I suspect that either I have some kind of syntax problem, or I am misunderstanding the newVar variable scope. In the example below, I create a newVar called ‘audFile’, then immediate;y update the value of this variable using the command getVar(“audFile”).global().set( “variable is changed” ), then print the value of the variable to the screen – showing that the variable value was never changed. Further down, I then have a keypress event, the success or failure of which ought to again change the ‘audFile’ variable… and again, it does not. Could you help me identify why my code does not appear to succeed in changing the value of this variable? Thank you!

    Here is the code:

    ___________________________________

    PennController.ResetPrefix(null) // keep this line here
    var showProgressBar = false;
    
    // This experiment prints a letter to the screen, and the subject
    // is just support to type it.
    
    // Create an experiment table
    AddTable("testTable", `stim,corrResp
    a,a
    s,s`)
    
    // Create experiment template, and loop over trials
    Template("testTable", row=>
        newTrial(
            
            // Initialize variable
            newVar("audFile","None").global(),
    
            // Try setting the variable right away, then print it to the screen
            getVar("audFile").global().set( "variable is changed" ),
            newText( "This should say 'variable is changed' but instead says " + getVar("audFile").value ).print(),
            newTimer(3000).start().wait(),
            clear(),
    
            // Print letter to be typed
            newText("letter", "Now please press " + row.stim)
                .css("font-size", "24pt")
                .log()
                .print(),
            
            // Get keypress
            newKey("keypress", "as")
                .log()
                .wait()
                .test.pressed( row.corrResp )
                .success( 
                    getVar("audFile").settings.global().set(v=>"correct"),
                    newText( "Correct!" ).print(),
                    newTimer(1000).start().wait()
                    )
                .failure(
                    getVar("audFile").settings.global().set(v=>"wrong"),
                    newText( "Wrong!" ).print(),
                    newTimer(1000).start().wait()),
                    
            // Print the final value of the variable
            newText( "This should say either 'Correct!' or 'Wrong!' but instead says:" + getVar("audFile").value ).print(),
            newTimer(1000).start().wait()
    
        )
    )
    #6877
    Jeremy
    Keymaster

    Hi,

    I am not sure where you saw such a use of getVar(...).value, but it’s a method you use to inject native JavaScript in your code. In your case, you’re accessing the value of the Var element before the trial has even started. This guide should bring insights into this issue.

    The proper PennController way of accomplishing what you’re trying to do is:

    newTrial(
        // Initialize variable
        newVar("audFile","None").set( "variable is changed" ),
        newText( "This now says 'variable is changed':" )
            .after( newText().text(getVar("audFile")) )
            .print()
        ,
        newTimer(3000).start().wait(),
        clear()
        ,
        // Print letter to be typed
        newText("letter", "Now please press A")
            .css("font-size", "24pt")
            .log()
            .print()
        ,
        // Get keypress
        newKey("keypress", "as")
            .log()
            .wait()
            .test.pressed( "A" )
            .success( 
                getVar("audFile").set("correct"),
                newText( "Correct!" ).print(),
                newTimer(1000).start().wait()
            )
            .failure(
                getVar("audFile").set("wrong"),
                newText( "Wrong!" ).print(),
                newTimer(1000).start().wait()
            )
        ,
        // Print the final value of the variable
        newText( "This should say either 'Correct!' or 'Wrong!' and it does:")
            .after( newText().text(getVar("audFile")) )
            .print()
        ,
        newTimer(1000).start().wait()
    )

    If your Var element is trial-wise, which I take to mean local (ie. non-global) then you don’t need to use the global command

    Jeremy

    #6878
    jherringt
    Participant

    This solves my problem perfectly – thank you!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.