Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

controling light intensity with touch

Pratyeka Muromachi
Meditating Avatar
Join date: 14 Apr 2005
Posts: 642
05-26-2006 07:57
With the latest lighting changes in SL I have decided to try my hand at scripting. I have made an on/off touch switch that works great. Now I want to adjust the intensity of the light using touch. One part of the lamp will increase intensity when touched, another part will lower the intensity when touched.

I did search for something similar without success. Can anyone post a hint on how to achieve this?
Thanks in advance
_____________________
gone to Openlife Grid and OpenSim standalone, your very own sim on your PC, 45,000 prims, huge prims at will up to 100m, yes, run your own grid on your PC, FOR FREE!
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
05-26-2006 08:06
Change the intensity.
CODE

float bright = 0;

default
{
touch_start(integer n)
{
bright += 0.2;
if(bright > 1.0)
{
bright = 0.0;
llSetPrimitiveParams([PRIM_POINT_LIGHT,FALSE,<0,0,0>,0.0,10.0,1.0]);
}
else
{
llSetPrimitiveParams([PRIM_POINT_LIGHT,TRUE,<1,1,1>,bright,10.0,1.0]);
}
}
}
Androclese Antonelli
Org. B-Day: 05/11/04
Join date: 25 Apr 2006
Posts: 96
05-26-2006 08:14
That would involce using LinkMessages. I'm not in a place where I can throw a script together right now, but when I get home, if nobody else has done it, I'll give you an example.

For the 10,000ft overview, you have three scripts.

One Main script that starts up and listens for the other two throught the message_link process

One "bright" script that sends a llMessageLink() to the primary script with a pre-determined value that the main script listens for.

One "dimn" script that sends a llMessageLink() to the primary script with a pre-determined value that the main script listens for.

You could use the integer 1000 for the "bright" listener value and "2000" as the "dim" listener value.

When the main script hears "1000" it raises the color vector by X value. When it hears "2000" it lowers the color vector by X value.

I would suggest using a prepared list with the color values you want, that way you can control the color range and you don't have to worry about where to stop either up or down though math. If you reach the end of the list, stop increasing/decreasing.

I hope that helps.
_____________________
The Sculpted Garden

Originally Born 5/11/2004 - New AV, Old Player.

If you leave the game, don't delete your account, just make it 'free'. You'll lose your inventory like I did. *sniff*
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
05-26-2006 08:32
Oh, yeh, if you want to have separate up/down buttons, sure.

But typical RL lights just cyclethrough a fixed series of levels, usually around three to five of them, and you only need one touch to cycle up through them.

Easier to implement and more realistic.
Pratyeka Muromachi
Meditating Avatar
Join date: 14 Apr 2005
Posts: 642
05-26-2006 08:33
Argent: Thanks, that looks like a simple and elegant solution. If I understand correctly, your script does turn the light on and increase the intensity each time the object is touched, then if the intensity is at max, the next touch turns the light off. Very nice.

Andro: I understand the principle, I just need to read up on LSL more to know how to achieve this. An example would speed up my learning quite a bit. Whenever you have time, send me a basic example. Thanks!
_____________________
gone to Openlife Grid and OpenSim standalone, your very own sim on your PC, 45,000 prims, huge prims at will up to 100m, yes, run your own grid on your PC, FOR FREE!
Pratyeka Muromachi
Meditating Avatar
Join date: 14 Apr 2005
Posts: 642
05-26-2006 08:37
From: Argent Stonecutter
Oh, yeh, if you want to have separate up/down buttons, sure.

But typical RL lights just cyclethrough a fixed series of levels, usually around three to five of them, and you only need one touch to cycle up through them.

Easier to implement and more realistic.


The reason I'd like a separate on/off and intensity adjustment is I need LOTS of lamps in different rooms of a big building. It reduces left-click wear on my mouse when it's time to turn them off.

