Differences

This shows you the differences between two versions of the page.

Link to this comparison view

wiki:scripting_portal:state [2018/11/03 05:05]
Royale Mobian created
wiki:scripting_portal:state [2018/11/03 05:17]
Royale Mobian
Line 31: Line 31:
 If //target// state is the same as the //current// state, no state change occurs nor do any of the side effects. 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 ====
 +<code ossl>
 +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 be missed.
 +        // Here we do the state change safely, from within touch_end
 +        state two;
 +    }
 + 
 +    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'"​);​
 +    }
 +}
 +</​code>​
 +  * Tutorial on States, Events, and Functions
 +
 +==== Notes
 +  * To avoid dumping previously queued events, consider moving the state target command to the timer event, and calling it with llSetTimerEvent( 0.01 ), then stopping the timer in state_entry of the new state. Do NOT use state_exit if this is a concern.
 +
 +===== See Also =====
 +==== Keywords ====
 +  * jump
 +  * return
 +==== Events ====
 +  * state_entry
 +  * state_exit
  
QR Code
QR Code wiki:scripting_portal:state (generated for current page)