Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: 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.

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
Original Thread
07-08-2006 08:37
/15/5f/119036/1.html
_____________________
i've got nothing. ;)
2fast4u Nabob
SL-ice.net
Join date: 28 Dec 2005
Posts: 542
Updated
07-12-2006 06:07
I updated the script to correct a minor problem where the color of the light was not set correctly when the script resets. This caused the script to look like it was not working when in fact it was, but the color of the light was black.

-2fast
Burke Prefect
Cafe Owner, Superhero
Join date: 29 Oct 2004
Posts: 2,785
07-13-2006 09:14
This gives me some ideas. I'm working on a script that can be set to touch on/off or stepped like a lamp, or respond to listens and take entire light params. I might implement some of this. The script'll be opensource when it's done. :D
_____________________
Drizzt Naumova
Teh Foxeh DJ
Join date: 9 Oct 2005
Posts: 116
07-24-2006 22:23
Hmm, its seems to not be working now. Just to note, i can get the script to compile succesfully, but when placed into a prim and touched..the light comes on, but it will not change colors. An sl update script bork, maybe? it worked for me in the earlier.

and also when fixed, would it be possible to make this particular script listen for commands on a particular channel? e.g. /83 on or /83 red or /83 off and still keep the low lag cancel listen after 30 seconds?

Just thought i would post an update about this useful script :)

*EDIT*

Just seen Burke's post about the listen addition :)
2fast4u Nabob
SL-ice.net
Join date: 28 Dec 2005
Posts: 542
07-24-2006 22:34
From: Drizzt Naumova
Hmm, its seems to not be working now.


I am having problems too. It seems to be related to lag - I have tested on myo own sim and it works as designed - when I go elsewhere it does not work correctly.

Also note that the 30 second timer does not reset each time you touch the lamp. The timer starts when the light is touched the first time and runs out after 30 seconds regardless of how many times you touch the light within 30 seconds. So, try to let the timer run out, then turn the light on, then change the color - that may help since you might be issuing the color change command near the end of the 30 second timer.

From: Drizzt Naumova
would it be possible to make this particular script listen for commands on a particular channel? e.g. /83 on or /83 red or /83 off and still keep the low lag cancel listen after 30 seconds?


Oh sure...just change the value of the channel_num variable to 83 as follows:

CODE

integer channel_num=83;


If you have an update, please share :)

-2fast
Drizzt Naumova
Teh Foxeh DJ
Join date: 9 Oct 2005
Posts: 116
07-25-2006 02:40
thanks
but in relation to changing the way the script is turned on and off:
instead of using touch activation to turn on and off, i was reffering to making it possible to turn it on or off using chat commands

and i will test your theory out also to see if it was the timer not expiring fast enough for my project :)
Will keep you updated about it

*EDIT*

From: someone
light_color=llList2Vector(colorVectors,listLocatio n);


also there is a typo there at the end in the word "listLocation". There was a space between the o and n, 2fast :) that would cause a syntax error if not caught. Its the most simple mistakes that can make you pull your hair out hehe :D

should be:
listLocation);
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
07-25-2006 10:36
Actually, Drizzt, that space is a bug in the forum's lsl highlighter. The script is entered correctly -- you can verify this by clicking 'quote' on his post and examining the script.
2fast4u Nabob
SL-ice.net
Join date: 28 Dec 2005
Posts: 542
07-25-2006 11:12
From: Drizzt Naumova
instead of using touch activation to turn on and off, i was reffering to making it possible to turn it on or off using chat commands


I can easily modify the script to respond to commands like /83 on and /83 off; however, that would undermine the script's low-lag feature.

Getting the light to respond to on/off chat commands requires that the listener remain active at all times - to listen of the on/off and other commands.

I wrote the script to activate the listener on touch only so that I could terminate the listener after 30 seconds. In addition, the listen attempts to reduce server load by listening only to the owner of the object the script is in.

Implementing your change is pretty straight forward and might be a good exercise for someone that is starting to learn about scripting. You need to modify the listen function and check the value of the "message" parameter to determine what was said when the value of listLocation is -1 (there is presently a return statement after that check). You can even reuse the code from the touch function to switch the light on and off. Just keep in mind that you need to always keep the listener active at all times too (hint: get rid of llSetTimerEvent(30) and move the llListen call into state_entry).

As for bugs as a result of additional spaces appearing in the code - you need to get used to that. As Keknehv said, there is a bug in the forum highlighter that sometimes adds spaces. I have seen people that are not familiar with scripting correct that problem themselves so it seems to be very well understood among the SL community.

-2fast
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
07-25-2006 14:17
From: 2fast4u Nabob
As for bugs as a result of additional spaces appearing in the code - you need to get used to that. As Keknehv said, there is a bug in the forum highlighter that sometimes adds spaces. I have seen people that are not familiar with scripting correct that problem themselves so it seems to be very well understood among the SL community.

-2fast


Although I agree with this comment, you can help things along. It basically puts a space in every so many characters (60? 72? something like that) if there hasn't been one already.

Simply changing your lines to have spaces after commas and either side of an equals sign will stop 99% of it.
_____________________
Eloise's MiniMall
Visit Eloise's Minimall
New, smaller footprint, same great materials.

Check out the new blog
2fast4u Nabob
SL-ice.net
Join date: 28 Dec 2005
Posts: 542
07-31-2006 13:13
From: Eloise Pasteur
Simply changing your lines to have spaces after commas and either side of an equals sign will stop 99% of it.


Super...I did not know that...I have updated the script here and in the Scripting Library.

-2fast
Kaimi Kyomoon
Kah-EE-mee
Join date: 30 Nov 2006
Posts: 5,664
08-27-2007 00:57
Thank you!
_____________________



Kaimi's Normal Wear

From: 3Ring Binder
i think people are afraid of me or something.