LScript User Guide
Next Section Previous Section Table of Contents Index Errata

LScript Data Types

LScript supports several different types of data. These types are discussed in the following sections.

numeric

Numeric values in LScript can be negative or positive, and can include an optional decimal point to designate floating-point values. LScript also supports scientific notation.

     main
     {
         var integer = 15;
         var real = 0.3;
         var negative = -35.356;
         var scientific = 7.5E+13;
         ...
     }

string

String literals are represented by enclosing characters in double quote marks at the beginning and end of the character sequence:

     main
     {
         var astring = "Here is a test string";
         ...
     }
As with C, LScript allows--and sometimes requires--you to "escape" certain characters within your strings. Escaping a character causes LScript to treat that character as a literal, as something that is part of the string being defined and without special meaning. To escape a character, use the back-slash character (\) immediately before the character you wish LScript to process.

For example, double quote marks typically indicate the beginning and ending of a defined string literal. What if you needed to include one or more double quotes in your string for display? The following LScript code line would generate nothing but errors:

     quoteString = ""I don't like this", he said";
LScript would interpret the first two double quote marks as beginning and ending of the string literal you are assigning to variable quoteString, and then continue processing your LScript starting with the first character in your intended string. This is very bad.

You need to instruct LScript to ignore the quote marks within the string as special characters, and this is accomplished by escaping them:

     quoteString = "\"I don't like this\", he said";

array

LScript supports arrays of up to ten (10) dimensions. Array elements can hold any of the supported LScript data types (except array; you cannot nest arrays). Each array can contain any mixture of these data types.

Arrays can be created using two different formats. The first format simply declares the array, indicating the number of elements the array contains:

     main
     {
         var check[15];
         ...
     }
Declaring an array in this fashion causes each element within the new array to be marked internally as "uninitialized." You cannot utilize an element in such a state until you have assigned a value to it. LScript will detect any attempt to access uninitialized array elements--or variables, for that matter--and generate an error message.

Using the second initialization format, you can declare and initialize an array from entries in a disk file. This disk file should contain one array element per line—either numeric or character string—and should be in ASCII text format. The LScript engine will attempt to determine the data type as it reads in each entry.

Initialization of an array from a disk file takes place when you use a string literal value in place of the numeric parameter that would indicate the size of the array:

     main
     {
         var points["\\home\\rbh\\points.txt"];
         ...
     }
Once arrays are declared, individual elements can be accessed by referencing the array element directly. Array elements are accessed in LScript starting at offset one (1), as in Pascal, instead of zero (0), as in C or C++. Each LScript can use as many arrays as it needs, limited only by your computer's available memory.

Any expression that will evaluate to a integer value can be used as an array index. For example,

     randElement = points[random(1,size(points))];
Entire LScript arrays can also be initialized in a single step from a single value by assigning that value to the array using an empty index:

     main
     {
         var check[15];

         check[] = -1;   // populate all 15 elements with '-1'
         ...
     }

v2.0 UPDATE

Version 2.0 of the BML interpreter no longer requires single-dimension (or "linear") arrays to be explicitly declared. BML will now allocate elements in your array "on-demand", meaning that if you access an element in your array that does not yet exist, BML will automatically expand your existing array to encompass the new size.

Be aware that, because arrays are now created "on-the-fly", situations where you would have to use an explicitly-declared array to contain function return values may no longer behave as expected. To illustrate, consider the following pre-v2.0 code example:

     main
     {
         var firstItem;   // line 3

         firstItem = parse(" ","Hello there, everybody!");  // line 5

         if(firstItem == "Hello")        // line 7
         ...
     }
The call to parse() returns three items; "Hello", "there," and "everybody!". In the above example, only the first item ("Hello") would be assigned to the variable 'firstItem' in versions of the BML Engine prior to version 2.0. The reason for this is because 'firstItem' was not explicitly declared as an array when it was created, therefore it is only capable of holding one value. The other values returned from parse() ("there," and "everybody!") would simply have been discarded.

However, in BML v2.0, arrays are created automatically by the interpreter as they are needed, so this same code in v2.0 would cause an array of three (3) elements to be automatically generated, and assigned to 'firstItem'. As you can see, line #7 above would be incorrect in v2.0, because 'firstItem' is now an array of three elements!

To work with this new behavior, you could either change line #7:

         if(firstItem[1] == "Hello")        // line 7
Or, a better solution would be to wrap 'firstItem' in an associative assignment "blanket" so that line #5 behaves as it did in previous versions of BML:

         (firstItem) = parse(" ","Hello there, everybody!");
PLEASE NOTE that multi-dimensional arrays must still be explicitly declared using the var keyword.

vector

The LScript vector type is constructed of three numeric values, each separated by a comma, inclosed in angle braces ('<>'). This data type can be passed to many of the built-in LScript and Modeler functions, as well as to user-defined procedures as procedure arguments. Some LScript commands also return vector data types.

     main
     {
         var apoint = <4.5,10,.03>;
         ...
     }
Once declared, individual elements of the vector data type can be accessed using the keywords x, y, and z. For example:

     main
     {
         var apoint = <4.5,10,.03>;

         ++apoint.x;
         --apoint.y;

         if(apoint.x > 4)
         ...
     }

point/polygon id

These two data types are not directly usable by an LScript. They are included in LScript to support the return values of such Modeler-specific functions as addpoint() and addpolygon(). Variables and arrays alike can each be of this data type.

nil

nil can be both a condition and a data type in LScript. Variables and array elements can be assigned--and tested for having--this value. This value is also returned by certain LScript built-in commands to indicate failure of some kind. For instance, the object creation command file() will return nil if it cannot create a File Object.

A good use of this value is in initializing arrays. Initialization with this value avoids a potential run-time error that can be generated when accessing uninitialized array elements.

     main
     {
         var check[15];

         check[] = nil;
         ...
     }
You will probably want to initialize an array to nil when dealing with LScript commands that have variable return values, such as parse().

process id

As with point id and polygon id, this data type is not directly usable by an LScript. It is included in the LScript data set to support the process control commands spawn(), wait() and terminate().


Next Section Previous Section Table of Contents Index Errata
© 1996 Virtual Visions, Inc.
© 1997 NewTek, Inc.