LSL 101/Simple Script Skeleton

From Second Life Wiki
Jump to navigation Jump to search

Here is the simplest possible valid LSL script. It doesn't actually ask the computer to do anything but all scripts have, at minimum, this structure:

<lsl> default {

    state_entry() {
    }

} </lsl>

In order to explain even this short piece of code we need to introduce you to some terms that will probably be new to you.

LSL scripts use a concept called states and events.

State is actually a very good name for what states do in LSL. If you think of a car, it can either be moving or stopped. We can say its state is moving (when it is moving) and its state is stopped (when it is stopped). Another example is your own state of being: you can be awake, asleep, active, sitting, standing, hungry, bored, confused, etc.

All LSL scripts have at least one state: the default state. This is the state when no other states are active. You can see in the code above the word default is used to tell the script about what happens in the default state.

Here is an example with two states:

<lsl> default {

    state_entry() {
        llOwnerSay("Switching to the hungry state...");
        state running;
    }

}

state running {

   state_entry() {
       llOwnerSay("I am very hungry! Does anyone have any spam?");
   }

} </lsl>

The first thing to notice is that the running state needs the word state, so that the script knows this describes a state rather than something else. The default state does not need to be proceeded by the word state.

Next notice the curly braces '{' and '}'. These tell the script which lines are part of the default state, which are part of the hungry state, and which belong to the state_entry() event handler (which we describe next).