Local and Global Variables

In MAXScript, there are two classes of variables that you can create: "local" and "global" variables. When a variable is global, it means that the variable can be used anywhere in the program. The only place you cannot use a global variable is before you have defined it.

For example, the following lines will produce an error:

sphere radius:rad

global rad = 10

The variable "rad" does not exist until it has been defined.

The following is an acceptable form of the previous lines:

global rad = 10

sphere radius:rad

Now that "rad" has been defined, MAXScript was able to use it in the definition of a sphere. In fact, since "rad" is a global variable, it can be used throughout the rest of the script.

There are several global variables that can be used without defining them first. They are the system global variables, which have already been created and assigned to a default value. Most of these variables can be reassigned to different values. For more information on the system global variables, see the 3ds Max System Globals topic in the MAXScript reference.

Unlike global variables, local variables can be used only within the block in which they are defined. A block is a grouping of MAXScript code, such as the statements within loops and conditional statements.

Here is an example of a for loop:

for i = 1 to 3 do


local rad = 10

s = sphere()

s.pos.x = i * 10

s.radius = rad


Local variables can only exist within a block. For this reason, they must be created in a block. If you try to define a local variable from the top level of MAXScript, you will receive an error message.

In the previous example, you defined a local variable named "rad". This variable is active only within the parentheses inside the for loop; if you attempt to use the variable "rad" elsewhere, you will get an error.

Although rad is initialized to a specific value, this is not necessary. When you define variables using a "local" or "global" identifier, you can do so without assigning an initial value. The name of the variable will be recognized. However, the software will not know the type of the variable (name, number, color, etc.). MAXScript automatically assigns the variable a special value of "undefined". The first time you assign something to the variable, it will become defined by the data type that you have assigned it to. In practice, it is always best to define variables with an initial value.

It is important to note that rad is not the only local variable in the previous example. The variable "s" is also a local variable, and cannot be accessed outside of the parentheses. It is not necessary to declare every variable with a "global" or "local" statement. You learned previously that you can implicitly create a variable by simply assigning a value to it. If you do not make the "global" or "local" distinction upon your variable, MAXScript will create the appropriate association, depending on where the variable is defined in the script. For example if you create a variable inside a block, MAXScript automatically treats it as a local variable. On the other hand, any variable created at the top level of MAXScript (outside of all blocks) is always a global variable. As mentioned previously, MAXScript does not allow for the creation of local variables outside of a block.

These concepts are presented in the following


for i = 1 to 5 do


global rad = 10

cyl_height = 25

c = cylinder()

c.radius = rad

c.height = cyl_height

c.pos.x = i * 25



s = sphere radius:rad

In this example, four variables are created: rad, cyl_height, c, and s. Because they were implicitly created within a block, c and cyl_height are both local variables. The other two variables are both global; s, because it was created outside of a block, and rad, because it was declared as a global variable. For this reason, you can use rad again to define the sphere, s.


Next Topic

Defining Custom Functions