Variable Scope and Extent
global
have a global scope. This means they continue to exist for the duration of the program's execution, and they are visible in modules that use the current module. Variables that are declared local
, however, can be declared and used only within the context of a limited variable scope. Local declarations cease to exist once that variable scope has ended.
The ScriptX language has several constructs that introduce a new variable scope different from that of the scope surrounding it, including function and method definitions and some loops. Compound expressions are the most commonly used construct that defines a new variable scope. See "Compound Expressions" on page 52 for more information about local variables and examples of how variables act within different variable scopes.
By default, ScriptX variables are global, including the variables that are used to name functions, classes, and some objects. However, you should explicitly declare each variable you use to be either local
or global
using the expressions described in the section "Declaring Variables" on page 37.
A Note on Modules
modules
. Modules in ScriptX are a packaging system that allows control over multiple global variable namespaces. Global variable names are only visible within the module where they are declared, and in modules that use that module. Modules allow for integration of large titles from components made by many different programmers without naming conflicts.
Declaring Variables
local
or global
construct.local varname, varname, varname . . .
The varname is the name of the variable to be declared. Multiple variable names can be specified in the same expression, separated by commas.
global varname, varname, varname . . .
Global variables can only be declared at the outermost scoping level. It is considered good programming practice, in ScriptX as in all languages, to declare a global variable before it is used. Many programmers declare global variables at the beginning of a program or module.
global density, mass, velocity
global
(regardless of the scope in which it is defined) and a warning is printed to your environment's debugging window or stream:mishegoss := @normal
-- ** Warning: Undeclared global mishegoss
@normal
global pickup
print pickup
-- ** While calling from the listener at:
0: PUSH-EXTERNAL-VARIABLE Scratch:pickup
-- ** Scratch:pickup does not have a variable value
(UninitializedVariable)
:=
), which is described in the section "Assignment" on page 38. A colon can also be used to assign an initial value to a variable.
global pickup := 52
global defenseBudget:3.18e11, moonsOfMars:2
Constants
local
or global
reserved word with the reserved word constant
. Constants have values that cannot be changed by assignment, and as such must have values assigned to them at time of declaration:global constant halfTon := 1000
global constant lightSpeed := 2.998e8 -- speed of light, meters/sec
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.