Difference between revisions of "LSL Variables"

From Second Life Wiki
Jump to navigation Jump to search
Line 1: Line 1:
A '''variable''' is a place to store information, like a number or strings.
A '''variable''' is a place to store information, like a number or a string.
 
A variable has a name, a type, and a value.  The name starts with a letter, and the name convention is similar to C or Java.  Case matters.  ''X'' is not the same as ''x''.


LSL is a strongly and dynamically typed language. This means that variables must be declared by type and that variables may only hold values of a corresponding type.
LSL is a strongly and dynamically typed language. This means that variables must be declared by type and that variables may only hold values of a corresponding type.


==Variable Conventions==
Some examples:
=== Common Variable Uses===
* '''i''' is usually used as an index variable in a {{LSLG|for}} loop.  '''j''' is usually used for a nested for loop (when i is already being used), and then '''k'''.
* '''x''', '''y''', and '''z''' usually refer to the first three dimensions of some vector (when applicable).
 
===Magic Numbers===
The phrase "magic number" refers to a number written literally as a constant in code, but the meaning is not intuitive from the context of the code.  It may also be such a number used repeatedly throughout the code.
 
For example:
 
if ( vctSpd.z < 10 ) {
}
 
The 10 is a "magic number". In a scenario where this test is repeated 20 times in the script, any change to the number would require 20 updates by hand, which increases the chance for errors.
 
To avoid magic numbers, declare a variable of the appropriate type, set the value of the variable in the declaration and use the variable in your code.
 
integer intSlowSpeed = 10;


if ( vctSpd.z < intSlowSpeed) {
integer l;
}
float  x = 1.2;
string  n = "Lee";


Updating the value of intSlowSpeed here requires only one change and reduces the opportunity for errorThe name of the variable also expresses its meaning.
===scope===
The variable name is in scope from the point it first appears to the end of the scope it is in, or the end of the script for global variablesA name may not be defined twice in the same scope, but a name may be redefined in an inner scope, and it hides the same name at outer scope.  Again, the semantics are very similar to C and Java.

Revision as of 13:43, 19 February 2007

A variable is a place to store information, like a number or a string.

A variable has a name, a type, and a value. The name starts with a letter, and the name convention is similar to C or Java. Case matters. X is not the same as x.

LSL is a strongly and dynamically typed language. This means that variables must be declared by type and that variables may only hold values of a corresponding type.

Some examples:

integer l; float x = 1.2; string n = "Lee";

scope

The variable name is in scope from the point it first appears to the end of the scope it is in, or the end of the script for global variables. A name may not be defined twice in the same scope, but a name may be redefined in an inner scope, and it hides the same name at outer scope. Again, the semantics are very similar to C and Java.