LSL States

From Second Life Wiki
Revision as of 15:44, 16 February 2007 by Anthony Reisman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

States are a specified section of code within which all Events are specified. The main state that is required by all LSL scripts is default and is automatically created when a new script is created in Second Life.

All scripts must have a default state, and all states must have a state_entry event.

To add another state add an entry like this:

<lsl>
 state my_state
 {
     state_entry()
     {
          // What should happen when we enter this state
     }

     // other events and code
 }

</lsl>

To switch from one state to another add a line like this:

<lsl>
state my_state;
</lsl>

When this line of code is executed, it will run anything in the state_exit event, and then switch to the new state.

When switching states, all event queues are cleared, and events requiring setup are disabled such as timer, sensor, and listen.

The state_entry event is run when the code enters that state. There is sometimes confusion in thinking that the stat_entry event will run when a scripted object is rezzed from inventory. This is not the case as scripts are frozen when taken into inventory in the state they are in, and resume operation in that state when rezzed. The event first called when an object is rezzed out of inventory is on_rez.