The following table lists keywords and statements that can be used in a script for better control flow.
Name | Description | |||||
| The | |||||
| A | |||||
| The | |||||
| A
A | |||||
| Terminates a | |||||
| The | |||||
| The | |||||
| The | |||||
| A function is a block of code designed to perform a particular task. Functions can take in a set of arguments and can return a value. Functions are one of the primary methods for organizing and re-using code. | |||||
| Ends the function execution, and the function will return the specified value to the caller. | |||||
| Can be used within a function to get a list of values that were passed into the function call. | |||||
| The | |||||
| Throws an |
Examples for if
…else
and else if
:
In this example, a notification saying “boolTag is true” will be displayed if the tag’s value is true.
...
Note that the if
and else if
conditions must be enclosed in parentheses.
Examples forwhile
:
The tag "A" will be incremented from 0 to 100 over the course of one second:
Code Block | ||
---|---|---|
| ||
var val = 0; while (val <= 100) { tag.write("A", val); val = val + 1; thread.msleep(10); } |
Example for do
…while
:
The tag “boolTag” will be toggled at least once. If the tag “doLoop” is true after one second, “boolTag” will continue to toggle once per second. Once “doLoop” becomes false
, the toggling will stop and the script will end.
Code Block |
---|
do { tag.write("boolTag", !tag.read("boolTag")); thread.sleep(1); } while (tag.read("doLoop")); |
Examples for for
A for
loop lets you provide 3 expressions that define the behavior of the loop.
...
Code Block | ||
---|---|---|
| ||
for (var i = 0; i <= 100; i++) { tag.write("FOR_VAL", i) thread.msleep(100) } |
Looping Over Arrays
A for
loop can also be used to iterate through arrays using the array index.
Code Block |
---|
// Define an array var myArray = [ "val1", "val2", "val3" ] // Get the indices for (var index in myArray) { notification.send(index); } // 0, 1, 2 // Get the values for (var index in myArray) { var value = myArray[index]; notification.send(value); } // val1, val2, val3 |
Looping Over Objects
A for
loop can also be used to iterate through objects using keys.
Code Block |
---|
// Define an object var myObject = { "key1": "val1", "key2": "val2", "key3": "val3" } // Get the keys for (var key in myObject) { notification.send(key); } // key1, key2, key3 // Get the values for (var key in myObject) { var value = myObject[key]; notification.send(value); } // val1, val2, val3 |
Example forcontinue
The continue
keyword can be used in a while
or for
loop to move to the next iteration without breaking out of the loop. This can be used to skip specific items.
...
Code Block | ||
---|---|---|
| ||
for (var i = 0; i < 10; i++) { if (i == 5) { continue; } notification.send(i); } // 0, 1, 2, 3, 4, 6, 7, 8, 9 |
Examples for switch
…case
, break
, and default
In this example, when the script executes, the tag "REPLY" will be assigned the string value "Hello, <name>" by default. If the name entered is empty, the “REPLY” tag will be set to "Could not load name." instead.
...
The break
statement is used to break out of the switch
statement and prevent any other case
s from executing. If break
is not used, then multiple cases (including the default) may be executed. This can lead to unexpected behavior, so in most cases, each case
should have a break
statement.
Examples for function
,return
, and arguments
...
Code Block | ||
---|---|---|
| ||
function multiply(a, b) { return a * b; } var x = multiply(4, 3); // x is now equal to 12 |
Optional Arguments
Functions do not need to have any arguments. For example, here is a function that outputs a message, waits for 3 seconds, then exits the project.
...
Code Block |
---|
function multiplyDefault(a, b) { if (b === undefined) { b = 10; } return a * b; } multiplyDefault(2, 3); // 6 multiplyDefault(2); // 20 |
The arguments
keyword
There is also a special keyword arguments
, which gives an array containing the values passed into the function call. This allows functions to use a variable number of arguments.
...
Code Block |
---|
function returnFirstExtraArg(x, y, arguments) { return arguments[0]; } returnFirstExtraArg("a", "b", "c"); // "c" |
Examples for try
…catch
and throw
A try
…catch
statement can be used to gracefully handle situations that would normally cause the script to terminate. If the code in the try
block fails, this error is returned to the catch
block, which either makes use of or ignores the error.
...
However, throw
can also be used outside of a try
block, in which case the entire script will terminate with an error. This can be useful for scripts that are called via system.importScript
, or when you need to prevent the rest of the script from executing.
Checking Whether a Tag is Loaded or Not
When a project first starts, some tags may not be loaded yet, in which case tag.read
or tag.write
will throw an error. Remote tags may take a few seconds to load, depending on the polling rate, communication method, and the size of the project.
...
Code Block |
---|
// Inside main startup script "Startup1" var loading = true; var waited = 0; while (loading) { try { tag.read("remoteTag"); loading = false; } catch (error) { if (waited > 10000) { throw Error('Could not load tag "remoteTag" after 10 seconds. Please check the connection and restart the project.'); } thread.msleep(100); waited += 100; } } // Now do any other startup actions |
Checking an Error Code From a Tag
The example below checks the value of an error code tag “errorCode” before attempting to turn a motor on using the tag “motorOn”. Depending on the error encountered (if any), the script will display a different notification. If there is a problem with tag.read
or tag.write
, this will also be handled using the notification instead of terminating the script.
Code Block |
---|
try { var errorCode = tag.read("errorCode"); switch (errorCode) { case 0: tag.write("motorOn", true); break; case 1: throw Error("Power Disconnected"); break; case 2: throw Error("Mechanical Failure") break; case 3: throw Error("Invalid Operation Mode") break; default: throw Error("Unrecognized Error Code") } } catch (error) { var errorString = "Could not turn on motor: " errorString += error.message; notification.send(errorString) } |
Built-In Functions for Script Importing and Execution
Canvas also provides built-in functions which allow you to utilize scripts from within other scripts.
Name | Description |
| Calls another script in the project directly by name. This pauses the calling script until the target script finishes executing. This can also be used to import functions and variables from other scripts. |
| When an external program is called directly, the caller will be in standby mode until the called program (the external program) ends its operation. However, if an external program is called using |
Examples forsystem.importScript
and system.runScript
...
Code Block |
---|
// C notification.send("In C"); |
Running Scripts in Order
We can use system.importScript
to execute the scripts in order, like so:
...
While the target script is running, the calling script is paused. If there is an error in the target script, the calling script will have an error as well.
Running Scripts Immediately
Scripts can also be run in parallel using system.runScript
. This immediately begins running the script on another thread.
...
Note that with runScript
you cannot guarantee the order of execution. In this case, the order of steps 1 and 2 may be reversed, as well as steps 4 and 5.
Importing Functions and Variables
system.importScript
can be used to import functions and variables from other scripts. In this example, we have a script called "myFunctionScript", which has a function fib
that returns the Fibonacci sum:
...
Code Block |
---|
system.importScript("myVariableScript"); notification.send(x); // 7 |
Getting a Return Value From a Script
system.importScript
returns the value of the last expression in the target script.
...