Syntax Basics
As mentioned in the previous section, a script is a set of instructions for a computer to do stuff, therefore we need to write those instructions in a language that the computer understands. Just as languages like English RhinoScript has its own syntax rules. There are, however, some differences between the natural languages people use to speak to each other and the formal languages people use to tell computers what to do. Unlike a human a computer needs instructions that follow strictly the syntax rules of the language. That's their weakness, or their strength, depending on how you look at it!
An example of a well-formed RhinoScript statement is
print 'hello world'
>> hello world
Variables
Variables are names that refer to values. We can think of them as boxes or buckets that store data. We create variables in order to allocate space for a value (a number, a name, a reference to another variable) inside the computer’s memory. Variables are vital for a script to for accessing values that are not available at the moment the script is written, such as user input values, or results of internal calculations of the program. A script can have, and usually has, many variables.
Notice that the name of the variable and its value are two different things!
Naming and creating variables
Creating a variable, or declaring it, just means using RhinoScript to tell the computer to allocate some space in memory that can be accessed throughout the execution of the script. In order to create variables in RhinoScript we use the word 'Dim' followed by the name of the variable.
For example
Dim totalSum
Dim 001_my_variable
Dim average
Dim result
Dim dim ' error!!
Dim totalSum ' This creates the variable 'totalSum'
- Variable names cannot start with a digit (e.g. "10_value" is an invalid name)
- Cannot contain spaces (e.g. "total sum" is an invalid name)
- Must be different from Rhino's keywords (see a list of some of Visual Basic's reserved words (VB is the programming language RhinoScript is based on)
If any of these rules is broken the program will stop and show an error.
To assign a value to a variable we use the '=' (equals) sign:
totalSum = 10 ' This assigns the value 10 to totalSum
print totalSum ' This prints '10' to Rhino's command line
myName = "Bob"
print myName
>> Bob
myName = "Joe" ' This assigns "Joe" to the variable, overwriting "Bob"
print myName
>> Joe
Dim numberToAdd1, numberToAdd2, totalSum
numberToAdd1 = 51
numberToAdd2 = 49
totalSum = numberToAdd1 + numberToAdd2 ' assigns '100' to totalSum
Dim myVariable : myVariable = 30
Dim myName : myName = "Daniel"
print myVariable & ", " & myName
>> 30, Daniel
Data Types
Variables can store different kinds of values.
Strings are used for text:
Dim myString = "Hello World"
Not surprisingly, the data-type 'Integer' stores integer numbersDim myInteger = 100
Dim myInt = 200129
Dim myDouble = 3.1416
Boolean variables can only be true or falseDim myBoolean = false
Dim myOtherBoolean = Not myBoolean
print myOtherBoolean
>> true
Dim a : a = "hello"
Dim o : o = Rhino.GetObject("Select a point", 0)
If isPoint(o) Then
print "yes"
Else
print "no"
End If
Dim a : a = "Hello"
print isString(a)
Null is a type that is returned when nothing is assigned to the variable
Dim someValue
print someValue
Comments
Comments are lines of the script written in plain English for descriptive purposes. Commenting a script is a good way of making sure that it makes sense to other people or even to the code's author (it's surprisingly hard to understand a script written in the past if it's not thoroughly commented). In order for the compiler to ignore these lines we must start the line with an apostrophe sign " ' ". This tells the compiler that that particular line is not to be executed (just some "human" stuff).
' this is a comment, and will be ignored by the compiler
' because it starts with the apostrophe ( ' ) character.
References
Sections of this entry borrow -shamelessly at times- from
Rutten's Rhino 101
www.ksteinfe.net