Patch Editor
JavaScript to Patch Bridging

Script to Patch Bridging

You can pass information between scripts and the Patch Editor by adding shared variables.

There are 7 variable types supported with some type differences between the Patch Editor and script:

Patch Editor typeScript type
BooleanBoolSignal
ColorColorSignal
NumberScalarSignal
PulseEventSource
TextStringSignal
Vector 2Vec2Signal
Vector 3VectorSignal or PointSignal
Vector 4Vec4Signal

Shared variables can be created from the Inspector Panel after selecting a script file in the Assets Panel.

Sending variables from scripts to the Patch Editor

Sending

To send a variable from a script to the Patch Editor, use the set methods provided by the PatchesModule API.

The following example shows a Boolean being sent from a script to the Patch Editor:

// Load in the Patches module
const Patches = require('Patches');

(async function() { // Enable async/await in JS [part 1]

    // Create a boolean variable
    const myBoolean = true;

    // Send the boolean to the Patch Editor as 'myBoolean'
    await Patches.inputs.setBoolean('myBoolean', myBoolean);

})(); // Enable async/await in JS [part 2]
        

Accessing

To access a variable sent from a script in the Patch Editor:

  1. Select the script file in the Assets Panel.
  2. In the Inspector Panel, to the right of From Script, click + to add a new variable.
  3. Change the variable name to match the name of the variable defined in your script.
  4. Under Interactions select Create Producer Patch.

This will create a purple producer patch with any variables you have defined under the From Script section as outputs. You can connect the output port(s) to other patches.

Sending variables from the Patch Editor to scripts

Sending

To send a variable from the Patch Editor to a script:

  1. Select the script file in the Assets Panel.
  2. In the Inspector Panel, to the right of To Script, click + to add a new script variable.
  3. Change the variable name to match the name of the variable that will be used in the script.
  4. Click the arrow next to the variable.

This will create a yellow consumer patch, the value of which can be read inside the script. You can connect other patches to the input port to change the value.

Accessing

To access a variable sent from the Patch Editor in a script, use the get methods provided by the Patches Module.

The following example shows Text being accessed in a script from the Patch Editor:

// Load in the Patches module
const Patches = require('Patches');

(async function() { // Enable async/await in JS [part 1]

    // Get the 'myString' string value from the Patch Editor
    const myString = await Patches.outputs.getString('myString');

})(); // Enable async/await in JS [part 2]
        
Was this article helpful?