Difference between revisions of "Script:Random Sounds"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with "<lsl> →‎This script will play the sounds in the object's inventory randomly.: float MinimumSeconds = 10.0; float MaximumSeconds = 30.0; float volume = 0.5; // How loud? Bet…")
 
(Replaced deprecated <lsl> with <syntaxhighlight>; reformatted code to conform to the LSL Wiki standards; added attribution. header and category)
 
Line 1: Line 1:
<lsl>
{{LSL Header}}
<syntaxhighlight lang="lsl2">
/*
/*
   This script will play the sounds in the object's inventory randomly.
   This script will play the sounds in the object's inventory randomly.
Line 15: Line 16:
{
{
     soundnames = [];
     soundnames = [];
     sounds = llGetInventoryNumber( INVENTORY_SOUND );
     sounds = llGetInventoryNumber(INVENTORY_SOUND);
     integer n;
     integer n;
     for ( n=0; n < sounds; ++n )
     for (n = 0; n < sounds; ++n)
     {
     {
         soundnames += llGetInventoryName( INVENTORY_SOUND, n );
         soundnames += llGetInventoryName(INVENTORY_SOUND, n);
     }
     }
}
}
Line 25: Line 26:
default
default
{
{
     state_entry()
     state_entry()
     {
     {
         LoadSounds();
         LoadSounds();
          
          
         if ( sounds > 0 )
         if (sounds > 0)
             llSetTimerEvent( 0.1 );
        {
             llSetTimerEvent(0.1);
        }
         else
         else
        {
             llOwnerSay("No sounds.");
             llOwnerSay("No sounds.");
        }
     }
     }
      
      
     timer()
     timer()
     {
     {
         integer random = (integer) llFrand ( sounds );
         integer random = (integer) llFrand(sounds);
         string sound = llList2String( soundnames, random );
         string sound = llList2String(soundnames, random);
         llTriggerSound( sound, volume );
         llTriggerSound(sound, volume);
          
          
         llSetTimerEvent( MinimumSeconds + (llFrand( MaximumSeconds - MinimumSeconds)) );
         llSetTimerEvent(MinimumSeconds + (llFrand(MaximumSeconds - MinimumSeconds)));
     }
     }
      
      
Line 53: Line 57:
     }
     }
}
}
</lsl>
</syntaxhighlight>
 
 
''Script kindly contributed by {{u|Silent Mole}}.''
[[Category:LSL Examples]]

Latest revision as of 17:17, 12 March 2023

/*
  This script will play the sounds in the object's inventory randomly.
*/

float MinimumSeconds = 10.0;
float MaximumSeconds = 30.0;
float volume = 0.5; // How loud? Between 0 and 1.

///////////////////////////////////////////////////////////////////////
integer sounds = 0; // # sounds in inventory
list soundnames;    // names of sounds in inventory
///////////////////////////////////////////////////////////////////////
LoadSounds()
{
    soundnames = [];
    sounds = llGetInventoryNumber(INVENTORY_SOUND);
    integer n;
    for (n = 0; n < sounds; ++n)
    {
        soundnames += llGetInventoryName(INVENTORY_SOUND, n);
    }
}
///////////////////////////////////////////////////////////////////////
default
{
    state_entry()
    {
        LoadSounds();
        
        if (sounds > 0)
        {
            llSetTimerEvent(0.1);
        }
        else
        {
            llOwnerSay("No sounds.");
        }
    }
    
    timer()
    {
        integer random = (integer) llFrand(sounds);
        string sound = llList2String(soundnames, random);
        llTriggerSound(sound, volume);
        
        llSetTimerEvent(MinimumSeconds + (llFrand(MaximumSeconds - MinimumSeconds)));
    }
    
    changed (integer change)
    { 
        if (change & CHANGED_INVENTORY)
        {
            llResetScript();
        }
    }
}


Script kindly contributed by Silent Mole.