User:ANSI Soderstrom/LSL Beginners Class

From Second Life Wiki
Jump to: navigation, search

Hi and welcome to my LSL Beginners Class.

First, we need to know how the computers can understand the Humans. Don´t forget that computers are very dumb if we don´t tell them how to work and above all : what is what !

So, let us begin with How to use comments. Comments are very important to let know yourself what´s where exactly happened. If you read your code in a half year again, then it´s sometimes very hard to understand the idea behind your own code. Believe me :)

Our LSL-Examples are ever in grey boxes (like the grey box below this line), also you can copy/paste this code directly to your LSL-Editor.

<lsl> // the well-known comments are always starting with a double-slash. The LSL-Editor will display comments in a orange color. // A comment with a double-slash ends automatically at the next line break. i'm not a comment (because the missing double slash) and i will produce a error at compiling this code

/* To comment (or comment out) multiple lines you can use a slash with a following asterisk. If you are done with your multiple-line comments, just write a asterisk again, followed from a slash All sentences between this slash->astersisk combinations are comments !

  • /

// you can mix this comments in this way too :

/* COMMENTS WILL NEVER BE INTERPRATED BY A COMPILER, SO THEY TAKING NO MEMORY // so it´s a good choice to write so many comments as you can // you write the comments for YOURSELF (except you plan to sell your code) // If you want to sell your code, use comments !!! comment your code !!! ever !!! always !!!

  • /


Now we need to know how to tell our objects how to work with numbers, strings, vectors and so on. In LSL (and in all other languages too) there are different Types we can use.

The following example uses : [States] [Types] [Typecasting] [Chat-functions]

<lsl> default {

   // a integer is ever a entirety number. This number can have a value from −2,147,483,648 up to +2,147,483,647. This is enough for the most cases.
   // if you want to work with a number, think about a UNIQUE identifier and mark this identifier as "integer"

   // variable declaration 
   integer myFirstNumber;

   // variable declaration with initialisation (preferred)
   integer mySecondNumber = 1;

   // to work with decimals, declare your identifier as "float"
   float myThirdNumber = 0.2;

   // to hold a string or a whole sentence in a variable, declare your identifier as "string" and write your string within quotes.
   string myFirstString = "Hello";

   You can do some math only with integers and float´s, not with strings. The destination variable should be able to hold the right type of numbers,
   otherwise the number will be passed/rounded/cutted to the right type.
   You have to declare your variable only 1 time ! Otherwise you´ll get a error
   integer myFirstMath  = myFirstNumber + mySecondNumber;     // 0 + 1      = 1
   float   mySecondMath = mySecondNumber  + 0.2;              // 1 + 0.2    = 1.2
           mySecondMath = mySecondMath    + 0.9;              // 1.02 + 0.9 = 2.1
   float   myThirdMath  = mySecondMath    + 0.2;              // 2.1 + 0.2  = 2.3

   // you can connect strings with this method too
   string myFirstConnection = myFirstString + " Avatar";   // "Hello" + " Avatar" = "Hello Avatar";

   // to see your results you can use a Chat-function. This Chat-Functions expect a string
   // to show a number with a Chat-function, you have to do some typecasting
   llOwnerSay("my first Number: "   + (string)myFirstNumber);
   llOwnerSay("my second Number: "  + (string)mySecondNumber);
   llOwnerSay("my third Number: "   + (string)myThirdNumber);

   llSay     (0,"my first string: " + myFirstString);

   llWhisper (0,"my first Math: "   + (string)myFirstMath);
   llWhisper (0,"my second Math: "  + (string)mySecondMath);
   llWhisper (0,"my third Math: "   + (string)myThirdMath);
   llShout   (0,"my Connection: "   + (string)myFirstMath + myFirstConnection);

} </lsl>