Difference between revisions of "Event Driven Sounds"

From Second Life Wiki
Jump to navigation Jump to search
Line 45: Line 45:


=== Collided ===
=== Collided ===
Would you like your bushes to rustle when something walks into them? Or your rats to squeak if someone steps on them?
<lsl>
default
{
    state_entry()
    {
        if ( llGetStatus(STATUS_PHANTOM) == TRUE )
            llVolumeDetect(TRUE);
        else llVolumeDetect(FALSE);
    }
    collision_start(integer total_number)
    {
        llPlaySound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0);
    } 
}
</lsl>
<lsl>
default
{
    state_entry()
    {
        llCollisionSound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0);
    } 
}
</lsl>
Which sound call is necessary depends on how the object is set. Pathfinding-enabled creatures require the collision event. Obstacles vary depending on how their pathfinding attributes are set.


=== Sat on ===
=== Sat on ===

Revision as of 08:50, 16 July 2012

Event Driven Sounds

Touched

Would you like your creation to make noise when someone clicks on it?

<lsl> default {

   touch_start(integer total_number)
   {
       llPlaySound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0 );
   }

} </lsl>

Near

If someone is nearby, this script will play a sound.

<lsl> float DISTANCE = 3.0; // in meters. float SECONDS = 1.0; // how often to check

default {

   state_entry()   { llSetTimerEvent( SECONDS ); }
   
   sensor( integer n )
   {
       llPlaySound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0 );
       llSetTimerEvent( SECONDS );
   }
   
   no_sensor()     { llSetTimerEvent( SECONDS ); }
   
   timer()
   {
       llSetTimerEvent( 0 );
       llSensor( "", NULL_KEY, AGENT, DISTANCE, PI );
   }

} </lsl>

Collided

Would you like your bushes to rustle when something walks into them? Or your rats to squeak if someone steps on them?

<lsl> default {

   state_entry() 
   {
       if ( llGetStatus(STATUS_PHANTOM) == TRUE ) 
           llVolumeDetect(TRUE);
       else llVolumeDetect(FALSE);
   }
   collision_start(integer total_number)
   { 
       llPlaySound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0);
   }  

} </lsl>

<lsl> default {

   state_entry() 
   { 
       llCollisionSound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0);
   }  

} </lsl>

Which sound call is necessary depends on how the object is set. Pathfinding-enabled creatures require the collision event. Obstacles vary depending on how their pathfinding attributes are set.

Sat on

Would you like your horse to neigh, or your chair to creak when someone sits on it? <lsl> vector sit_position = <-0.0,0.0,-0.1>; vector sit_rotation = <0,0,0>;

default {

   state_entry()
   {
       llSitTarget(sit_position, llEuler2Rot(sit_rotation * DEG_TO_RAD));
   } 
   
   changed(integer change) 
   {
       if (change & CHANGED_LINK) 
       {
           key avatar = llAvatarOnSitTarget();
           if ( avatar != NULL_KEY )  
           {
               llPlaySound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0 );
           }
       }        
   }   

} </lsl>

Scripting comments

This gets the name of the first sound in the object's inventory. You can replace it with the name of a sound or its UUID.

<lsl> string soundname = llGetInventoryName( INVENTORY_SOUND, 0 ); </lsl>

You can replace llPlaySound() with llTriggerSound() in the scripts above. The latter plays sounds unattached, so consider carefully if you're using it on a moving object or with a several seconds long sound.