Script Action
The Script Action action in a Reaction will run the provided script, which is a Reactor expression.
The script has access to all defined global variables. If the Script Action is in a rule-based Reaction (i.e. a Set or Reset Reaction), it also has access to all of the expressions/variables defined by the rule.
The script may also use the name of a global or ruled-based variable on the left side of an assignment operator; that is, scripts can set values on global or ruled-based variables. Consider the following script:
This script is a compound statement; it has three distinct statements that will be executed. Assuming gLastTime
is the name of an expression-less global variable already defined, the first statement will set the value of that global variable to the current time. Assuming count
is the name of an expression-less rule-based variable, its value will be set to 0. Assuming i
is neither a known global or rule-based variable, it will be defined as a temporary local variable in the script. Local variables are only available within the context of the script in the current step of the Reaction; they will not be available anywhere else in the Reaction.
Know Your Variables!
In an assignment where the identifier on the left side of the equal sign (=
) is unknown (i.e. the target variable isn't already defined in any scope), the default behavior of the expression language is to silently create a local (temporary) variable. If you misspell the name of a global or rule-based variable in an assignment, a local variable will therefore be created without warning. You may then observe that your intended global or ruled-based variable is not changing as expected. Remember: spelling and case are significant in expressions!
Naming Collisions
If you have a global variable and a rule-based variable with the same name (i.e. a name collision between two different levels of scope), and the rule's Set or Reset Reaction contains a script that refers to that name, the expression language will see the rule's variable first. There is no way to "see past" the rule's variable to access the global variable; you will have to change the name of one or the other. This is the reason many programmers use naming conventions like Hungarian Notation.
Updated: 25-Sep-2024