Jump to content
  • 0
MIVIMEX

Script: Switch ON/OFF by the same chat command

Question

Hello! I have such a wonderful script. It is turned on/off by chat command "ON/OFF". But I need the light to turn on and off on the same command, for example light. You say "light" for the first time and the light turns on. The second time and it turns off. Please help!

Thanks for your help in advance!

default
{
state_entry()    
{        
key owner = llGetOwner();         
llListen(0, "", owner, "");    }    
listen(integer channel, string name, key id, string message)  
{        
if (message == "ON")        
   {            
   llSetPrimitiveParams([PRIM_POINT_LIGHT,TRUE,<1.0, 1.0, 1.0>,1.0,15,0.750, 
PRIM_GLOW, ALL_SIDES, 0.4]);        }                
else if (message == "OFF")        {            
 llSetPrimitiveParams([PRIM_POINT_LIGHT,FALSE,<1.0, 1.0, 1.0>,1.0,15,0.750, PRIM_GLOW, ALL_SIDES, 0.0]);        }      }}

 

Edited by MIVIMEX

Share this post


Link to post
Share on other sites

15 answers to this question

Recommended Posts

  • 0

That's a basic toggle switch, so just follow exactly the same logic that's already in the switch. The action of your trigger (the message received in the listen event) reverses a condition (the state of the light) from being TRUE to being FALSE, or vice versa. It doesn't make any difference what the message is, as long as it reverses the condition.  Therefore, forget about whether the message says "ON" or "OFF".  Any message will do, even if it's the same message, because the change you want is caused by receiving a message, not by its content.  Therefore, you can write

listen (integer channel, string name, key id, string message)
{
    iSwitch = !iSwitch;  // That is, whatever the switch was before, make it the opposite when any message is received
    if ( iSwitch == TRUE)
    {
        // Turn the light on
    }
    else if ( iSwitch == FALSE)
    {
        // Turn the light off
    }
}

That's the basic logic of any toggle switch, whether it's a scripted one or a real switch in your living room.  That logic can be buried in other, more complicated business, the way yours is now.  It can also be made much simpler, so that you can express the entire change within a single line of code. The logic remains the same.  The only thing that's important here is that the switching variable itself ( iSwitch) must be a global integer variable, so that the script remembers its current state in between times when you trigger the listen event.

Edited by Rolig Loon
  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0

@Rolig Loon

Hooray! One more script was successfully created thanks to you! Thank you! Here's what I got.

integer iSwitch ;
default
{
state_entry()    
{        
key owner = llGetOwner();         
llListen(0, "", owner, "light");    }  
listen (integer channel, string name, key id, string message)
{
    iSwitch = !iSwitch;  // That is, whatever the switch was before, make it the opposite when any message is received
    if ( iSwitch == TRUE)
    {
        llSetPrimitiveParams([PRIM_POINT_LIGHT,TRUE,<1.0, 1.0, 1.0>,1.0,15,0.750, 
PRIM_GLOW, ALL_SIDES, 0.4]); 
    }
    else if ( iSwitch == FALSE)
    {
        llSetPrimitiveParams([PRIM_POINT_LIGHT,FALSE,<1.0, 1.0, 1.0>,1.0,15,0.750, PRIM_GLOW, ALL_SIDES, 0.0]); 
    }
}}

 

Share this post


Link to post
Share on other sites
  • 0

Lovely!  Now you can give some thought to ways of making it even simpler, in your spare time.  :)  Congratulations.

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
13 minutes ago, Rolig Loon said:

Lovely!  Now you can give some thought to ways of making it even simpler, in your spare time.  :)  Congratulations.

Even simplier? How is it?

Share this post


Link to post
Share on other sites
  • 0

See if you can figure out why this does the same thing ....

listen (integer channel, string name, key id, string message)
{
    iSwitch = !iSwitch; 
    llSetPrimitiveParams([PRIM_POINT_LIGHT,iSwitch,<1.0, 1.0, 1.0>,1.0,15,0.750, 
PRIM_GLOW, ALL_SIDES, 0.4 * iSwitch]); 
}

9_9

 

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
27 minutes ago, Rolig Loon said:

See if you can figure out why this does the same thing ....


listen (integer channel, string name, key id, string message)
{
    iSwitch = !iSwitch; 
    llSetPrimitiveParams([PRIM_POINT_LIGHT,iSwitch,<1.0, 1.0, 1.0>,1.0,15,0.750, 
PRIM_GLOW, ALL_SIDES, 0.4 * iSwitch]); 
}

9_9

 

It works! But I can not see yet what's the matter there ... it seems like it's related to the symbol *

Share this post


Link to post
Share on other sites
  • 0
9 minutes ago, MIVIMEX said:

