Scalars

The following examples can be executed directly within RealFlow’s batch script editor. The results will be written to the "Messages" window. To execute a script choose Script > Run from the batch window’s menu.

 

If you take a closer look at the “Hello World” example, you can see that the “text” object is highlighted in red. This object is also called a variable. A variable is a placeholder and this special case is called a scalar. A scalar can only store one value. Please take a look at this line again:

text = "Hello World"

 

The variable’s name is text and the value is Hello World. The quotation marks indicate that the value is a string – a series of characters. Everything inside the quotation marks is treated as a single value, though it may consist of more than one word. So here, the scalar condition that only one value can be assigned is fulfilled. If you want to assign a number as a value, then no quotation marks are needed, e.g.

number_value = 25

 

You can also define the number as a series of characters without its numerical meaning:

number_string = "25"

 

With such a variable it is not possible to perform mathematical calculations. An example:

number1 = "25"
number2 = 25
result  = number1 + number2
scene.message(result)

 

The output in RealFlow’s "Messages" window:

>WARNING: Script error: “cannot concatenate ‘str’ and ‘int’ objects” at  line number 3.

 

As a result you would normally expect “50”, but here you receive a syntax error, telling you that it is not possible to calculate a meaningful result from a word and a number. It is like trying to solve this equation:

Steve + 25 = ?

 

Such a calculation simply makes no sense, but in the example above that is exactly what the script tries to do. Therefore you will get an error. Anyway it is possible to connect numbers and characters, for example to print out a result. This is called concatenation. It is also very important to know that values have to be assigned and introduced before they can be used. A script like this one will not work, because the variable is used before a value has been assigned:

scene.message(text)
text = "Hello World"

 

It is also possible to assign a blank or zero value to a variable just to introduce it:

initial_mass = 0
name = ""

 

Another issue with variables is naming. A variable’s name should only be used once, unless you want to overwrite the previously assigned value, because only the latest assigned value will be used. This example changes the previous value and writes out 0.05:

friction = 0.01
friction = 0.05
scene.message(friction)

 

Naming does not only include double names, it is also about using significant names and descriptions. The first issue is the range of available or allowed characters. You should never use any special characters like %, &, §, ä, Å, É or similar glyphs. These characters are not correctly interpreted and only lead to trouble or errors. The situation gets even worse with conversion between different operating systems. The best method to avoid these problems is to restrict variable names to a limited set of characters:

 

[ A - Z ], [ a - z ], [ 0 - 9 ], the hyphen [ - ] and the underscore character [ _ ]

 

Another issue is that variables are case sensitive. In Python “friction” is not the same as “Friction”. A variable must keep its name over the entire script and must not be changed. Lastly you have to avoid using full stops (periods) with names under all circumstances. Dots are an element of the Python scripting language, so you cannot use them with variable names. You have already seen the dot with the message statement:

scene.message("Hello World")

 

Now take a look at this example:

a = "Mauritius"
b = "Pineapple"
c = 1200
d = 3.99

 

Formally that is absolutely correct, because there is no rule that a variable must have more than 1 character, but it is not really clear what all these values should express. It is much better to use meaningful names. Imagine a script with dozens of variables – with a notation like the one above it is not easy to find out their sense. Even if you are the author of the script, you will probably have trouble indentifiying the variables after a few weeks or even months. This example is much clearer:

origin      = "Mauritius"
fruit       = "Pineapple"
weight_in_g = 1200
price       = 3.99