Linden Scripting Language Script Structure

All scripts, have the following simple structure: They are a sequence of blocks called “states”. Inside each State block, is a sequence of code blocks called “event handlers”. And inside each event handler, is a sequence of “functions”.

Here's a example showing the structure:

State A {
  Event Handler 1 { do this, do that, ...}
  Event Handler 2 { do this, do that, ...}
  ...
}

State B {
  Event Handler 1 { do this, do that, ...}
  Event Handler 2 { do this, do that, ...}
  ...
}

...

The thing this works in-world is that, each object at any give time is always in one of the state as defined in the script. When a event occurs, for example, when a object is touched, pushed, in a particular position, or being sit on or talked to, the event handler in the current state will be called, and the functions inside the event handlers specify what happens next (For example, it may tell it to switch to another state).

Here's a actual code example. Try to see the “states:event-handlers:functions” structure:

// default state
default{
  state_entry() {
    llSay(0, "i'm on!"); // 
    llSetColor(<1,0,0>, ALL_SIDES); // sets all sides to red
  }
  touch_start(integer total_number) {
    state off;
  }
}

// another state
state off{
  state_entry() {
    llSay(0, "i'm off!");
    llSetColor(<0,0,0>, ALL_SIDES);
  }
  touch_start(integer total_number){
    state default;
  }
}

In the above code, the “default” is the default state. All code must have this state, and is the initial state of the object. All other states are named like “state xyz”, where xyz can be any user defined string.

The state_entry() is the initial event handler. It is invoked when the state is activated. The touch_start() is another event handler. It is invoked when the object is touched.

The llSay, llSetColor are all functions. They specify what to do. For details about each function's documentation, see: http://wiki.secondlife.com/wiki/Category:LSL_Functions.

The “state off” and “state default” are state switching statements. To switch to a target state, just use “state new_state_name”.

Another important even handler is state_exit(), which is executed when a state exits.


Page created: 2007-03.
© 2007 by Xah Lee.
Xah Signet