Wednesday, August 02, 2006

Light with Touch On/Off and changeable color via chat commands

Ok...I am a professional programmer (well..software architect) in real life sooo....I can script in SL too. I have been doing a fair bit of work on various projects and posted this script on the SL Scripting Library for anyone to use.

I am often asked about light scripts - lighting a prim is easy, the contols around it can be tricky.

This script has some nice control features and is low lag too.

Features:

*Anyone can touch to turn On/Off
*Listens to owner and allows the color of the light to be changed on channel /5555
*Low lag - listens to owner on touch only and cancels the listen after 30 seconds
* Detaled comments to help new scripters

The commands to change the colors are:
/5555 white
/5555 red
/5555 green
/5555 blue

Say the commands after turning the light on or off.

Enjoy...the script follows:




// Sample touch lighting LSL script with color change capability - 07/07/2006 - 2fast4u Nabob
//
// * Permission granted to modify and create derivative works.
// * If you sell this script, include attribution to 2fast4u Nabob, the author of
// this script in your product's documentation and primary script.
//
// Provided "as is"
//
// Features:
//
// *Touch On/Off
// *Listens to owner and allows the color of the light to be changed
// *Low lag - listens to owner on touch only and cancels the listen after 30 seconds
//
// Instructions:
//
// 1.Place this script into a prim that you want to light-up when touched
// 2. Thank 2fast4u Nabob for helping you :)
//

// channel_num is the channel that the light listens on. Type /5555 ... to talk to the light
integer channel_num=5555;
// colorWords lists the colors that the light supports - add here and in colorVectors
list colorWords=["white" , "red" , "green" , "blue"];
// colorVectors represent the color values that the light supports
list colorVectors = [<1.0> , <1.0> , <0.0> , <0.0>];

// Don't edit anything below this line

integer isOn = FALSE;
vector light_color;

integer listenHandle = 0;

// This makes it easier to change the light's parameters in one place
setLightParameters()
{
llSetPrimitiveParams ([PRIM_POINT_LIGHT , isOn , light_color , 1.0 , 10.0 , 0.75]);
}

default
{
on_rez(integer start_param)
{
llResetScript(); //Reset in case owner changed
}

state_entry()
{
light_color = llList2Vector(colorVectors , 0); //Set the initial color to white
}
touch_start(integer num_detected)
{
// If not already listening , start listening to the owner
if(listenHandle == 0)
{
listenHandle = llListen(channel_num , "" , llGetOwner() , "");
llSetTimerEvent(30.0);
}
// Toggle the light on/off
isOn = !isOn;
// Set the light's parameters
setLightParameters();
}

timer()
{
// If the script is listening , stop listening and stop the timer to reduce lag
if(listenHandle != 0)
{
llListenRemove(listenHandle);
llSetTimerEvent(0.0);
}
}

listen(integer channel , string name , key id , string message)
{
// The following IF statement is for the paranoid :) Not really necessary to check the
// id against the owner , but no harm in doing it anyway
if(channel != channel_num id != llGetOwner())
return;
// Look for what the user says in the list....
integer listLocation;
listLocation = llListFindList(colorWords , [message]);
// Did not find what the user said in the list? Stop here
if(listLocation == -1)
return;
// found what the user said in the list , set the corresponding vector
light_color = llList2Vector(colorVectors , listLocation);
// set the light's parameters - does not change On/Off setting
setLightParameters();
}
}






No comments: