Difference between revisions of "LSL Variables"

From Second Life Wiki
Jump to: navigation, search
m (tweak the examples to discourage the perhaps deprecated style of single-letter variable names that are hard to grep for)
(Scope of variables: Expanded)
Line 18: Line 18:
  
 
== Scope of variables ==
 
== Scope of variables ==
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.
+
LSL has three scopes: Global, [[User-defined functions|Function]] and [[:Category:LSL Events|Event]]. Variables defined in the Global scope are accessible anywhere in the program, and retain their value through all events, function calls and [[state]] changes. Variables defined in a function or event are available only within that function or event. If you define a variable twice in the same scope, it will cause an [[LSL Errors|error]], but if you define a variable in a function or event scope, with the same name as a variable in the global scope, it will hide the global variable so long as control remains in that function or event. Again, the semantics are very similar to C and Java.
  
 
== See Also ==
 
== See Also ==

Revision as of 16:14, 9 October 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 statically typed language. This means that variables must be declared by type and that variables may only hold values of a corresponding type. However, a list variable may hold zero or more values of any other type.

Some examples:

integer count = 2;
float measure = 1.2;
string chars = "Lee";
list words = ["This", "Is", "A", "List"];
list entries = ["A list may contain many types of values such as", 2, 1.2, <0.4, 0.8, 1.6>];
vector vec = <1,6,2>;

Scope of variables

LSL has three scopes: Global, Function and Event. Variables defined in the Global scope are accessible anywhere in the program, and retain their value through all events, function calls and state changes. Variables defined in a function or event are available only within that function or event. If you define a variable twice in the same scope, it will cause an error, but if you define a variable in a function or event scope, with the same name as a variable in the global scope, it will hide the global variable so long as control remains in that function or event. Again, the semantics are very similar to C and Java.

See Also