Difference between revisions of "State"

From Second Life Wiki
Jump to navigation Jump to search
m (changed example script)
(Change example to do state change from touch_end event)
Line 1: Line 1:
{{Issues/SVC-3017}}{{LSL Header|ml=*}}
{{LSL Header|ml=*}}
{{RightToc}}
{{RightToc}}
<div id="box">
<div id="box">
Line 6: Line 6:
In LSL, most scripts sit idle until they receive some input, or detect some change in their environment. 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 what state it is in.
In LSL, most scripts sit idle until they receive some input, or detect some change in their environment. 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 what state it is in.


The main state is the '''default''' state. When a script is compiled, reset or loaded, this is the state it enters by default. After the default state definition can follow additional state definitions which the script may use to change how and which events are handled.
The main state is the '''default''' state. When a script is compiled, reset or loaded, this is the state it enters by default. After the default state definition, additional state definitions may follow, which the script may use to change how and which events are handled.
</div>
</div>
</div>
</div>
Line 56: Line 56:
     state_entry()
     state_entry()
     {
     {
        // PUBLIC_CHANNEL has the integer value 0
         llSay(0,
         llSay(PUBLIC_CHANNEL,
             "You either just saved the script after editing it"
             "You either just saved the script after editing it"
             + "\nand/or the script (re)entered the default state.");
             + "\nand/or the script (re)entered the default state.");
Line 65: Line 64:
     }
     }


     touch_start(integer num_detected)
     touch_end(integer num_detected)
     {
     {
        // Note: NEVER do a state change from within a touch_start event -
        // - that can lead to the next touch_start on return to this state to me missed.
        // Here we do the state change safely, from within touch_end
         state other;
         state other;
     }
     }
Line 72: Line 74:
     state_exit()
     state_exit()
     {
     {
         llSay(PUBLIC_CHANNEL, "The script leaves the default state.");
         llSay(0, "The script leaves the default state.");
     }
     }
}
}


state other
state two
{
{
     state_entry()
     state_entry()
     {
     {
         llSay(PUBLIC_CHANNEL, "The script entered the other state.");
         llSay(0, "The script entered state 'two'");
         state default;
         state default;
     }
     }
Line 86: Line 88:
     state_exit()
     state_exit()
     {
     {
         llSay(PUBLIC_CHANNEL, "The script leaves the other state.");
         llSay(0, "The script leaves state 'two'");
     }
     }
}
}

Revision as of 04:19, 27 December 2012

Description

In LSL, most scripts sit idle until they receive some input, or detect some change in their environment. 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 what state it is in.

The main state is the default state. When a script is compiled, reset or loaded, this is the state it enters by default. After the default state definition, additional state definitions may follow, which the script may use to change how and which events are handled.

The correct title of this article is state. The initial letter is shown capitalized due to technical restrictions.

default { events }

default { events }
• event events one or more events

The default state definition.

state target { events }

• label target state name
• event events one or more events

target state definition.

state target;

• label target name of a state to run

When a state target; is encountered at runtime, if the current state and the target state are different:

  1. Trigger state_exit in the current state if it exists and clear the event queue.
  2. Change state to target, any listens are unregistered.
  3. Trigger state_entry in the target state if it exists.

If target state is the same as the current state, no state change occurs nor do any of the side effects.

Caveats

  • On state change:
    • All listens are released.
    • The event queue is cleared. (see Notes for a partial workaround)
    • Repeating sensors are released.
    • The timer event clock is not cleared.
    • This means if the new state has a timer event, and the previous state has a timer set, the timer event in the new state will be triggered on the interval set in the previous state.
  • The default state must be defined before all others.
  • States cannot have user functions or variables inside their immediate scope, only event definitions may be inside a states scope.
  • User-defined (global) functions cannot change a script's state. The compiler will throw the error 'ERROR: Global functions can't change state'. Note: Previously, global functions could change state in the body of a simple 'if' statement; this "workaround" no longer works.

Examples

<lsl> default {

   state_entry()
   {
       llSay(0,
           "You either just saved the script after editing it"
           + "\nand/or the script (re)entered the default state.");
       // white and opaque text
       llSetText("Click to change states", <1.0, 1.0, 1.0>, (float)TRUE);
   }
   touch_end(integer num_detected)
   {
       // Note: NEVER do a state change from within a touch_start event -
       // - that can lead to the next touch_start on return to this state to me missed.
       // Here we do the state change safely, from within touch_end
       state other;
   }
   state_exit()
   {
       llSay(0, "The script leaves the default state.");
   }

}

state two {

   state_entry()
   {
       llSay(0, "The script entered state 'two'");
       state default;
   }
   state_exit()
   {
       llSay(0, "The script leaves state 'two'");
   }

} </lsl>

Notes

See Also

Keywords

•  jump
•  return

Events

•  state_entry
•  state_exit