Comments
Users can insert comments anywhere in a script. Comments are not executed like regular code. Instead, they can be used for documentation or temporarily disable blocks of code.
...
Code Block | ||
---|---|---|
| ||
// This is a single-line comment /* This is a * multi-line * comment.*/ |
Calling Functions
A function takes in some parameters and returns a value. Functions let you re-use pieces of code without re-writing everything.
...
For example, the script below calls the function notification.send
function with the argument "Hello, world!"
. This will bring up a notification that says “Hello, world!”:
...
Some functions do not require any arguments, for example system.exit
, which exits the runtime project. In a script, it would look like this:
system.exit();
Other functions require multiple arguments. In this case, arguments should be separated by commas.
For example, tag.write
takes two arguments. The first argument is the tag name, and the second argument is the value to write. The script below writes the value 1
to the tag called "my_tag"
:
tag.write("my_tag", 1);
Declaring Variables
Variables let you store values and access them later in the script.
...
Variables must be declared before they can be used in the script. Variables declared in a separate script cannot be used unless you first import that script using system.importScript
.
Using and Setting Variables
Once a variable has been declared, you can assign a new value to it using the assignment operator =
. For example:
...
Code Block |
---|
tag.write("my_tag", 1); |
Operators, Expressions, and Parentheses
You can add, subtract, multiply, and use other operators on variables and other values. See the “Operators” section for more information.
Code Block |
---|
1 + 2; // 3 (addition)
1 - 2; // -1 (subtraction)
1 * 2; // 2 (multiplication)
1 / 2; // 0.5 (division)
1 == 2; // false (equal to)
1 < 2; // true (less than)
!true // false (not) |
Just like in mathematics, expressions can be combined and nested within each other. The values will be evaluated as soon as they are needed.
Parentheses can be used to group expressions together . This lets you change and to specify the order of operations. For example:
Code Block |
---|
(1 + (2) * 3; //) 9 1 + (2 * 3); // 7 |
You do not need to store values to variables to use them. Expressions will be evaluated as soon as they are needed. For example:
Code Block |
---|
add(1, (1 + 2) * 3 // 39 add(add(1, + 2), * 3) - 4 // 65 |
Reading and Writing Tag Values
To get a tag’s current value, use tag.read
. This takes in function has one argument, which is the name of the tag, and returns the tag’s value.
Code Block |
---|
var value = tag.read(“my_tag“); |
To write a tag value, use tag.write
. This takes in two arguments: the name of the tag and the value to write. There is no return value.The following script increments a tag’s value by 1:
Code Block |
---|
var value = tag.readwrite(“my_tag“tag”, 1); value =// writes the value + 1; tag.write(“my_tag”, value); |
...
1 to the tag "my_tag" |
Tags within tag groups can be accessed using the /
character. For example:
Code Block |
---|
tag.write("outer_group/inner_group/my_tag", tag.read("my_tag" + 1);)) |
You must call tag.write
each time you want to update a tag value, and you must call tag.read
each time you want to use the latest tag value. When used in a loop, it is recommended to use tag.read
at the beginning of the loop and to use tag.write
at the end of the loop.
Semicolons
JavaScript uses semicolons to indicate the end of expressions. Using semicolons at the end of lines is not required, but is recommended for better organization and clarity:
...
Code Block |
---|
some_function();some_other_function; |
Whitespace
JavaScript ignores whitespace characters like spaces, tabs, and line breaks.
...