Now that I think of it... It would be nice to turn them all off at the same time, with just one click...
_____________________
gone to Openlife Grid and OpenSim standalone, your very own sim on your PC, 45,000 prims, huge prims at will up to 100m, yes, run your own grid on your PC, FOR FREE!
Burke Prefect
Cafe Owner, Superhero
Join date: 29 Oct 2004
Posts: 2,785
05-26-2006 09:02
Hmmm. Time for some mood lighting! Woot!
_____________________
Androclese Antonelli
Org. B-Day: 05/11/04
Join date: 25 Apr 2006
Posts: 96
05-26-2006 10:13
Here is your Dimmer Code:

Put this in the "light" part of your lamp

CODE

// -[ Licensing and Copyright ]-----------------------
//
// Script copyright by Androclese Antonelli of Second Life :: 20060526
//
// This program is free software; you can redistribute it and/or modify it under the terms of the
// GNU General Public License version 2 as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along with this program in a
// notecard; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.
//
// GPL license version 2 on the web: http://www.gnu.org/copyleft/gpl.html (Note, URL might change
// once v3 is released.)
//
// Dimmer Range Black ---> White
list colors = [ "<0, 0, 0>", "<0.125, 0.125, 0.125>", "<0.25, 0.25, 0.25>", "<0.375, 0.375, 0.375>", "<0.5, 0.5, 0.5>", "<0.625, 0.625, 0.625>", "<0.75, 0.75, 0.75>", "<0.875, 0.875, 0.875>", "<1, 1, 1>" ];
// Length of the Color Range List
integer colorlistlength;
// Current list position
integer colorlistposition;

default
{
state_entry()
{
colorlistlength = ( llGetListLength(colors) - 1 );
colorlistposition = 0;
//llSetPrimitiveParams([PRIM_POINT_LIGHT, TRUE, <0, 0, 0>, 1.0, 10.0, 0.75]);
llSetPrimitiveParams([PRIM_POINT_LIGHT, FALSE, <0, 0, 0>, 0, 0, 0]);
}

link_message(integer sender_num, integer number, string message, key agent) {
// Lite Emitter Toggle
if ( number == 1000 ) { // Brighten the Light
if ( (colorlistposition <= colorlistlength) && (colorlistposition != 0 ) ) {
--colorlistposition; // move down the list by one
string requestedcolor = llList2String(colors, colorlistposition);
// Set the color
llSetPrimitiveParams([PRIM_POINT_LIGHT, TRUE, (vector)requestedcolor, 1.0, 10.0, 0.75]);
return;
}
}

if ( number == 2000 ) { // Brighten the Light
if ( (colorlistposition <= colorlistlength) && (colorlistposition != colorlistlength ) ) {
++colorlistposition; // move down the list by one
string requestedcolor = llList2String(colors, colorlistposition);
// Set the color
llSetPrimitiveParams([PRIM_POINT_LIGHT, TRUE, (vector)requestedcolor, 1.0, 10.0, 0.75]);
return;
}
}
}
}


Here is the Dimmer switch. Put it in the Prim you want to touch to dim the lamp:

CODE

default
{
touch_start(integer total_number)
{
llMessageLinked (LINK_SET, 1000, "", "");
}
}


Here is the Bright switch. Put it in the Prim you want to touch to brighten the lamp:

CODE

default
{
touch_start(integer total_number)
{
llMessageLinked (LINK_SET, 2000, "", "");
}
}


I've sent you a copy of the object in SL.
_____________________
The Sculpted Garden

Originally Born 5/11/2004 - New AV, Old Player.

If you leave the game, don't delete your account, just make it 'free'. You'll lose your inventory like I did. *sniff*
Pratyeka Muromachi
Meditating Avatar
Join date: 14 Apr 2005
Posts: 642
05-26-2006 12:11
Androclese: Thank you for the script. I have tried it and studied it, then I modified it to get the effect I was after. Your script act through the color vector. What I did is control the intensity, leaving the color vector alone. This way, I preserve the color of the lamp. I also included the on/off part, activated by a third prim.

CODE
// -[ Licensing and Copyright ]-----------------------
//
// Script copyright by Androclese Antonelli of Second Life :: 20060526
//
// This program is free software; you can redistribute it and/or modify it under the terms of the
// GNU General Public License version 2 as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along with this program in a
// notecard; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.
//
// GPL license version 2 on the web: http://www.gnu.org/copyleft/gpl.html (Note, URL might change
// once v3 is released.)
//
// Dimmer Range Black ---> White
list intensity = [ "0.000", "0.100", "0.200", "0.300", "0.400", "0.500", "0.600", "0.700", "0.800","0.900","1.000" ];
// Length of the Color Range List
integer intensitylistlength;
// Current list position
integer intensitylistposition;
float requestedintensity;
integer x;

