Run a custom javascript code in the graph editor.

This can be useful to run complex conditional operations, complex maths or to interact with an HTTP object that can be created in the Devices tab.

You can add as many inputs and outputs to the node as you require. Here it is shown with 2 inputs and 2 outputs.


Properties


This node has several tabs in the Editor panel :

  • General : Activate Trigger mode and select between Internal or External script mode.
  • Input : Add inputs, rename them and choose their types.
  • Output : Add outputs, rename them and choose their types.
  • Script : (If in Internal script mode) Input for short and simple scripts.

Inputs


Name Type Description
Input_# Variant Input number #

By default the inputs are labelled input_# with # incrementing with each input. They can all be renamed in the Input tab of the Editor panel. The default type is Float.

Outputs


Name Type Description
Output_# Variant Output number #

By default the outpus are labelled output_# with # incrementing with each output. They can all be renamed in the Output tab of the Editor panel. The default type is Float.


Code


When in Internal script mode, there are two sections in the Script tab:

  • Global section will execute once when the graph is loaded or when the script is modified
  • Main section will execute continuously (or when triggered if in Trigger mode)

Trigger mode


If Trigger mode is activated in the General tab an input named Trigger will appear on the node and in the Inputs tab.

In trigger mode, the script will only execute when a Trigger Event is received on this input.

This concerns only the part of the script that is in the Main section (internal mode) or update() function (external mode).

The Global section (or init() function) will still execute only once when the project/graph is loaded or when the script is modified.


External scripts


When writing scripts in external files (.js extension) take care to use the following format:

function init() {
// Equivalent to the "GLOBAL" section
}
function update() {
// Equivalent to the "MAIN" section
}

Visual Code


We recommend using Visual Studio Code to work on large scripts.

It comes with many quality-of-life improving features such as auto-completion, auto-indent and color syntaxing making coding easier.

When opening an external script with the Javascript node, the following files will be automatically copied into the folder containing the script: modulokinetic.d.ts and tsconfig.json

The presence of these fils in the folder will enable auto-completion and tooltips to work with Visual Code’s intelliSense for every script file in the folder.

Alternatively you can also include this line at the top of your .js file:

/// <reference path="modulokinetic.d.ts" />

If the modulokinetic.d.ts file is in the same folder as your script auto-completion will work.

You can also add this line at the top of your javascript :

// @ts-check

In this case, Visual Studio Code will check that each designer. function has all parameters assigned with the correct type.

Note that with this option activated Visual Studio Code will also send an error message for every use of your input and output pins as they are not declared in the script.

To avoid this you can declare your input and outputs at the beginning of your function update() without assigning a value to them.

For example:

function update() {
var input_1;
var input_2;
var output_1;
var output_2;
...
}

Examples


Need more help with this?
Don’t hesitate to contact us here.

Thanks for your feedback.