Skip to main content

Variable fields

A variable field stores a string as its value, and a string as its text. The value is an ID of a variable, while the text is the name of a variable.

Variable field

A block with the label "variable:" and a dropdown field with "x"
selected.

Variable field with editor open

The same block with the dropdown open. The menu has the items "x", "Rename
variable..." and "Delete the "x"
variable"

Variable field on collapsed block

The same block after being collapsed. It has the label "variable: x" and a
jagged right edge to show it is
collapsed.

Creation

Untyped

{
"type": "example_variable_untyped",
"message0": "variable: %1",
"args0": [
{
"type": "field_variable",
"name": "FIELDNAME",
"variable": "x"
}
]
}

Typed

{
"type": "example_variable_typed",
"message0": "variable: %1",
"args0": [
{
"type": "field_variable",
"name": "FIELDNAME",
"variable": "x",
"variableTypes": ["Number", "String"],
"defaultType": "Number"
}
]
}

The variable constructor takes in an optional variable name, an optional validator, an optional array of variable types, and an optional default type.

  • The variable name should be a string. This will be the name of the initial variable the field holds. If it is null or undefined a unique name will be generated.
  • The variable types should be an array of strings. This tells the field what types of variables the field can hold (i.e. what types of variables to add to the dropdown). If it is null or undefined, all variable types will be accepted (and added to the dropdown).
  • The default type should be a string. This will be used when creating the field's initial variable model. If this is defined, its should be included in the variable types array. If it is null or undefined this value defaults to an empty string, meaning the initial variable will be flexibly typed.

→ For more information on strict typing, see Type Checks.

Serialization

The JSON for a variable field looks like so:

{
"fields": {
"FIELDNAME": {
"id": "QJD^+@[RVIwbLSZoDb:V"
}
}
}

Where FIELDNAME is a string referencing a variable field, and the value is the ID of the variable the field references.

If you are using this field in the toolbox, you can also specify the name and (optional) type directly, since there will be no variable available to reference.

{
"fields": {
"FIELDNAME": {
"name": "my_variable",
"type": "string"
}
}
}

Creating a variable validator

note

For information on validators in general see Validators.

A variable field's value is a string, so any validators must accept a string and return a string, null, or undefined.

Here's an example of a validator that only accepts some predefined variables as options. These variables would need to be defined with the Workspace.getVariableMap().createVariable function when the workspace is loaded.

function(newValue) {
var validIds = ['Worf', 'Riker', 'Picard'];
if (validIds.indexOf(newValue) == -1) {
return null;
}
return newValue;
}

An animated GIF showing the validation function at work. When "Picard" or
"Riker" is chosen from the dropdown, the dropdown is set to that choice. When
"x" is chosen, the dropdown is set to the previous choice, which is
"Riker".