The Code widget is a place to add custom Javascript to process up to four different inputs and send the results to up to four outputs.
Instructions
1 Connect
Connect up to four input and four output widgets. The input and output values are displayed.
2 Write Code
Click to open the more tab, and add your Javascript in the code panel. Each input is an element in the ins associative array.
- Input 1 – ins.in1
- Input 2 – ins.in2
- Input 3 – ins.in3
- Input 4 – ins.in4
The four output values are returned as an array with four elements by the return statement. E.g.
return [ out1, out2, out3, out4 ];
When you’ve completed your code, close the more tab for the code to take effect.
3 Code Example
Here’s a code example demonstrated in the GIF above, where two inputs are tested to determine whether or not a third input value passes through the widget or not. This concept is called “gating” and the code checks to see if ins.in1 and ins.in2 (the top two inputs) both have a value that’s greater than 500. If they do, then the “gate” is opened, and the values coming into ins.in3 (the third input down) are allowed through. Otherwise, the output is zero.
if (ins.in1 > 500 && ins.in2 > 500) { Â return ins.in3; } else { Â return 0; }
4 Code Editing Suggestions
When working on the Javascript in the Code widget, it can be helpful to do the following:
- ALWAYS make a copy of your code and save it in a separate file using a text editor
- Use the below Code Widget Simulator below to develop and test your code before you paste it into the Code widget in a live NTK patch
5 Code Widget Simulator
This Code Widget Simulator allows you to test code outside the NTK environment, where you can find syntax errors and bugs without causing a problem for NTK. Once your code is doing what you want, copy the code from the simulator into the actual Code widget. In the JSBin window, open the Console column to see any errors or the output of any console.log() statements you use.
Enter your code after the comment line:
// put Code widget Javascript here  // your code
To use the simulator, you can work in the embedded version below, or go directly to the JSBin site and use it full screen there:Â http://jsbin.com/deqede/edit?js,output
6 Problem Solving
Once you are working in the NTK authoring system, keep in mind the following:
- Have the browser’s Javascript console open while you code. This way, you can see any error messages that are displayed.
- Chrome: View>Developer>Javascript Console
- Safari: Develop>Show Error Console (to make the Develop menu visible – Preferences>Advanced>Show  Develop menu in menu bar)
- If you have an error in your code, you may need to reload the page, or click on the Save button after you fix the error.
- If all else fails, and the error in your code makes NTK not work, if you are using the command line version, them run:
npm run reset - Otherwise, you should delete the NTK patch file
- In the NTKserver directory, go to modules>nlMultiClientSync
- Delete the file currentPatch.nlp
- Restart NTK