It works! But I can not see yet what's the matter there ... it seems like it's related to the symbol *

iSwitch is binary information, true or false (wich is an integer to LSL)

light intensity is multiplied by the iSwitch state, which is 1 for TRUE and 0 for FALSE, the * is simple math it ... multiplies numbers =^.^=

 

Edited by Fionalein
  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
5 minutes ago, MIVIMEX said:

it seems like it's related to the symbol *

LSL does not have true Boolean variables. Instead we can use any integer variable and  assign it a value == 0 or != 0. That is, any integer variable can be a simple TRUE/FALSE, or ON/OFF binary switch.  So if iSwitch == 0 ....... ;)

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
4 minutes ago, Rolig Loon said:

LSL does not have true Boolean variables. Instead we can use any integer variable and  assign it a value == 0 or != 0. That is, any integer variable can be a simple TRUE/FALSE, or ON/OFF binary switch.  So if iSwitch == 0 ....... ;)

Silly question -  for science: is 2 TRUE as well? I had some languages that treated "anything but zero" as TRUE.

Edited by Fionalein
  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
4 minutes ago, Fionalein said:

iSwitch is binary information, true or false (wich is an integer to LSL)

light intensity is multiplied by the iSwitch state, which is 1 for TRUE and 0 for FALSE, the * is simple math it ... multiplies numbers =^.^=

 

 

3 minutes ago, Rolig Loon said:

LSL does not have true Boolean variables. Instead we can use any integer variable and  assign it a value == 0 or != 0. That is, any integer variable can be a simple TRUE/FALSE, or ON/OFF binary switch.  So if iSwitch == 0 ....... ;)

I think I understand. If multiplies by one then the light is on, if zero is not.

  • Like 2

Share this post


Link to post
Share on other sites
  • 0
2 minutes ago, MIVIMEX said:

 

I think I understand. If multiplies by one then the light is on, if zero is not.

the last numer is light intsensity:

llSetPrimitiveParams([PRIM_POINT_LIGHT,iSwitch,<1.0, 1.0, 1.0>,1.0,15,0.750, 
PRIM_GLOW, ALL_SIDES, 0.4 * iSwitch]);

now if you want some scripting exercise you could replace it as this and write a more elaborated code to actually dim the light by listening to light+ and light- commands or similar to affect your dimmer float variable:D

llSetPrimitiveParams([PRIM_POINT_LIGHT,iSwitch,<1.0, 1.0, 1.0>,1.0,15,0.750, 
PRIM_GLOW, ALL_SIDES, dimmer * iSwitch]);
  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
29 minutes ago, Fionalein said:

Silly question -  for science: is 2 TRUE as well? I had some languages that treated "anything but zero" as TRUE.

Hehe.. I'm beginning to wish that I had asked the moderators to move this thread to the LSL Scripting forum, where it really belongs.  :)

The answer is a bit tricky.  Try writing a script with this listen event in it...

listen (integer channel,  string name, key id, string message)
{
    iSwitch = (integer)message;
    llSetPrimitiveParams( [PRIM_GLOW, ALL_SIDES, 0.4 * iSwitch ]);
}

and send it the message "2".  You should get a glow intensity of 0.8.  But if you wrote

listen (integer channel, string name, key id, string message)
{
    if ( (integer)message == TRUE)
    {
        llSetPrimitiveParams( [PRIM_GLOW, ALL_SIDES, 0.4]);
    }
}

then sending it the message "2" ( or anything else that would be interpreted as non-zero) would make glow = 0.4.   So, if you test an integer variable and find that its value is NOT zero, then it is TRUE.  If you ask what its numerical value is, that's a different question.  The trick is in what you are asking when you test it.  By analogy, look at a red dress and think of the difference between asking " Is it RED?" and "Is it COLORED?".

 

Edited by Rolig Loon
  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0

@Rolig Loon @Fionalein

Here's what I got. Now I can control the brightness of the glow by entering values from 0 to 10 in the chat . But if you enter a value more then an error occurs. Which is understandable, it is not possible to glow brighter.

Is it possible to combine these two properties with a "light" toggle command and brightness 1-10 control?

integer iSwitch ;
default
{
state_entry()    
{        
key owner = llGetOwner();         
llListen(0, "", owner, "");    }  
listen (integer channel, string name, key id, string message)
{
    iSwitch = (integer)message;
    llSetPrimitiveParams( [PRIM_GLOW, ALL_SIDES, 0.1 * iSwitch ]);
}}

 

 

Edited by MIVIMEX

Share this post


Link to post
Share on other sites
  • 0

Play with this ...

    if ( (string) ( (integer) data) == data)
        llOwnerSay("'" + data + "' contains a valid integer");

You're on your own now. :ph34r:

  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×