Jump to content

Toggling On/Off a function via a channel


Miguelito Shilova
 Share

You are about to reply to a thread that has been inactive for 3505 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I have a linkset where there is a prim containing a simple menu (llDialog)

The menu has a button labeled 'Jets On/Off' that when pressed sends the message 'JETS' out on a defined integer channel.

I intend to have another prim in the linkset listen for that message and toggle on / off a particle effect whenever it hears 'JETS', so the particle effect prim needs to keep track of the current on / off state of the particle effect script.

That script contains two functions: Bubbles_On() and Bubbles_Off() - the latter simply containing a blank particle effect function (e.g., llParticleSystem([]);) to replace the particle effect with no effect. There's also a sound 'water' that is also to accompany the particle effect.

I'm trying to use a default state and an on state to do the toggle. Here's the script (I've removed the particulars of the particle effect since they're not the issue) ...

// Particle Effect Declarations

// …

integer CHANNEL  = -321;

 

Bubbles_On()

{

// llParticleSystem([ … settings … ]);

   llLoopSound("water", 1.0);

}

 

Bubbles_Off()

{

    llParticleSystem([]);       // A blank particle system for turning off bubbles

    llStopSound();

       

}

 

default {

    state_entry()

    {

        Bubbles_Off();    

        llPreloadSound("water");

        llListen(CHANNEL, "", "", "");

    }

    listen(integer channel, string name, key id, string msg)

    {

       if (msg == "JETS")

       {

            Bubbles_On();

            state default;

       }

       else

       state on;

    } 

}

 state on {

     listen(integer channel, string name, key id, string msg)

     {

       if (msg == "JETS")

       {

            Bubbles_Off();

            state on;

       }

       else

       state default;

        

    }

}

 

The script above compiles successfully, and on rez the particle efffect is off in its prim

When the button in the menu prim is pressed, the particle effect starts, however when the button is pressed again the particle effect does not stop. Can someone provide a little guidance on how to set up the toggle?

Thanks so much!

--Mig

 

 

 

Link to comment
Share on other sites

If this were my script, I think I would just write

integer CHANNEL  = -321;integer gBubbles;default{	state_entry()	{		llPreloadSound("water");		llListen(CHANNEL, "", "", "");	}	listen(integer channel, string name, key id, string msg)	{		if (msg == "JETS")		{			gBubbles = !gBubbles;			if (gBubbles)			{				// llParticleSystem([ … settings … ]);				llLoopSound("water", 1.0);			}			else			{				llParticleSystem([]);				llStopSound();			}		}	}}

 The most common problem when I write scripts with more than one state is that I forget to open a listener in one of them, so its listen event is deaf.  :smileywink:

Link to comment
Share on other sites

The listener will not survive a state change. So in your second state you would have to set up the llListen() again.

If it's in the same linkset, you should look into http://wiki.secondlife.com/wiki/LlMessageLinked to communicate with scripts in a linkset.

If it would be the particles only, without the sound, I would say call the particle functions from within the main script with llLinkParticleSystem(integer link, list rules). Depending on the object, the sound may work from within the prim where the main script is as well.

However, to toggle your command within a single state, define a global variable.

integer giToggle;and when receiving your "JETS" command:if (msg == "JETS"){    giToggle = !giToggle; // Alternating between 0 and 1.    if (giToggle) // if giToggle is 1    {        Bubbles_On()     }    else     {        Bubbles_Off()     }}

 

 

 

 

Link to comment
Share on other sites

is a number of things to consider when using states

the main one is to follow the flow of what happens when a state is entered and exited. Codes in the state_entry will always execute when the state is invoked. Before the new state is invoked then any code in state_exit of the current state will execute

a example using 3 states. Where is a default state (the control state) that listens and a On State and a Off State

if coming from a procedural programming background then this example shows that states can also be thought of as subroutines

 

integer listener;default{   state_entry()   {      listener = llListen(...);   }      listen (...)   {     if (msg == "JETS")        state On     else        state Off;   }   state_exit()   {      llListenRemove(listener);   }}state On{   state_entry()   {     llSay("state: on");     ... bubbles on     state default;     }   state_exit()   {     llSay("next state will be default");     }}state Off{   state_entry()   {     llSay("state: off");     ... bubbles off     state default;   }   state_exit()   {     llSay("next state will be default");     }}

 

Link to comment
Share on other sites

Since it is a linkset, why not just use message_linked with a specific integer to indicate it is a jets message, and then there's no need to use listeners?

 

The event for link messages will supply the prim number sending the message, plus an integer, a string, and a key, which would allow you to pass in a key for the particles target as well, should that be advantageous.

 

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3505 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...