Jump to content

modify script with on/off toggle with touch


poetsmoon
 Share

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

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

Recommended Posts

I have a script that will make a set of wings appear to flap by texture moving from one prim to the next and back. I am allowed to modify the script but my scripting knowledge is limited. I would like to modify the script so anyone owning the wing can toggle the running of the script on and off by touching the wing. Is this possible? If so can anyone help me with this? Thank you.

  • Like 1
Link to comment
Share on other sites

You use a touch event to detect when someone touches the wings, the llDetectedKey() function to know who touched them, and finally the llGetOwner()  function to see if that person is the wing’s owner. Like this:

 

touch_start(integer total_number)
     {
          if (llGetOwner() == llDetectedKey(0))
          {
               // Here the actual code that initiates or stops wings flap
          }
}

  • Like 1
Link to comment
Share on other sites

A toggle simply switches a global integer from ON to OFF (TRUE to FALSE, 1 to 0, .... ).

 

integer gON;default{    touch_start(integer num)    {        gON = !gON;  //  Here's the switch ...        if (gON)   //  So, let's test it ....        {            //  It's on.        }        else        {            //  It's not.        }    }}

 

  • Thanks 2
Link to comment
Share on other sites

The basic mechanism for an on/off script is:

integer toggle; // by declaring it here, you make this a global variable, which means you can access it anywhere in the script.   An integer's default value is 0, or OFFdefault{	state_entry()	{		//llSay(0, "Hello, Avatar!");	}	touch_start(integer total_number)	{		if(llDetectedKey(0)==llGetOwner()){//if the toucher is the owner			toggle = !toggle; // switch the value of toggle to its opposite.  If 0, or FALSE, switch to 1, or TRUE (or vice versa).			if(toggle){//shorthand for "if the value of toggle is equal to TRUE"				//code to turn things on			}			else { //if it's not TRUE, it must be FALSE				// code to turn things off			}		}	}}

You can simplify the code in the touch_start event, thus

integer toggle; // by declaring it here, you make this a global variable, which means you can access it anywhere in the script.   An integer's default value is 0, or OFFdefault{	state_entry()	{		//llSay(0, "Hello, Avatar!");	}	touch_start(integer total_number)	{		if(llDetectedKey(0)==llGetOwner()){//if the toucher is the owner			if(toggle=!toggle){//shorthand for "first switch the value of toggle to its opposite and then, if the new value is TRUE"				//turn things on			}			else{//the new value must be FALSE								//turn things off			}		}	}}

You say you are animating a texture.   This presumably means you are using the function llSetTextureAnim.   The Wiki article  has an example of how to start an animation,and then stop it at a specific frame, which you might find useful (it's the final item in the examples section).

 

ETA: Rolig beat me to it, while I was typing.

Link to comment
Share on other sites

  • 2 years later...
On 10.04.2015 at 5:53 PM, Rolig Loon said:

A toggle simply switches a global integer from ON to OFF (TRUE to FALSE, 1 to 0, .... ).

 


integer gON;default{    touch_start(integer num)    {        gON = !gON;  //  Here's the switch ...        if (gON)   //  So, let's test it ....        {            //  It's on.        }        else        {            //  It's not.        }    }}

 

Hello! Please help!  I used your tip and it works perfectly for my loudspeakers :

https://community.secondlife.com/forums/topic/412976-script-for-loudspeaker/?tab=comments#comment-1677888

but I dont really like how my OFF line looks like. Is there any way to simplify it?

 

integer gON;

default

{   

 touch_start(integer num)    
 
 {        gON = !gON;  
 
 //  Here's the switch ...       
  if (gON)   
 //  So, let's test it ....        
 {    llSetTextureAnim(ANIM_ON | SMOOTH | SCALE | PING_PONG | LOOP, ALL_SIDES, 1, 1, 1.0, 3.0, 2.0);        
 //  It's on.        
 }        
 
 else        {     llSetTextureAnim(ANIM_ON | SMOOTH | SCALE | PING_PONG | LOOP, ALL_SIDES, 0, 0, 0.0, 0.0, 0.0);       
 //  It's not.        
 }   
  }
  }

Got it already! I found this line:

llSetTextureAnim(FALSE, ALL_SIDES, 0, 0, 0.0, 0.0, 1.0);

So script looks this way now:

integer gON;

default

{   

 touch_start(integer num)    
 
 {        gON = !gON;  
 
 //  Here's the switch ...       
  if (gON)   
 //  So, let's test it ....        
 {    llSetTextureAnim(ANIM_ON | SMOOTH | SCALE | PING_PONG | LOOP, ALL_SIDES, 1, 1, 1.0, 3.0, 2.0);        
 //  It's on.        
 }        
 
 else        {    llSetTextureAnim(FALSE, ALL_SIDES, 0, 0, 0.0, 0.0, 1.0);      
 //  It's not.        
}   
}
}

Thanks! @Rolig Loon

Edited by Sunbleached
  • Thanks 1
Link to comment
Share on other sites

  • 2 years later...
You are about to reply to a thread that has been inactive for 1434 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...