Variables
Variables allow a script to hold information. Unlike many scripting languages, Second Life variables are strongly typed. For example, to declare an integer, named i, use the following code.
integer i;
Once declared, the variable can be assigned using the equals operator. For example, to assign the variable i to the value of zero, the following code would be used.
i = 0;
Values can also be added to i as follows:
i = i + 1;
The above expression can also be expressed with the following shorthand:
i+=i;
Additionally, if the value of i is to be incremented by only one, the following shorthand can be used:
i++;
Second Life supports a number of variable types. These types are summarized in Table 2.1.
Table 2.1: Variable Types
| Type | Description |
|---|---|
| integer | a whole number ranging from -2,147,483,648 to 2,147,483,647 |
| float | a decimal number ranging from 1.175494351E-38 to 3.402823466E+38 |
| vector | three floats in the form < x , y , z >. Usually a position, color, or Euler rotation |
| rotation | a quaternion rotation, made up of 4 floats, < x , y , z , s > |
| key | a UUID (specialized string) used to identify something in SL, notably an agent, object, sound, texture, other inventory item, or dataserver request |
| string | a sequence of characters, limited only by the amount of free memory available to the script. |
| list | a heterogeneous list of the other data types. |
The two numeric types are integer and float. The type of integer should be used when no decimal places are required. If decimal places are required, then the numeric type, float, should be used. The other variable types, described in Table 2.1, will be described in later chapters.
A variable's scope defines from where the variable may be accessed. The Linden Scripting Language supports two levels of variable scope. Variable scope will be explained in the next section.
Variable Scope Types
Declaring a variable in a script does not necessarily make that variable accessible from anywhere in the script. The accessibility of a variable is referred to as variable scope. Where the variable is defined at determines the variable's scope. There are two types of variable scope in Second Life.
- Local Variables, and
- Script-Level Variables
The next two sections will describe each of these scope types.
Local Variables
Local variables are variables defined inside of a function. These variables can only be accessed from within the function they were declared in. Also, they can only be accessed by parts of the function further down than where they were declared. For example, consider the following script that displays the value of the i variable.
Listing 2.1: Display Variable
default
{
touch_start(integer total_number)
{
integer i;
i=0;
llSay(0, "The value of i is: " + (string)i);
}
}
The above code is inside if the function touch_start, which is a special type of function, called an event. Mark Biss2007-12-11T00:00:00Not sure of the meaning of this sentenceEvents and functions will be explained later in this chapter. Events and functions are blocks of code that are called at certain times. The touch_start event is called whenever an avatar touches the object containing the script.
First the integer i is declared. Next the variable is assigned a value of zero. This object then “says” what the value of the variable is. Notice the llSay function call. This is very important, as it is how many of the early programs in this book will communicate. Using the llSay function, an object can communicate in a similar manner to an avatar. The first parameter to llSay specifies the channel. Channel zero means that every avatar around will hear the communication. Sometimes objects will want to communicate with each other over private channels. To display the variable, a typecast of (string) is used to covert it to a string.
It is very important to use the variable only after it has been declared. The following script is invalid because the variable is displayed before it has been declared.
default
{
touch_start(integer total_number)
{
llSay(0, "The value of i is: " + (string)i);
integer i;
i=0;
}
}Additionally, other functions cannot access the local variable declared inside of other functions.
Script-Level Variables
Local variables are limited in their scope. They can only be accessed within a single function. Additionally, the value of a local variable is reset each time the function is called. This can be very limiting. Often a script will want to hold onto values indefinitely. Script-level variables hold their values indefinitely and they can be accessed by any function in the script. The following shows a script-level variable, named count, being used.
Listing 2.2: Script Level Variables
integer count;
default
{
state_entry()
{
count = 0;
}
touch_start(integer total_number)
{
count ++;
llSay(0, "Count: " + (string)count);
}
}
The above code implements a simple counter. The counter is reset to zero when the script first starts. When the default state is entered, the state_entry event function is called. In this function the count variable is reset to zero. Each time the object is touched the touch_start event function will be called. This will increment the count variable and the value of the count variable is then displayed.
Notice where the count variable is declared. Placing the count variable's declaration outside of any state causes the variable to be script-level.












