LSL, Otherwise known as Linden Scripting Language, the primary language
used to create functionality in second life.
*********DATA TYPES***********
Some people may not realize that scripting is about finding, calling for, manipulating and returning
information. These are the most basic ideas behind why we need scripts to make things happen.
Making things move requires that we understand our in-world coordinate system and then tell
the object how to act within those coordinates. Scripting also requires that we understand the
"Type" of data, or information, we are using for our intended tasks.
Quick Segway:
An important resource to remember is the online scripting portal located at this web address:
http://wiki.secondlife.com/wiki/LSL_Portal
This page can be quickly accessed by going to the "Help" drop down menu above and selecting
the "Scripting Portal..." option from the menu, this will take you directly to the scripting reference.
The newer SL browsers do not have the scripting portal listed in the help menu drop down so you will
have to rely on good old cut and paste.
END of Segway!
Back to data types. Virtual scripting allows us to communicate with what would otherwise be
inanimate objects. Thats what makes scripting interesting. We can also make objects communicate
with each other and pass data between themselves automatically.
Objects can be told to scan the environment and act or react in a certain way if something or
someone is detected.
By understanding the "Types" of data we can manipulate and the information we can send and receive
from the environment, we can dream up, and imagine, some pretty cool effects and actions.
The environment uses a set number of data "Types":
Key - "a822ff2b-ff02-461d-b45d-dcd10a2de0c2"
Integer - Integers are whole numbers. Between -2,147,483,648 and +2,147,483,647
Float - 175.098267 --> If you want a decimal point in your number, then it is a float.
Rotation - <0.0, 0.0, 0.0, 1.0>
Quaternion -<0.0, 0.0, 0.0, 1.0> --> Quaternions and rotations are the same thing.
Vector - <105,12,55>
String - "I am a string."
List - ["List Item 1", "List Item 2", "List Item 3", "Etc...", "On and On", "And on..."]
These "Types" of data allow us to create virtual "Containers" that store numerical, alphanumerical
and text type data.
One of the more unique data types we find in virtual environments are called "Keys".
A key will look something like this: ec1f7d45-744c-427f-9987-a96d7b50d72b
A "Key" is also known as an object or avatar UUID.
A UUID is known as a "Universally Unique Identifier".
Every single thing in a virtual environment has a "Key" or "UUID" value. Everything!
*********FUNCTIONS***********
"Functions" are the programming codes we use to actually manipulate the Data "Types"
we are working with.
Functions are numerous, there are so many functions available that allow us to manipulate
our data and our objects that the list is much too long to go through, so we focus on small
numbers of relevant functions to get started and to develop an understanding of how
functions work in Virtual environments.
When looking at a function in the scripting portal, the header in the portal shows a function
listed as follows: Function: llSay( integer channel, string msg );
This listing essentially tells us what the Function is looking for in order for it to behave as
intended in our program.
Function: llSay( integer channel, string msg );
The first part "Function:" tells us that this page refers to a Function of course.
"llSay", the part before the bracket is the "Function" itself.
"( integer channel, string msg )", This tells us that this "Function" is looking for 2 values,
An "Integer" first which represents the channel to chat on,
and a "String", the actual message to chat out.
";", The semi colon is VERY important, 90% of all the lines in a program must have this
semi colon at the end. This tells the program that this line of code is finished and to
move onto the next line of code in the program.
Note that all "Functions" have 2 lower case Ls in front of them (ll), which stands for
Linden Language.
We are going to be working with functions regularly and quite often! A program will not do much
without "Functionality"

