Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

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

2fast4u Nabob
SL-ice.net
Join date: 28 Dec 2005
Posts: 542
07-07-2006 09:20
I have learned a lot from the scripting forums, so here is my first public script.

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.

NOTE: This is the most up-to-date version of the script

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 command to change the colors are:
/5555 white
/5555 red
/5555 green
/5555 blue

Enjoy :)

CODE

// 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 , 1.0> , <1.0 , 0.0 , 0.0> , <0.0 , 1.0 , 0.0> , <0.0 , 0.0 , 1.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();
}
}


-2fast
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
07-08-2006 08:37
/54/d2/119252/1.html
_____________________
i've got nothing. ;)
2fast4u Nabob
SL-ice.net
Join date: 28 Dec 2005
Posts: 542
Often requested Revision
10-15-2007 13:35
I have updated the script with an often-requested feature: listen all the time and do not support touch on/off.

Here is the revised script - refer to the comments for details:

CODE


// Sample touch lighting LSL script with color change capability - 10/15/2007 - 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:
//
// *Listens to owner and allows the color of the light to be changed
// *Commented code to help you figure out what is going on
//
// 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=555;


// Add pairs of color names and vectors...names of colors must be in lower case
list colors=[
"white",<1.0 , 1.0 , 1.0> ,
"red", <1.0 , 0.0 , 0.0> ,
"green", <0.0 , 1.0 , 0.0> ,
"blue",<0.0 , 0.0 , 1.0>
];

// Edit below this line at your own risk ;)
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.0]);
}

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

state_entry()
{
light_color = llList2Vector(colors , 1); //Set the initial color to white
// Listen on channel_num and only to owner. Listen to anyone by changing llGetOwner() to NULL_KEY
llListen(channel_num , "" , llGetOwner() , "");
}

listen(integer channel , string name , key id , string message)
{
// Look in the list for what the user said (convert the message to lower case first)....
integer listLocation;
string cmd=llToLower(message);
listLocation = llListFindList(colors , [cmd]);
// Did not find what the user said in the list? Could be a command...
if(listLocation == -1)
{
if("on" == cmd)
{
isOn=TRUE;
setLightParameters();
}
else if ("off" == cmd)
{
isOn=FALSE;
setLightParameters();
}
return;
}
else
{
// found what the user said in the list , set the corresponding vector
light_color = llList2Vector(colors , listLocation+1);
// set the light's parameters - does not change On/Off setting
setLightParameters();
}
}
}


Jerry Lebed
Registered User
Join date: 10 Nov 2007
Posts: 13
11-11-2007 07:19
is there a way to make this script only listen to a certain group of people (and also: to add people/remove people from this list)?
Tazmania Trefusis
Registered User
Join date: 13 Jan 2008
Posts: 85
04-23-2008 21:33
Love the script but just wanted to query why the prim itself doesn't light up with the selected colour and only the area.
Would that require much in the way of changes to the script?
Take care
Sebastian Oxide
Registered User
Join date: 26 Sep 2006
Posts: 1
Click to change
05-23-2008 07:49
Can this script be modified so you simply click for the object to change color according to a list? No need to listen just wait for a click.