Skip to main content

Variables

  • Assigning creates a variable. total = 0; There's no declaration keyword and no type annotation.
  • A variable must be assigned somewhere above its first use in the source text, or the script won't compile (undefined variable). This is checked by position in the text, not by what actually runs.
  • So a variable assigned only inside an if branch compiles when it's used after the if. If that branch didn't run, the variable holds Integer 0.
  • There is one scope per script. A { } block doesn't create a new scope. A loop counter is still there after the loop with its final value, and assigning to a name inside a block changes the same variable everywhere.
  • Variables live for one run of the script. Every dispatch starts with none, and two actions running at the same time never see each other's variables. Use Storage to keep a value between runs.
  • A variable can have the same name as a builtin. x and x(…) are told apart by the parenthesis, though reusing a builtin's name isn't recommended.
if (MouseGetCursorX() > 500) {
side = "right";
}
UtilityPrint(StringFormat("side = {0}", side, "")); // compiles; prints "side = 0" when the branch didn't run

for (i = 0; i < 3; i = i + 1) { }
UtilityPrint(StringFormat("i after loop = {0}", i, "")); // 3 -- no block scope