A script in Second Life is written with LSL and is used to allow an object to interact with the world.
When looking at an objects edit menu, there are several tabs at the top: General, Object, Features, Texture, and Contents.
The first four describe the look and feel of a given object. Contents is a listing of things contained within the object, such as notecards and scripts. While a notecard is a document containing information to be read, a script is a document of actions to be performed.
In Second Life, scripts are used to control how objects behave. They can be used to control an objects appearance and behavior. They can also cause an object to provide or collect information as well.
First Script[]
There are any number of scripts floating around available for general use, and they can be a good way of learning the basics of scripting. One of them can be found below:
//Hovering Text // Customizing the script: // Change the text inside of "TEXT GOES HERE" to whatever you please. // The <1.0,1.0,1.0> is the color the text will show in Float form, 1.0,1.0,1.0 // being WHITE while, 0.0, 0.0, 0.0 is BLACK this is all in RGB (Red, Green, Blue). // experiement with combinations to get different colors. // The 1 at the end sets the text's transparency, 1.0 being SOLID, while 0 would be clear, // and .5 would be half way between clear and solid. default { state_entry() { llSetText("TEXT GOES HERE", <1.0,1.0,1.0>, 1); } }
Using this particular script, you can label your object with a public label that everyone can see. You may have noticed that a number the lines at the top of the program are preceded by double slashes. That is an indication that everything that follows on that line is nothing but commentary, and therefore should not be executed.
States[]
Central to every script is the idea of the state. Every script has at least one state, and depending on its complexity, may have several. The one state that every script will have is the default state. You can think of it as being the main module, to which all other modules are subordinate, with each module being a state.
Events[]
Within any given state, there will be one or more events. With two exceptions, events are triggered by external actions happening to the object containing the script. And these are handled by whatever event handlers have been included in whatever state is currently in control.
An example of an event handler would be touch_start, which is triggered when the object has been touched by someone. When triggered, the user code that has been associated with the event_handler will be executed. For more event handlers, see http://wiki.secondlife.com/wiki/Category:LSL_Events
In the example above, another event known as State_entry was used. This is a one time event that is executed as soon as the state is initiated, and then never again.
Flow Control[]
Many scripts, especially smaller ones like the earlier example, are strictly sequential. They are just a series of instructions followed in sequence. But sometimes there are decisions to be made, or actions to be repeated. That is when flow control statements become relevant.
Example Two - Clicking the object, puts the object's contents into the avatar's inventory in its own folder |
list inventory = [];
default { state_entry() { integer i; for ( i = 0; i < llGetInventoryNumber(INVENTORY_ALL); i++) { string item = llGetInventoryName(INVENTORY_ALL, i); if (llGetInventoryPermMask(item, MASK_OWNER) & PERM_COPY) { inventory += [item]; } } integer index = llListFindList(inventory, [llGetScriptName()]); inventory = llDeleteSubList(inventory, index, index); } touch_start(integer total_number) { llGiveInventoryList(llDetectedKey(0), llGetObjectName(), inventory); } } |
If...Then..Else[]
The If structure is used to determine whether a block of instructions should be executed.
The basic structure looks as follows:
If condition { Code for when condition is true } else { Code for when condition is false}
This is a script for a
For[]
This is used for repeating a set of instructions a fixed number of times. In example two, the number of repetitions would be equal to the number in inventory items in the object.
While[]
The basic advantage of the For loop is that you can repeat a set of instructions a specific number of times. But what if you have no idea of how many times the instructions should be repeated. In that case you will want to use the While loop as follows:
While (Condition = True) { Do_these_Instructions}
As long as the Condition is true the loop will continue executing. Just make sure that the condition is capable of being false or you will never get out of it.
Do While[]
Do {Set of instructions} While (Condition = True)
Conceptually the same as the While loop, the Do While loop is used when you know for sure that you want the instructions to be executed at least once.
Functions[]
These are the components that do the real work. They may be functions that you have created. Or they may be ones that are already built into the language. Linden Lab functions will always start with two small L's.
Basic Concepts[]
It is beyond the scope of this wiki (for now) to include basic information on scripting terms aside from:
Basic Terms[]
More common scripting/programming terms linked to here are:
- argument
- expression
- call
- constant
- list
- null
- string
More info about these can be found on the LSL Wiki.