Difference between revisions of "LSL Variables"

From Second Life Wiki
Jump to navigation Jump to search
Line 40: Line 40:
     {
     {
           string i = "Hello there!"; //This WILL compile just fine, unlike in Java.
           string i = "Hello there!"; //This WILL compile just fine, unlike in Java.
           llOwnerSay(i); //Will say "Hello there!". this is the local variable, accessed pnly in this part of the script
           llOwnerSay(i); //Will say "Hello there!". this is the local variable, accessed only in this part of the script
           llOwnerSay(j); //Will say "Global Hi", this is the global variable that can be accessed anywhere in the script
           llOwnerSay(j); //Will say "Global Hi", this is the global variable that can be accessed anywhere in the script
     }
     }

Revision as of 07:54, 16 July 2008

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

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. That is to say, the following code will compile and run.

integer i = 50;

default {
     state_entry() {
          string i = "Hello there!"; //This WILL compile just fine, unlike in Java.
          llOwnerSay(i); //Will say "Hello there!". There is no way to get the global variable i.
     }
}

I found this confusing at first, this may make it a little clearer The same rules apply to any variable type, A local variable name will overide any global variable previously defined

string j = "Global Hi";
integer i = 50;
default {
     state_entry() 
     {
          string i = "Hello there!"; //This WILL compile just fine, unlike in Java.
          llOwnerSay(i); //Will say "Hello there!". this is the local variable, accessed only in this part of the script
          llOwnerSay(j); //Will say "Global Hi", this is the global variable that can be accessed anywhere in the script
     }
}

See Also