User:Toady Nakamura/Touch Toggle Light

From Second Life Wiki
Jump to: navigation, search
  • Based on Simon Kline's "Scripting from Scratch Class" in 2010, modified by Toady Nakamura]
  • Place in prim, touch to change from dark to light.
  • Example of on/off toggling


integer on_off = FALSE; //STARTS IN THE OFF POSITION
 
default
{
    touch_start(integer total_number)
    {
        if (on_off == TRUE)        // if the light exactly equals "==" ON turn it off
        {
            llSetPrimitiveParams([PRIM_POINT_LIGHT, FALSE, <1, 1, 0.5>, 1.0, 10.0, 0.75,PRIM_GLOW ,ALL_SIDES,0]);
            llOwnerSay("Light is Off");
            on_off = FALSE;     // Flip to FALSE now that light is OFF
                                // note here only one "=" sign as the value is being set
 
        }
 
        else  // otherwise do the opposite
        {
            llSetPrimitiveParams([PRIM_POINT_LIGHT, TRUE, <1, 1, 0.5>, 1.0, 10.0, 0.75,PRIM_GLOW ,ALL_SIDES,0.2]);
            llOwnerSay("Light is On");
            on_off = TRUE;  // flip to TRUE now that light is ON
        }
    }
}

Another way to do the same thing

An "expert scripter" came along and changed this script nearly beyond recognition. I have reverted their edits so that the version I use in class remains to match the student's notes. However, the "expert" contribution may be useful to some people, so I am including their version here.

integer on_off;// = FALSE;
 
default
{
    touch_start(integer num_detected)
    {
    //  toggle between TRUE (1) and FALSE (0)
        on_off = !on_off;
 
        if (on_off == TRUE)
        {
            llSetLinkPrimitiveParamsFast(LINK_THIS,
                [PRIM_POINT_LIGHT, FALSE, <1.0, 1.0, 0.5>, 1.0, 10.0, 0.75,
                PRIM_GLOW, ALL_SIDES, (float)FALSE]);
 
            llOwnerSay("Light has been turned off.");
 
        }
        else
        {
            llSetLinkPrimitiveParamsFast(LINK_THIS,
                [PRIM_POINT_LIGHT, TRUE, <1.0, 1.0, 0.5>, 1.0, 10.0, 0.75,
                PRIM_GLOW, ALL_SIDES, 0.2]);
 
            llOwnerSay("Light has been turned on.");
        }
    }
}

Notice that while it is similar... all the helpful notes were stripped off and the note which was left was now irrelevant to the way it was coded. This is a user page, please do not make changes without using the "discussion" page, or merely appending your changes with explanation at the bottom.


Visit my LSL wiki page for my library of simple scripts ! Toady Nakamura