The Scripting portal is the place to search for specific functions when you are looking to
achieve a certain effect or do something specific with data.
*********PROGRAM STRUCTURE***********
The server is the computer which reads, interprets and reacts to the scripts based on what we
program into them.
The server reads a script from left to right, top to bottom, just like we read books.
LSL scripts have 2 main sections, the "Declaration" section and the "Body".
The "Declaration" section is where we declare the variables, or data types, that we wish
to use in the script. When we declare variables in this section these variables will be
available throughout the entire script.
The "Body" of the script is where we begin constructing the logic of our program with
"Functions".
The "Body" of our script is where we first encounter something called an "Event" block.
An "Event" block is a block of code which tells the server to do something when this or that
takes place. There are many "Events" that can occur to an object or script.
default
{
state_entry()
{
llSay(0, "Hello, Avatar!");
}
}
The simple program above demonstrates the basic "Body" structure of code.
The "default" code above tells the server that this is where the script logic
begins.
This is also the default script you will see after creating a new script.
"state_entry()" is the code which specifies the first event encountered in any
script, and that is that the script has just entered into existence in-world.
Weather it be because you just rezzed the object with the script in it, just created
and compiled the script or just reset the script.
Whenever you bring a script into the environment one of the first "Events" that occurs
to the script is the "state_entry()" event!
The "state_entry()", "Code Block" is not required by the script, but it is helpful to know that
using this Code block means that the server will do whatever is in this "Event Block"
before it does anything else in your script.
This is useful if you need to load data into variables before the rest of the script can
be properly executed.
Notice that each code block is followed by curly braces, "{}", an open and closed curly
brace defines the scope, or length, of each block and the code within it.
default
{ }
default <<---NOTICE that code blocks do not require Semi Colons ";".
{
state_entry()
{
}
}
Notice how the Curly braces nest within each other. The main script is all contained
within the "default" code block, think of the "default" code block as the main container
for the rest of your script.
Some of the more common event code blocks which occur inside the main "default"
container are;
touch_start(integer total_number)
{
}
on_rez(integer start_param)
{
}
listen(integer channel, string name, key id, string message)
{
}
attach(key id)
{
}
changed(integer change)
{
}
sensor (integer numberDetected)
{
}
collision_start(integer num)
{
}
timer()
{
}
link_message(integer source, integer num, string str, key id)
{
}
state_exit()
{
}
at_target(integer tnum, vector targetpos, vector ourpos)
{
}
not_at_target()
{
}
Event Code Blocks are how we coordinate and manage what is happening
in the environment. Each Event Code Block "Listens" in a way for a specific
thing to happen, and when it happens it tells the server to execute the code
within it.
The nice thing about Event Code Blocks is that they can also be used by us
to trigger events as well, using the "timer()" code block for example allows
us to create a function which repeats over a certain time interval.
All of the available "Events" are also listed on the Scripting portal for you
to review and read about. When you dissect someone elses script, understanding
the event blocks will help you to gain a better understanding of what the script
is for.
*********VARIABLE ASSIGNMENTS***********
We looked at Data "Types" when we began, this is where we put the idea of Data "Types"
to use.
When we are creating a new script we will always be working with some sort of value.
Whether the value is a number or text or a combination of both, even a physical grid
position, we need to use that value according to what it is.
Assigning a value to a variable is really easy to do.
string MyVar = "Hello!";
This line of code tells the server to "Remember" that the variable "MyVar" retains or holds
the string value "Hello!".
the "string" part tells the server that this variable is going to be "Text".
The "MyVar" part is the name I chose to give my variable so I would remember it.
The "= 'Hello!';" part is telling the server that the variable I just created called "MyVar"
contains the value "Hello!".
When you declare a new variable, you do not have to assign a value, you can assign
a value to your new variable later in your script if you don't know what the value of
the variable will be.
Its generally a good idea to start declaring your variables above, and before, your "default"
code block. This makes the value of these variables available to all of the "Functions" and
"Events" in your script, even if they change dynamically.
*********LETS GET STARTED!***********
Let's create a script, a simple script, which demonstrates the use of some
basic concepts that we learned about above.
You have the choice of rezzing a prim on the ground and creating a new
script inside your new prim, or right clicking on any folder in your inventory
and selecting the "New Script" option.
Let's begin by declaring a new string variable called "MyText" in the upper
script area, 'Before' our 'default' code block; Ignore the stars they are placed
into this script to keep the stages separate.
***************************
string MyText = "Hello There!";
***************************
Now we add the 'default' code block with the opening and closing curly braces;
***************************
string MyText = "Hello There!";
default
{
}
***************************
Next, we add our first event, the 'state_entry()' event code block,
this tells the server to do this first as soon as the script starts;
***************************
string MyText = "Hello There!";
default
{
state_entry()
{
}
}
***************************
Finally, we add a "Function" inside our "state_entry()" code block since the
server will check here first for "Functions" to execute;
***************************
string MyText = "Hello There!";
default
{
state_entry()
{
llSay(0, MyText);
}
}
***************************
Now click the "Save" button at the bottom left hand corner of your script
screen and the script will be saved.
Discussion:
What will this script do.
Scripting 101.a
By: Krull Kitty
Krull Quar in Second Life