Variables can be used in CoyoteScript to dynamically keep track of values and re-use them by specifying the name of the variable. Variables can hold a variety of different object types such as text, numbers, lists, and maps. There are 2 types of variables in CoyoteScript, script variables and global variables.
Local variables are prefixed with a “$” symbol. These variables only exist within the scope of a single script execution. This means that the value you assign to the variable only exists in the very script that it was created in. Trying to get the value assigned to a local variable in any script is not possible from another script. Even across different executions of the same script, the value of a variable will not persist and has to be reassigned every time. Local variables are excellent for creating temporary variables that are only relevant to the script itself without having to worry about conflicting names.
A local variable can be created like this:
$myVariable = hello
In this example, we have created a local variable with the name “myVariable”, and stored a value to it in the form of a bit of text saying “hello”.
Local variables can be referenced like this:
Expanding on the example, in this print command”$myVariable” would return its value (being “hello”) when the command is being executed, which would result in the debugbar printing a line of text saying “hello”.
Global variables are prefixed with a “%” symbol. These variables work just like local variables, except for they can have their value changed and read from any script at any time. They are also saved permanently until reset by a script. This means that one script can assign a value to a global variable, and another script can read that same value from that variable at any time. Global variables are perfect for sharing information between multiple scripts, or saving variables indefinitely. These should be used more thoughtfully and with great care to avoid conflicting names. Global variables are saved in a text based file found in your system’s documents folder at CoyoteMIDI/userdata/script variables.yml.
A global variable can be created like this:
%mySpecificVariableName = hello
In this example, we have created a global variable with the name “mySpecificVariableName “, and stored a value to it in the form of a bit of text saying “hello”. Note that the name has deliberately been made more complex to reduce the chance of accidentally using the same name for a variable used for a different purpose later on.
Global variables can be referenced like this:
Variables can store various types of objects. Depending on the object type, the variables might behave differently from another and allow you to do additional kinds of operations. The type of object that is stored in the variable simply depends on what you assign to the variable. These object types include:
Text based variables simply hold a plain text string. These variables usually offer no valid operations. If a mathematical operation is applied to a text based variable, the text will try to be parsed as a number first.
$text = The quick brown fox jumps over the lazy dog print $text > The quick brown fox jumps over the lazy dog
Number based variables hold numeric values. These can be used for mathematical operations. For a complete overview of mathematical operations check out math operations.
$x = 5 $y = $x + 2 print "$x plus 2 is $y" > 5 plus 2 is 7
List variables are variables that hold a collection of items. These variables will offer collection based operations to add or remove one or multiple values from the list, as well as to access individual values at given positions. For a complete overview of collection operations check out collection operations.
$list = a, b, c $list + e $list - b print $list > (a, c, e) print ($list 2) > c
Map based variables hold a collection of items where each item consists out of a key item and a value item. These variables offer collection based operations as well as access to values of items with a given key. For a complete overview of collection operations check out collection operations.
$map = 1: a, 2: b, 3: c $map + 4: e $map - 2: b print ($map) > (1 : a, 3 : c, 4 : e) print ($map 3) > c
Assignment and non-assignment operations
Variables can have their value modified directly in a single operation without having to re-assign a value to the variable. This is called an Assignment operation. Whenever a variable is at the start of a line, any operation done to it will be an assignment operation and change its value. This applies to both mathematical and collection operations.
$x = 3 $x + 5 print $x > 8
If the variable is not at the start of a line, like for example when used inside the print command, you can perform operations to it without automatically re-assigning a value to it.
$x = 4 print ($x + 2) > 6 print $x > 4