The Script block offers three approaches to ‘talk’ with other virtual blocks that are attached to it. Via ‘Custom Actions’, to allow parent blocks to send data to your Script functions, via ‘Custom Outputs’ which helps you send data to widgets in the Dashboard area and by directly invoking children’s actions.
Custom Actions
To define a custom action you just need to use the ‘Actions’ object and add a new property which represents the function to be executed when a parent block calls the action via the ACTION Gate. Eg:
// Define a custom action
Actions.doSomething = function(value){
notification.success("Parent sent: "+value);
};
From the parent block (the one connected to the left port of your Script block) you call this function through an ACTION Gate. Any value passed to the action becomes the function's argument:
Custom Outputs
You can create any number of outputs that become data sources in the Dashboard area. To define an output just call the ‘createOutput’ helper function. This function returns an object that contains a ‘send’ property that you can invoke when you are ready to ‘feed’ your output:
var counter = 0;
var customOutput = createOutput('myOutput'); // Create a custom output
// Setup a timer and call the anonymous function every half a second (500ms)
setInterval(function(){
counter = counter + 1;
customOutput.send(counter); // Feed the output
}, 500);
From the Dashboard area, you should now be able to choose your custom output as a Data Source for any of the widgets:
Calling Children Actions
Any block connected to the port located on the right side of your Script block is considered a ‘Child’ and you can invoke its actions directly from your code:
var childBlock = getChildren("SoundPlayer_1");
// If there isn't any block labelled 'SoundPlayer 1' connected as a child,
// the variable'childBlock' will be 'false'.
if(childBlock){
childBlock.actions.Beep();
}
Note that the spaces in the child’s name are replaced with a “_”. You can alternatively change the name of your Virtual block.
You can check the full list of methods available to your scripts from the Script Block reference page.