Var
element
description
Creates a variable that can be manipulated during script evaluation.
syntax
newVar(ELEMENT_NAME: string, DEFAULT_VALUE)
-
ELEMENT_NAME
: The name of the newly-created element. -
DEFAULT_VALUE
: The default value of the element; set at the start of script evaluation.
since
beta 0.3example(s)
+ If a Var element with the same name was made global before the current newVar command is executed, the current newVar command will refer back to that Var element instead of creating a new one, and the default value will be ignored (the global Var element will not be set to the default value).
$newVar("trialsLeft", 3)
@,
@newText("remain", " Number of remaining attempts: ")
@ .after( newText("trial", "3") )
@,
@newTextInput("guess", "Guess my name")
@ .after( getText("remain") )
@ .print()
@ .wait(
@ getTextInput("guess")
@ .test.text( /Jeremy/i )
@ .failure( // Wrong guess:
$ getVar("trialsLeft")
$ .set( v => v-1 ) // Decrease trialsLeft
@ ,
@ getText("trial") // Update trial's text with it
@ .text( getVar("trialsLeft") )
@ ,
$ getVar("trialsLeft") // Disable guess if 0 attempt left
$ .test.is(0)
$ .success( getTextInput("guess").disable() )
@ )
@ )
+ The code above creates a new Var element named trialsLeft and initiates it with the value 3. It then adds a text input box to the page pre-filled with Guess my name, and followed by Number of remaining attempts: 3 to its right. + When the return/enter key is pressed while editing the text input box, the code tests whether its content is Jeremy (ignoring case). If the content of the text input box is not a match, trialsLeft is decreased by 1 and the text is updated. Additionally, if 0 was reached, the text input box is disabled, thus preventing further proceeding. Action commands
var.global
syntax
.global()
description
Makes the Var element accessible from the script that has not been evaluated yet (=~ all the script below .global()
).
example
click to expand
@PennController(
@ newVar("participantsName", "")
$ .global()
@ ,
@ newTextInput("name", "Please write your name")
@ .print()
@ .wait()
@ .setVar( "participantsName" )
@)
@.log("Participant", getVar("participantsName") );
@
@PennController(
@ newText("hello", "How are you today ")
@ .after( newText("name", "").text(getVar("participantsName")) )
@ .print()
@ ,
@ newButton("good", "Good, thank you")
@ .after( newButton("bad", "Not so well") )
@ .print()
@ ,
@ newSelector("mood")
@ .add( getButton("good"), getButton("bad") )
@ .wait()
@)
@.log("Participant", getVar("participantsName") );
- The code above creates two PennController trials: the first one creates a global Var element named participantsName whose value is set to the text contained in the TextInput element at the end of the trial; the second trial prints a Text element reading “How are you today “ and appends another Text element to its right, whose content is set to the value of the global Var element.
var.local
syntax
.local()
description
Makes the Var element accessible only from the script of the current PennController instance (default setting). If a Var element with the same named was made global before (using .global()
—see above) then the Var element reverts to being local (while keeping its current value), meaning that any further Var element with the same name will no longer share the value of the preceding same-named Var elements.
example
click to expand
@
@PennController.Header(
@ newVar("StartTime", 0)
@ .test.is( 0 ) // Won't be 0 if set as global
@ .success( // 0 means local: set time
@ getVar("StartTime")
@ .set( v=>Date.now() )
@ )
@);
@
@PennController.Footer(
@ newVar("TimeDifference")
@ .set( getVar("StartTime") )
@ .set( v=>Date.now()-v )
@ ,
@ newText("elapsed")
@ .text( getVar("TimeDifference") )
@ .before( newText("labelElapsed", "Time elapsed (in ms): ") )
@ .print()
@ ,
@ newButton("validate", "Continue")
@ .print()
@ .wait()
@);
@
@
@PennController( "label" ,
@ getVar("StartTime")
@ .global() // Make global on 1st trial
@ ,
@ newButton("getTimeDiff", "How many ms since display?")
@ .print()
@ .wait()
@);
@
@PennController( "label" ,
@ getVar("StartTime")
$ .local() // Make local again on 2nd trial
@ ,
@ newButton("getTimeDiff", "How many ms since PREVIOUS display?")
@ .print()
@ .wait()
@);
@
@PennController( "label" , // Var is local: 0 => set time (header)
@ newButton("getTimeDiff", "How many ms since NEW display?")
@ .print()
@ .wait()
@);
-
The Header creates a new Var element named StartTime initially set to 0, if no Var element named StartTime was previously made global. When the first of the three label trials is run, there is no global StartTime yet, and the test from the Header is successful: StartTime is set to the current timestamp. The script for the first label trial is then executed: StartTime is made global, and a button is added to the page. When the button is clicked, the Footer creates a new Var element named TimeDifference which is first set to the value of StartTime and then set to the current timestamp subtracted of StartTime. The result is displayed in a new Text element and a button to be clicked for proceeding is added to the page.
-
When the second label trial is run, the StartTime element is global, so its value remains unchanged from the previous trial: the Header practically has no effect. Then the script of the second trial is executed and StartTime is made local again (keeping its current value for the remaining of the trial though). At the end of the trial, since StartTime still has the value set on the first trial, the reported time difference corresponds to how many milliseconds elapsed since the display of the first trial.
-
When the third label trial is run, the Header creates a new Var element named StartTime, initially set to 0: indeed, trial #2 made StartTime local again, and so when the
newVar
command from the Header is executed on trial #3, it ‘sees’ no existing Var element named StartTime. The test is therefore successful and StartTime is set to the current timestamp. The rest of the trial proceeds as in trial #1.
var.log
syntax
.log()
description
Adds a line to the results file indicating the value of the Var element.
example
click to expand
@newVar("image", "none")
$ .log("final", "set")
@,
@newText("instructions", "Press any key")
@ .print()
@,
@newKey("any", "")
@ .wait()
@,
@getKey("any")
@ .test.pressed(" ")
@ .success(
@ newImage("tree", "Tree.png")
@ .print()
@ ,
@ getVar("image")
@ .set( "tree" )
@ )
@,
@getButton("continue", "Continue")
@ .print()
@ .wait()
-
Adds a line of text reading Press any key and wait for any key to be pressed. Adds an image of a tree if the space key was pressed. Adds a button to click to continue.
-
At least one line will be added to the results file, indicating the final value of the Var element (either none or tree, depending on whether the pressed key was the space key). If the space key was pressed, another line will be added, reporting the value and the timestamp corresponding to the
.set
command (and to the key press, since.set
is executed immediately after it).
var.set
syntax
.set()
description
Sets the Var element to the specified value.
example
click to expand
@defaultText
@ .print()
@,
@defaultKey
@ .wait()
@,
@newVar("firstKeyPressed")
@,
@newText("firstPress", "Press any key")
@,
@newKey("firstKey", "")
@,
@getVar("firstKeyPressed")
$ .set( getKey("firstKey")
@,
@newText("printFirstKey", "")
@ .text( getVar("firstKeyPressed") )
@,
@newText("secondPress", "Good, now press a second key.")
@,
@newKey("secondKey", "")
@ .test.pressed( getVar("firstKeyPressed") )
@ .success( newText("same", "So, you pressed the same key twice!") )
@ .failure( newText("diff", "You appear to be quite inconstant.") )
@,
@newButton("validate", "OK")
@ .print()
@ .wait()
- First creates a Var element named firstKeyPressed and then prints a text inviting to press any key. When a key is pressed, it is stored in the Var element (
.set
command). The Var element is then used to print which key was pressed, and when a second key is pressed, it is used to test that the same key was pressed.
Test commands
var.test.is
syntax
.test.is()
description
Tests the value of the Var element. You can pass a function which takes the element’s value as its argument and returns true
or false
.
example
click to expand
@newVar("trialsLeft", 3)
@,
@newText("remain", " Number of remaining attempts: ")
@ .after( newText("trial", "3") )
@,
@newTextInput("guess", "Guess my name")
@ .after( getText("remain") )
@ .print()
@ .wait(
@ getTextInput("guess")
@ .test.text( /Jeremy/i )
@ .failure( // Wrong guess:
@ getVar("trialsLeft")
@ .set( v => v-1 ) // Decrease trialsLeft
@ ,
@ getText("trial") // Update trial's text with it
@ .text( getVar("trialsLeft") )
@ ,
@ getVar("trialsLeft") // Disable guess if 0 attempt left
$ .test.is(0)
$ .success( getTextInput("guess").disable() )
@ )
@ )