topics.02_scripting basics


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

This just prints 'hello world' in Rhino's command line (from now on, whenever you see ">>" it refers to Rhinceros' output in its Command line". If you don't remember how to run a script in Rhino please refer to topics: editing and running scripts

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'

There are different styles for naming variables. Some people like using underscores to separate different words (e.g. my_variable), others use hyphens, and many people use a style called the 'camelcase', that capitalizes the first letter of each word in a variable name (e.g. myVariable). This is mostly personal taste, just stick to a notation you are comfortable with and be consistent. It will help you read your programs faster. The key thing is to use descriptive names that remind you of the value you are storing in the variable (e.g. avoid names that don't say much like "var2" or "a1"). Also, when creating variables in RhinoScript it is important to follow certain important rules.

- 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

The value of a variable can be defined in terms of a computation between other values or variables. The following script declares and assigns values to variables and defines one of them in terms of the other two.

Dim numberToAdd1, numberToAdd2, totalSum
numberToAdd1 = 51
numberToAdd2 = 49
totalSum = numberToAdd1 + numberToAdd2  ' assigns '100' to totalSum

To save space variables can be declared and their value assigned on the same line

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 numbers

Dim myInteger = 100
Dim myInt = 200129

Doubles are numbers with decimal values

Dim myDouble = 3.1416

Boolean variables can only be true or false

Dim myBoolean = false
Dim myOtherBoolean = Not myBoolean
print myOtherBoolean
>> true

Some Rhino functions return boolean values, look for instance at the following code:

Dim a : a = "hello"
Dim o : o = Rhino.GetObject("Select a point", 0)

    If isPoint(o) Then

        print "yes"
    Else
        print "no"
    End If

Boolean variables are particularly useful when evaluating a certain condition. We will see more of this later.

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.

Throughout these pages the examples of the code will often be commented to point out important aspects of the code in question.

References


Sections of this entry borrow -shamelessly at times- from
Rutten's Rhino 101
www.ksteinfe.net



Categories

Configure Widgets