Swarms of squares could be useful couldn't they? It was an idea from Les Patterson in the 3D Canvas forums to make lots of tiny reflecting particles to put in front of a light make a halo around it. Depending on the parameters you enter, the script presented in this chapter gives some weird results.
I'm showing it to you to introduce user defined variable types.
For our swarm assume we want to create a number of squares of a certain size, and distribute them randomly within a sphere of a given radius. (Tell you a secret. It really does a hemisphere, and I haven't debugged it. Where is the problem?)
You could declare three variable: NumParticlesSize, NumParticles, SwarmRadius .
This is ok if your program only deals with one swarm at one time. If you have more than one swarm, you can make a variable of type Swarm, then declare S1 and S2 as variables of that type. This is usually done in Visual Basic and Pascal like this:
Type SwarmClass
Public SwarmRadius 'The radius of the spherical space for the Swarm
Public NumParticles 'The number of objects (planes in this case) in the Swarm
Public SizeParticles 'The size of the planes
End Type
If you make variable S1 of type Swarm, you typically refer to it's subparts as S1.SwarmRadius, etc.
But in VBScript this type of declaration does not exist, and the Type keyword refers to something completely different. You can do the same thing using the Class keyword. Instead of creating a variable of a user defined type, you create an object of user defined class. The keyword Set creates the objects, and from then on the assignments are the same as for user defined types in Visual Basic. Practically it does the same thing, and results in increased flexibility if you want to convert you scripts to Visual Basic plugins. So it's done like this in VBScript:
Class SwarmClass
Public SwarmRadius 'The radius of the spherical space for the Swarm
Public NumParticles 'The number of objects (planes in this case) in the Swarm
Public SizeParticles 'The size of the planes
End Class
'Declare global variables
Public S 'as SwarmClass 'the swarm of objects
Sub Main (CanvasApp)
set S=New SwarmClass 'this creates a new object of type SwarmClass
S.SwarmRadius=10 'sets the radius
S.NumParticles=10000 'sets the number of particles
S.SizeParticles=.1 'sets the size of the particles
End Sub
The With keyword allows you some shorthand. I usually do the assignments in a subroutine, then write different subroutines for different objects. The calls to the ones you're not using can be commented out of the main code.
sub MakeSmallSwarm with S .SwarmRadius=10 .NumParticles=10 .SizeParticles=.1 end with End sub sub MakeBigSwarm with S .SwarmRadius=100 .NumParticles=10000 .SizeParticles=.1 end with End sub
Here's the code for the full script. It's a bit scruffy. If you want to use a predefined texture, you have to first create a paint strip object ( see Chapter 4) and select it before you run the script.