Scripting/Variables/en

De DigiWiki.

(Différences entre les versions)
(Page créée avec « {{Languages|Variables}} 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 lett… »)
(See Also)
 
(8 versions intermédiaires masquées)
Ligne 1 : Ligne 1 :
-
{{Languages|Variables}}
+
{{Languages|Scripting/Variables}}
 +
{{ScriptingNav/en}}
A '''variable''' is a place to store information, like a number or a string.
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 or underscore ("_"), and the name convention is similar to C or Java. Case matters''X'' is not the same as ''x''.
+
A variable has a name, a type, and a value.  The name starts with a letter or underscore ("_"), 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.
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:
Some examples:
-
<lsl>integer  count  = 2;            //A whole number
+
<lsl>
 +
integer  count  = 2;            //A whole number
float    measure = 1.2;          //A number with decimal places
float    measure = 1.2;          //A number with decimal places
string  chars  = "Lee";        //Any text within " "
string  chars  = "Lee";        //Any text within " "
Ligne 13 : Ligne 15 :
list    entries = ["A list may contain many types of values such as", 2, 1.2, <0.4, 0.8, 1.6>];
list    entries = ["A list may contain many types of values such as", 2, 1.2, <0.4, 0.8, 1.6>];
vector  vec_2  = <1,6,2>;      //Generally used for position as xyz, but can be used to store 3 numbers to be parsed out later
vector  vec_2  = <1,6,2>;      //Generally used for position as xyz, but can be used to store 3 numbers to be parsed out later
-
rotation _rot    = <1,2,3,4>;    //Can also be used to store 4 numbers to be parsed out later</lsl>
+
rotation _rot    = <1,2,3,4>;    //Can also be used to store 4 numbers to be parsed out later
-
 
+
</lsl>
== Scope of variables ==
== Scope of variables ==
A variable can be limited to only certain parts of the script, depending upon where it is placed.  This placement, and the areas where it is enabled is called the "scope."  Variables that apply to the entire script are called global variables: they are defined at the top of the script above the state declarations.  Variables that are within functions or within nested areas are considered local variables.
A variable can be limited to only certain parts of the script, depending upon where it is placed.  This placement, and the areas where it is enabled is called the "scope."  Variables that apply to the entire script are called global variables: they are defined at the top of the script above the state declarations.  Variables that are within functions or within nested areas are considered local 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.
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.
-
<lsl>integer i = 50;
+
<lsl>
 +
integer i = 50;
default  
default  
Ligne 28 : Ligne 31 :
         llOwnerSay(i); //Will say "Hello there!". There is no way to get the global variable i.
         llOwnerSay(i); //Will say "Hello there!". There is no way to get the global variable i.
     }
     }
-
}</lsl>
+
}
-
 
+
</lsl>
I found this confusing at first, this may make it a little clearer:
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
The same rules apply to any variable type, a local variable name will overide any global variable previously defined
-
<lsl>string  j = "Global Hi";
+
<lsl>
 +
string  j = "Global Hi";
integer i = 50;
integer i = 50;
Ligne 43 : Ligne 47 :
         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
     }
     }
-
}</lsl>
+
}
-
 
+
</lsl>
-
 
+
Converting between variable types is know as casting. for example in the script above, the global variable i has a value of 50. This can be cast to a string to send the value as text to the chat window. This can also be done with local variables.
Converting between variable types is know as casting. for example in the script above, the global variable i has a value of 50. This can be cast to a string to send the value as text to the chat window. This can also be done with local variables.
This is commonly used for debugging a script.
This is commonly used for debugging a script.
-
 
+
<lsl>
-
 
+
integer i = 50;
-
<lsl>integer i = 50;
+
-
 
+
default  
default  
{
{
Ligne 60 : Ligne 61 :
         llOwnerSay((string) j); //Will say "60". the local integer has been changed ( cast ) to a string.
         llOwnerSay((string) j); //Will say "60". the local integer has been changed ( cast ) to a string.
     }
     }
-
}</lsl>
+
}
-
 
+
</lsl>
-
 
+
A global variable can be defined with or without a value and the value can be changed later in the script.
A global variable can be defined with or without a value and the value can be changed later in the script.
Note that in setting the value of j, only the name was used, the type ( integer) is already set globally,
Note that in setting the value of j, only the name was used, the type ( integer) is already set globally,
Ligne 78 : Ligne 78 :
         integer k = 70; //Name, type and value set within a local scope
         integer k = 70; //Name, type and value set within a local scope
     }
     }
-
}</lsl>
+
}
 +
</lsl>
== See Also ==
== See Also ==
-
* [[string]]
+
<ul style="{{NewStyle|column-width|20.5em|moz=*|webkit=*}}">
-
* [[key]]
+
*[[Scripting/Types/string/en|string]]
-
* [[integer]]
+
*[[Scripting/Types/key/en|key]]
-
* [[float]]
+
*[[Scripting/Types/integer/en|integer]]
-
* [[list]]
+
*[[Scripting/Types/float/en|float]]
-
* [[vector]]
+
*[[Scripting/Types/list/en|list]]
-
* [[rotation]]
+
*[[Scripting/Types/vector/en|vector]]
 +
*[[Scripting/Types/rotation/en|rotation]]
 +
</ul>

Version actuelle en date du 2 octobre 2014 à 05:15

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 or underscore ("_"), 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;            //A whole number
float    measure = 1.2;          //A number with decimal places
string   chars   = "Lee";        //Any text within " "
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_2   = <1,6,2>;      //Generally used for position as xyz, but can be used to store 3 numbers to be parsed out later
rotation _rot    = <1,2,3,4>;    //Can also be used to store 4 numbers to be parsed out later

Scope of variables

A variable can be limited to only certain parts of the script, depending upon where it is placed. This placement, and the areas where it is enabled is called the "scope." Variables that apply to the entire script are called global variables: they are defined at the top of the script above the state declarations. Variables that are within functions or within nested areas are considered local 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
    }
}

Converting between variable types is know as casting. for example in the script above, the global variable i has a value of 50. This can be cast to a string to send the value as text to the chat window. This can also be done with local variables. This is commonly used for debugging a script.

integer i = 50;
default 
{
    state_entry() 
    {
        llOwnerSay((string) i); //Will say "50". the global integer has been changed ( cast ) to a string.
        integer j = 60;
        llOwnerSay((string) j); //Will say "60". the local integer has been changed ( cast ) to a string.
    }
}

A global variable can be defined with or without a value and the value can be changed later in the script. Note that in setting the value of j, only the name was used, the type ( integer) is already set globally, wheras the variable k, is set locally and has its type and value set there.

integer i = 50;
integer j;
 
default 
{
    state_entry() 
    {
        llOwnerSay((string) i); //Will say "50". the integer was given a value at declaration.
        j = 60;  //Value added to a global variable
        llOwnerSay((string) j); //Will say "60". the value was added later.
        integer k = 70; //Name, type and value set within a local scope
    }
}

See Also

Outils personnels
  • Cette page a été consultée 1 088 fois.
donate
Google Ads