MetaHammer is about tools and resources with impact. A metahammer is a script, a tool, a piece of software, an idea, or... anything... that has impact on what we do and on how we think.

LSL Snippets: Toggle

Function: Toggle Switch

Description: Alternates between two actions, like turning a light on and off.

Clarification: A toggle is designed to run two actions alternatively. In its simplest form, it will start or stop an action. But it’s important to think of stopping an action as starting another action that negates the previous action. For example, we think of turning off a light as stopping the flow of electricity to the light using a switch. In SL, however, a point light emitter is not a continuous action, but rather a steady state. There is no electricity running to the virtual lamp. It is simply in a light emitting state or a non-light emitting state. Similarly, a rotating object is in a moving or non-moving state. In order to change states, you have to set a new state using the same function used to set the previous state. Thus, if an object is rotating using llTargetOmega and you want to stop the rotation, you must invoke the llTargetOmega function and set some parameter to zero to actively stop it.

This works no matter how the function is invoked. A light or rotation can be started or stopped using a touch_start(), timer(), sensor(), listen(), etc. All of these are events triggered by various interactions.


On-off, like a light switch. Sets PRIM_LIGHT condition to zero of not zero.

Swap textures.

Open/close a door.

Start/stop a sound, or rotation, or any continuing action.

Activated by:

touch_start, collisions, anything that triggers a function.


integer on=FALSE; //Place at top of script to define global variable. The variable "on" is either TRUE or FALSE. If it's not TRUE, then it has to be FALSE, and vice versa.
//For a light switch, the default state of the light is normally off, which is the same as saying "not on," or "on=FALSE".

touch_start(integer num_detected)
//touch_start is used to signal a process that initiates once when the object is touched. You can use the same process with a collision_start or sensor() event.
if (on) //Is the light on? If so, do the following
//turns prim light off
on=FALSE;//Resets the variable for next touch_start.
}//End if and stop.

else{//If the previous condition was not true, do the following
llSetPrimitiveParams([PRIM_LIGHT,1,<1,1,1>,1]); //turns prim light on
    on=TRUE;//Resets the variable for next touch_start.
}// End else
}//end touch_start

//—–End Snippet—–


Instead of using the 0n=TRUE/FALSE test, you can substitute the following:


This automatically switches the variable between TRUE and FALSE.  Thus, the more concise snippet:

integer on=FALSE; //Place at top of script to define global variable.

touch_start(integer num_detected)
//The exclamation point means "not". If on was TRUE, on is now not TRUE (i.e.,FALSE) and vice versa.
if (on)

//—–End Snippet—–

Comments are closed.