default
{
state_entry()
{
intensitylistlength = ( llGetListLength(intensity) - 1 );
intensitylistposition = 0;
//llSetPrimitiveParams([PRIM_POINT_LIGHT, TRUE, <0, 0, 0>, 1.0, 10.0, 0.75]);
llSetPrimitiveParams([PRIM_POINT_LIGHT, FALSE, <1,.7,0>, 0, 0, 0]);
}

link_message(integer sender_num, integer number, string message, key agent) {
// Lite Emitter Toggle
if ( number == 1000 ) { // Dim the Light
if ( (intensitylistposition <= intensitylistlength) && (intensitylistposition != 0 ) ) {
--intensitylistposition; // move down the list by one
requestedintensity = llList2Float(intensity, intensitylistposition);
// Set the intensity
llSetPrimitiveParams([PRIM_POINT_LIGHT, TRUE, <1,.7,0>,requestedintensity, 10.0, 0.75]);
return;
}
}

if ( number == 2000 ) { // Brighten the Light
if ( (intensitylistposition <= intensitylistlength) && (intensitylistposition != intensitylistlength ) ) {
++intensitylistposition; // move down the list by one
requestedintensity = llList2Float(intensity, intensitylistposition);
// Set the intensity
llSetPrimitiveParams([PRIM_POINT_LIGHT, TRUE, <1,.7,0>, requestedintensity, 10.0, 0.75]);
return;
}
}

if (number == 3000 ) {// toggle light on or off
if (x == 0)
{
llSetPrimitiveParams([PRIM_POINT_LIGHT, TRUE, <1,.7,0>, requestedintensity, 10.0, 0.75]);
x = 1;
llSleep(0.5);
return;
}
if (x == 1)
{
llSetPrimitiveParams([PRIM_POINT_LIGHT, FALSE, <1,.7,0>, requestedintensity, 10.0, 0.75]);
x = 0;
llSleep(0.5);
return;
}

}

}
}


The message sent for toggling the light is of course 3000.
In the lamp I've made, the prim that is the light source contain both the "Dimmer code" and the "Toggle code"

Toggle code
CODE
default
{
touch_start(integer total_number)
{
llMessageLinked (LINK_SET, 3000, "", "");
}
}


To resume, using these script, you can build a lamp made of 3 prims; the main prim is the light source and on/off toggle and contains the main script. Then one prim for the "bright up" part and one of the "dim down" part.

Since the "intensity" is controled here, the color can be set to whatever you like and will be preserved. Also. when you toggle the lamp off, the intensity is preserved when you turn it back on.

Thank you! I'll send you the lamp I made for you to try.
_____________________
gone to Openlife Grid and OpenSim standalone, your very own sim on your PC, 45,000 prims, huge prims at will up to 100m, yes, run your own grid on your PC, FOR FREE!
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
05-26-2006 13:35
From: Pratyeka Muromachi
Now that I think of it... It would be nice to turn them all off at the same time, with just one click...
For a big building I'd be surprised if you could link everything at once.

You'll probably have to use llSay() or even llShout().

Have a listener that listens on (say) 33691 and sets the broghtness to whatever you say, and have the master-off switch turn everything off at once by llSay(33691,"0.0";);
Pratyeka Muromachi
Meditating Avatar
Join date: 14 Apr 2005
Posts: 642
05-26-2006 13:43
From: Argent Stonecutter
For a big building I'd be surprised if you could link everything at once.

You'll probably have to use llSay() or even llShout().

Have a listener that listens on (say) 33691 and sets the broghtness to whatever you say, and have the master-off switch turn everything off at once by llSay(33691,"0.0";);


Good idea, Thanks!
_____________________
gone to Openlife Grid and OpenSim standalone, your very own sim on your PC, 45,000 prims, huge prims at will up to 100m, yes, run your own grid on your PC, FOR FREE!