Scripting
The OpenSimulator Scripting Language (OSSL) and Linden Scripting Language (LSL) allows you to program interactivity into your virtual objects. Without this programming, objects are motionless and non- interactive. To make an object interactive, simply add a script. Using OSSL or LSL, you can create cars, planes, amusement park rides, weapons, and other entertainment devices.
Here are some good resources for beginners:
- LSL wiki – http://wiki.secondlife.com/wiki/LSL_Tutorial
- Video Tutorials – http://wiki.secondlife.com/wiki/Video_Tutorials
- OSSL Functions – http://opensimulator.org/wiki/Category:OSSL_Functions
Important Concepts
Before you start scripting, you need to understand a few important concepts and terminologies.
Inventory and Scripts
Each prim can store other things inside itself. You can think of the prim’s inventory as its pocket or closet. In its inventory, a prim can store other prims, objects, notecards, and scripts. In OpenSimulator, a script inside a prim can control the prim’s position and orientation, color, shape, and size, and other physical properties. It can enable the prim’s response to commands, reaction to the environment, or any other interactivity provided by the scripting language.
Physics and Vehicles
An object has a number of properties, including material. Material is especially important when you are scripting an object that behaves like a physical object, for example, a car. Any moving object that has physical properties is called a vehicle. Please note that as of the time of this writing, OpenSimulator offers two different physics engines for grid operators, the Open Dynamic Engine (ODE) and BulletSim. Which engine your grid is using depends on which engine the grid owner chose when they set up the grid. You may need to ask your grid owner. For more information about physics in OpenSimulator, see the physics wiki page.
States and Events
In OSSL and LSL, most scripts sit idle until they receive some input, or detect changes in their environments. At any moment, the script is in some state, and will react to events or inputs according to some scheme defined by the programmer. However, a script can also contain two or more different states, and react differently to events or inputs depending on its particular state. One common abstract model that is used in such cases is called a Finite State Machine.
For example, a door might be in a waiting state, and ignore all inputs except being touched. Once touched, it goes to the open state, in which it ignores being touched, but monitors which avatars pass through it. After a while, it changes to the closing state during which it closes, and then returns to the waiting state. States are not the only way to represent this kind of behavior, but in some cases they are a very good way.
In OSSL and LSL, a state is a specified section of code within which all events are specified. The main state that is required by all OSSL and LSL scripts is called default. All scripts must have a default state, and every state must have at least one event.
Here is a link to more detailed information on the “State Machine” that is LSL:
Running OSSL and LSL Scripts
OpenSimulator includes a facility for defining scripts within existing objects. To begin with your first script, rez a prim and in the Edit window, click the Contents tab. This directory is where scripts, if any are associated with an object, will appear.
<img>
Left-click on the New Script button, and a Script editing window will open. The editing window has a script editing area, where you can type or paste in a new script, and buttons for saving the script, undoing changes, etc.
<img>
The script editing area should contain the following default script:
default
{
state_entry()
{
llSay(0, "Script running");
}
}
Left-Click on the Save button. The message “Compile successful, saving…” should appear in the box below the script editing area. After a short pause you should see “Save complete” appear in the box.
Once the script is saved, you will see the message “Script running” appear in the text chat, usually in the lower-left-hand corner of your screen. This message indicates that the script has started.
Entering and Running a Simple Script
Here is a very simple program that changes the color and size of the object every time the object is touched.
integer counter;
default
{
state_entry()
{
llSay(0, "Script running");
}
touch_start(integer total_number)
{
// do these instructions when the object is touched.
counter = 0;
counter = counter + 1;
// choose three random RGB color components between 0. and 1.0. float redness = llFrand( 1.0 );
float greenness = llFrand( 1.0 );
float blueness = llFrand( 1.0 );
// combine color components into a vector and use that vector
// to set object color.
vector prim_color = < redness, greenness, blueness >;
// set object color to new color.
llSetColor( prim_color, ALL_SIDES );
// choose a random number between 0. and 10. for use as a scale factor.
float new_scale = llFrand(10.0) + 1.0;
// set object scale.
llSetScale(< new_scale, new_scale, new_scale > );
llSay( 0, "Touched by angel number " + (string)counter);
// choose a random number between 0. and 10. for use as a scale
float new_scale = llFrand(10.0) + 1.0;
// set object
llSetScale(< new_scale, new_scale, new_scale > );
llSay( 0, "Touched by angel number " + (string)counter);
}
}
As you click on the box, it will change with different colors and sizes and this program will count the number of times the object is touched, and write the number in the lower-left-hand corner of the screen. To insert this script into the object you just created, right-click on the object, reopen the Script editing window, paste this script into the Script editing area, and click Save. When the script has been saved, test it by right-clicking on the object and choosing Touch, as before.
You may have to do some debugging to correct errors, and when you finally get things running correctly, you may want to right-click on the object and choose Take to move the object into your Inventory. Objects in your inventory will remain there even after you log off, and will be available when you log back in.
When a touch_start event occurs, the program starts a timer that runs out every two seconds, causing a timer event. Whenever a timer event occurs, the script segment within the brackets below the string “timer()” is executed. The timer() section counts the number of times it is activated, and resets the script after twenty activations.
If multiple avatars touch the object at the same time, strange behavior may occur. Such an event may be monitored and controlled by using the total_number variable passed to touch_start.
Summary
This is just a very, very simple introduction to what can be accomplished in Scripting. Scripting and programming is a much broader topic than this manual can cover, so we encourage you to explore the many resources on the web to learn more about scripting. There are many varieties of scripts there that can be reverse engineered to fit your specific needs.
To learn more about scripting, see these links:
- LSL wiki – http://wiki.secondlife.com/wiki/LSL_Tutorial
- Video Tutorials – http://wiki.secondlife.com/wiki/Video_Tutorials
- OSSL Functions – http://opensimulator.org/wiki/Category:OSSL_Functions