Ready to bring your creations to life? The Linden Scripting Language (LSL) is how you make objects interactive. This guide will walk you through the absolute basics.
Every script, no matter how complex, is built from three fundamental concepts.
A state is a mode of behavior. An object can be in an "on" state or an "off" state. A door can be "open" or "closed." Every script starts in the `default` state.
Events are triggers. When someone touches an object (`touch_start`), a timer goes off (`timer`), or the object collides with something (`collision`), an event runs.
Functions are the actions. `llSay()` makes an object talk. `llSetColor()` changes its color. `llDie()` deletes the object. You use functions inside events to make things happen.
This is the traditional first step in any programming language. We'll make a prim say "Hello, World!" when you touch it.
default
{
touch_start(integer total_number)
{
llSay(0, "Hello, World!");
}
}
default { ... }
: The script begins in its one and only state, the `default` state.touch_start(...) { ... }
: This defines the `touch_start` event. The code inside these curly braces `{}` will only run when someone clicks the object.llSay(0, "Hello, World!");
: This is the function that does the work. It tells the object to say "Hello, World!" in public chat.Let's make something more useful. This script will make a prim toggle between white ("on") and black ("off") when touched. This will teach you how to use multiple states and store information in a variable.
// Define colors to use later. This part is outside of any state.
vector COLOR_ON = <1.0, 1.0, 1.0>; // White
vector COLOR_OFF = <0.0, 0.0, 0.0>; // Black
default // This will be our "ON" state
{
state_entry()
{
llSay(0, "Light is now ON.");
llSetColor(COLOR_ON, ALL_SIDES); // Set the color to white
}
touch_start(integer total_number)
{
state off; // When touched, switch to the 'off' state
}
}
state off
{
state_entry()
{
llSay(0, "Light is now OFF.");
llSetColor(COLOR_OFF, ALL_SIDES); // Set the color to black
}
touch_start(integer total_number)
{
state default; // When touched, switch back to the 'default' (ON) state
}
}
You've learned the most important concepts! The best way to learn now is to experiment.