Difference between revisions of "Event Driven Sounds"

From Second Life Wiki
Jump to navigation Jump to search
m (language tags to <source>)
 
(3 intermediate revisions by 2 users not shown)
Line 6: Line 6:
Would you like your creation to make noise when someone clicks on it?
Would you like your creation to make noise when someone clicks on it?


<lsl>
<source lang="lsl2">
default
default
{
{
     touch_start(integer total_number)
     touch_start(integer num_detected)
     {
     {
         llPlaySound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0 );
         string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
 
        llPlaySound(firstSoundInPrim, 1.0);
     }
     }
}
}
</lsl>
</source>


=== Near ===
=== Near ===
Line 20: Line 22:
If someone is nearby, this script will play a sound.
If someone is nearby, this script will play a sound.


<lsl>
<source lang="lsl2">
float DISTANCE = 3.0; // in meters.
float distanceInMeters = 3.0;
float SECONDS = 1.0; // how often to check
float timeBetweenSweeps = 15.0;


default
default
{
{
     state_entry()  { llSetTimerEvent( SECONDS ); }
     on_rez(integer start_param)
   
    sensor( integer n )
     {
     {
         llPlaySound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0 );
         llResetScript();
        llSetTimerEvent( SECONDS );
     }
     }
   
 
    no_sensor()    { llSetTimerEvent( SECONDS ); }
     state_entry()
   
     timer()
     {
     {
        llSetTimerEvent( 0 );
    //  AGENT is deprecated, use AGENT_BY_LEGACY_NAME instead
         llSensor( "", NULL_KEY, AGENT, DISTANCE, PI );
 
         llSensorRepeat("", NULL_KEY, AGENT_BY_LEGACY_NAME, distanceInMeters, PI, timeBetweenSweeps);
    }
 
    sensor(integer num_detected)
    {
        string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
 
        llPlaySound(firstSoundInPrim, 1.0);
     }
     }
}
}
</lsl>
</source>


=== Collided ===
=== Collided ===
Line 48: Line 53:
Would you like your bushes to rustle when something walks into them? Or your rats to squeak if someone steps on them?
Would you like your bushes to rustle when something walks into them? Or your rats to squeak if someone steps on them?


<lsl>
Which 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.
 
<source lang="lsl2">
default
default
{
{
     state_entry()  
     state_entry()
     {
     {
         if ( llGetStatus(STATUS_PHANTOM) == TRUE )  
         if (llGetStatus(STATUS_PHANTOM))
//      {
             llVolumeDetect(TRUE);
             llVolumeDetect(TRUE);
         else llVolumeDetect(FALSE);
//      }
         else
//      {
            llVolumeDetect(FALSE);
//      }
    }
 
    collision_start(integer num_detected)
    {
        string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
 
        llPlaySound(firstSoundInPrim, 1.0);
     }
     }
}
</source>


     collision_start(integer total_number)
<source lang="lsl2">
default
{
     state_entry()  
     {  
     {  
         llPlaySound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0);
         string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
 
        llCollisionSound(firstSoundInPrim, 1.0);
     }   
     }   
}
}
</lsl>
</source>


<lsl>
If you are hearing an annoying "thud" sound when things collide, add this to the objects to silence the default collision sound.
 
<source lang="lsl2">
default
default
{
{
     state_entry()  
     state_entry()  
     {  
     {  
         llCollisionSound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0);
         llCollisionSound("", 0.0);
     }   
     }   
}
}
</lsl>
</source>
 
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 ===


Would you like your horse to neigh, or your chair to creak when someone sits on it?
Would you like your horse to neigh, or your chair to creak when someone sits on it?
<lsl>
<source lang="lsl2">
vector sit_position = <-0.0,0.0,-0.1>;
vector sit_position = <0.0, 0.0, -0.1>;
vector sit_rotation = <0,0,0>;
//  here an euler rot
vector sit_rotation = <0.0, 0.0, 0.0>;


default
default
{
{
   
     state_entry()
     state_entry()
     {
     {
         llSitTarget(sit_position, llEuler2Rot(sit_rotation * DEG_TO_RAD));
         llSitTarget(sit_position, llEuler2Rot(sit_rotation * DEG_TO_RAD));
     }  
     }
   
 
     changed(integer change)  
     changed(integer change)
     {
     {
         if (change & CHANGED_LINK)  
         if (change & CHANGED_LINK)
         {
         {
             key avatar = llAvatarOnSitTarget();
             key sittingAvatar = llAvatarOnSitTarget();
             if ( avatar != NULL_KEY )
 
             {
        //  when there's no avatar sitting, stop running code here
                llPlaySound( llGetInventoryName( INVENTORY_SOUND, 0 ), 1.0 );
             if (avatar == NULL_KEY) return;
            }
 
             string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
            llPlaySound(firstSoundInPrim, 1.0);
         }         
         }         
     }   
     }   
}
}
</lsl>
</source>


=== Scripting comments ===
=== Scripting comments ===
Line 110: Line 138:
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.
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>
<source lang="lsl2">
string soundname = llGetInventoryName( INVENTORY_SOUND, 0 );
    string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
</lsl>
</source>


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.
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.

Latest revision as of 14:38, 25 January 2015

Event Driven Sounds

Touched

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

default
{
    touch_start(integer num_detected)
    {
        string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);

        llPlaySound(firstSoundInPrim, 1.0);
    }
}

Near

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

float distanceInMeters = 3.0;
float timeBetweenSweeps = 15.0;

default
{
    on_rez(integer start_param)
    {
        llResetScript();
    }

    state_entry()
    {
    //  AGENT is deprecated, use AGENT_BY_LEGACY_NAME instead

        llSensorRepeat("", NULL_KEY, AGENT_BY_LEGACY_NAME, distanceInMeters, PI, timeBetweenSweeps);
    }

    sensor(integer num_detected)
    {
        string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);

        llPlaySound(firstSoundInPrim, 1.0);
    }
}

Collided

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

Which 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.

default
{
    state_entry()
    {
        if (llGetStatus(STATUS_PHANTOM))
//      {
            llVolumeDetect(TRUE);
//      }
        else
//      {
            llVolumeDetect(FALSE);
//      }
    }

    collision_start(integer num_detected)
    {
        string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);

        llPlaySound(firstSoundInPrim, 1.0);
    }
}
default
{
    state_entry() 
    { 
        string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);

        llCollisionSound(firstSoundInPrim, 1.0);
    }  
}

If you are hearing an annoying "thud" sound when things collide, add this to the objects to silence the default collision sound.

default
{
    state_entry() 
    { 
        llCollisionSound("", 0.0);
    }  
}

Sat on

Would you like your horse to neigh, or your chair to creak when someone sits on it?

vector sit_position = <0.0, 0.0, -0.1>;
//  here an euler rot
vector sit_rotation = <0.0, 0.0, 0.0>;

default
{
    state_entry()
    {
        llSitTarget(sit_position, llEuler2Rot(sit_rotation * DEG_TO_RAD));
    }

    changed(integer change)
    {
        if (change & CHANGED_LINK)
        {
            key sittingAvatar = llAvatarOnSitTarget();

        //  when there's no avatar sitting, stop running code here
            if (avatar == NULL_KEY) return;

            string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
            llPlaySound(firstSoundInPrim, 1.0);
        }        
    }   
}

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.

    string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);

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.