Page 1230 - Kitab3DsMax
P. 1230
Part XII: MAXScript and Plug-Ins
script. If you weren’t using functions, you would have to go through your script and find every case where
you averaged numbers and then fix the problem. (What a headache!)
Now take another look at the function definition. The first line
Function average numbers =
tells Max that you’re creating a new function called average. It also tells Max that to use this function, you
have to pass in one piece of data, and that inside the function you refer to that data using a variable called
numbers. It doesn’t matter what the name of the actual variable was when the function was called; inside
the function, you can simply refer to it as numbers.
Creating functions that use multiple pieces of data is also easy. For example,
Function multEm a b c = (a * b * c)
creates a function that multiplies three numbers together. To use this function to multiply three numbers
and store the result in a variable called B, you would simply enter
B = multEm 2 3 4
The next two lines
local Total = 0
local Count = 0
create two variables and set them both to 0. The local keyword tells Max that the variable belongs to this
function. No part of the script outside of the function can see this variable, and if there is a variable outside
the function with the same name, changing the variable inside this function won’t affect that variable out-
side the function. That way, you never have to worry about what other variables are in use when someone
calls average; even if variables are in use that are named Total or Count, they won’t be affected.
The last line
total / (count as float)
uses the Total and Count values to compute the average. How does that value get sent back to whoever
called the function? Max evaluates all the MAXScript inside the function and returns the result. Because the
last line is the last thing to be evaluated, Max uses the result of that calculation as the result of the entire
function.
Tutorial: Creating a school of fish
Let’s look at an example that puts into practice some of the things you’ve learned in this chapter. In this
multipart tutorial, you use MAXScript to create a small school of fish that follows the dummy object around
a path.
Part 1: Making the fish follow a path
In this part of the tutorial, you use MAXScript to move one of the fish along a path in the scene. To do this,
follow these steps:
1. Open the Fish scene.max file from the Chap 49 directory on the DVD.
This scene consists of two fish and a dummy object that follows a path. What you need to do is
use MAXScript to create a small school of fish that follows the dummy object around the path.
2. Press F11 to open the MAXScript Listener window. In the window, choose File ➪ New Script to
open the MAXScript Editor window, and type the following script:
1182