Difference between revisions of "Toggle States"
Jump to navigation
Jump to search
m (lsl code tagging) |
m (<lsl> tag to <source>) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
{{LSL Header}} | {{LSL Header|ml=*}} | ||
=====Toggle via State Change===== | =====Toggle via State Change===== | ||
An example script showing how a [[LSL_States|state]] change can be used to switch between two script modes, in this case, On or Off. When the script first runs, it begins in the '''default''' state and the [[LSL_state_entry|state_entry]] event is executed. When a user interacts with the object by touch, the script switches to the '''on''' state. The '''on''' state executes its state_entry event, then waits for a user's touch to trigger a state change back to the '''default''' state. | An example script showing how a [[LSL_States|state]] change can be used to switch between two script modes, in this case, On or Off. When the script first runs, it begins in the '''default''' state and the [[LSL_state_entry|state_entry]] event is executed. When a user interacts with the object by touch, the script switches to the '''on''' state. The '''on''' state executes its state_entry event, then waits for a user's touch to trigger a state change back to the '''default''' state. | ||
< | <source lang="lsl2"> | ||
// Toggle On-Off via State | // Toggle On-Off via State | ||
Line 29: | Line 29: | ||
} | } | ||
} | } | ||
</ | </source> | ||
[[Category:LSL Examples]] | [[Category:LSL Examples]] |
Latest revision as of 18:10, 24 January 2015
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Toggle via State Change
An example script showing how a state change can be used to switch between two script modes, in this case, On or Off. When the script first runs, it begins in the default state and the state_entry event is executed. When a user interacts with the object by touch, the script switches to the on state. The on state executes its state_entry event, then waits for a user's touch to trigger a state change back to the default state.
// Toggle On-Off via State
default {
state_entry() {
// run this code when entering the default state
// displays red "OFF" as floating text above the prim
llSetText("OFF", <1,0,0>, 1.0);
}
touch_start(integer num_detected) {
// when touched, switch to state named 'on'
state on;
}
}
state on {
state_entry() {
// run this code when entering state 'on'
// displays green "ON" as floating text above the prim
llSetText("ON", <0,1,0>, 1.0);
}
touch_start(integer num_detected) {
// when touched, switch to the default state
state default;
}
}