Scripting/Variables
De DigiWiki.
Manuel | Fonctions | Events | Types | Operators | Constants | Flow Control | Library | Exemples | Legend | Tutorials |
Une variable est un élément dans lequel une information (nombre, chaine de caractères...) est stockée.
Une variable a un nom, un type et une valeur. Le nom commence par une lettre et les conventions sont les mêmes qu'on C ou en Java. La casse est prise en compte: "X" est différent de "x"
le LSL est un langage fortement typé. Cela veut dire que les types des variables doivent être déclarés et que les variables ne peuvent contenir que des valeurs correspondant à leur type. Cependant, une variable liste peut contenir des valeurs de n'importe quel autre type.
Quelques exemples:
integer nombre = 2; // Un nombre entier float virgule = 1.2; // Un nombre décimal string mot = "Lee"; // Tout texte entre " " list mots = [ "Ceci", "est", "une", "liste" ]; list differente = [ "Peut contenir différents types de valeurs", 2, 1.2, <0.4, 0.8, 1.6> ]; vector vecteur = <1,6,2>; rotation tourner = <1,2,3,4>;
Portée des variables
Le nom de la variable est reconnu depuis l'endroit ou elle apparaît pour la premiere fois jusqu'à la fin de la partie dans laquelle elle se trouve, ou la fin du script pour les variables globales. Un nom de variable ne peut pas être défini deux fois dans la même partie, mais le nom peut être défini dans une partie externe et redéfini dans une partie interne. Comme dit précédemment, la sémantique est très proche de celle utilisée en C ou en Java. Ainsi, le code qui suit pourra etre compilé et exécuté :
integer i = 50; default { state_entry() { string i = "Coucou !"; // Cela sera accepté lors de la compilation, contrairement à Java ou C++. llOwnerSay(i); // Dira "Coucou !". Il n'y a plus moyen d'obtenir la variable globale 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 } }