Global and local variables are useful when you want to compute some variable from the participant's responses and store this variable for later use.

get_global(key, state)

set_global(key, value, state)

get_local(key, state)

set_local(key, value, state)

assert_global_is_null(key, state)

Arguments

key

The variable's key (character scalar).

state

The participant's state object.

value

Value to set the variable to.

Value

get_global and get_local return the respective variables with key equal to key, or NULL if the variable has yet to be defined.

Details

You can set global or local variables within expressions that are run during the participant's testing session, for example in code blocks (code_block), or during on_complete functions (see page). The variable will be preserved for the duration of the participant's testing session until it is overwritten.

For tests without modules, global and local variables behave the same. The difference between local and global variables is that local variables are encapsulated within a given module (begin_module), and are wiped at the end of the module (end_module).

Use get_global and get_local to get variables. Use set_global and set_local to set them.

assert_global_is_null throws an error if the specified global variable is not NULL. This is useful to catch cases where the code would otherwise overwrite a pre-existing variable.