Jump to content

Loop help


Kokiron
 Share

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

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

Recommended Posts

I've got a script for a dancer, but I'm not sure how to get it to loop back to the beginning once it plays the last sound. Can anyone help?

string ANIMATION = "hold";   // put here name of your animation
float SOUND_LENGTH = 9.12;             // put here length of your sound
float SOUND_VOLUME = 1.0;               // puth here volume (1.0 = max )

integer numNames = 0;
integer playing = FALSE;
integer actNum = 0;

playSound( integer stat )
{
    if ( stat == TRUE ){
        actNum = 0;
        llPreloadSound( llGetInventoryName( INVENTORY_SOUND, actNum ) );
        playing = TRUE;
        llSetTimerEvent( 1.0 );
        if ( ANIMATION != "" ) {
            llSleep( 1.0 );
            llStartAnimation( ANIMATION );
        }
    }
    else {
        playing = FALSE;
        llStopSound();
        llSetTimerEvent( 0.0 );
        if ( ANIMATION != "" ) {
            llStopAnimation( ANIMATION );
        }

    }
  
}   // End playSound( integer stat )


// ------------------------------------------------------------
// ------------------------------------------------------------

default
{
    on_rez( integer num )
    {
        llResetScript();
    }
    state_entry()
    {
        llRequestPermissions( llGetOwner(), PERMISSION_TRIGGER_ANIMATION );
    }   // End state_entry()


    run_time_permissions(integer perm)
    {
        if ( perm & PERMISSION_TRIGGER_ANIMATION ) {
            numNames = llGetInventoryNumber( INVENTORY_SOUND );
            if ( numNames > 0 ){
                llOwnerSay(" Commands 'smangon' or 'smangoff'");
                llListen( PUBLIC_CHANNEL, "", "", "" );
            }
            else {
                llOwnerSay( "Any sound in the inventory!" );                
            }
        }
        else {
            llRequestPermissions( llGetOwner(), PERMISSION_TRIGGER_ANIMATION );
        }
    }   // End run_time_permissions(integer perm)


    attach(key attached)
    {
        if ( attached != NULL_KEY ){
            llResetScript();
        }
    }   // End attach(key attached)


    listen(integer channel, string name, key id, string message)
    {
        message = llToLower( message );
        if( ( message == "smangon" ) && ( playing == FALSE ) ) {
            playSound( TRUE );
        }
        else if( ( message == "smangoff" ) && ( playing == TRUE ) ) {
            playSound( FALSE );
        }
    }   // End listen(integer channel, string name, key id, string message)


    timer()
    {
        if ( playing == TRUE ) {
            llSetTimerEvent( SOUND_LENGTH );
            if ( actNum == numNames ) {
                actNum = 0;
                llSetTimerEvent( 0.2 );
            }
            else {
                if ( actNum>0) {
                    llStopSound();
                }
                llPlaySound( llGetInventoryName( INVENTORY_SOUND, actNum ), SOUND_VOLUME );
                ++actNum;
                if ( actNum < (numNames - 1))
                {
                    llPreloadSound( llGetInventoryName( INVENTORY_SOUND, actNum ) );
                }
                 if ( actNum == numNames)
                {
                    playing = FALSE;
                    llSleep( 1.5 );
                    llStopAnimation( ANIMATION );
                }
                
            }
           
        }
        else 
        {
            actNum = 0;
            llStopSound();
          if ( ANIMATION != "" ) 
          {
             llStopAnimation( ANIMATION );
            }
            llSetTimerEvent( 0.0 );
        }
        
        
    }   // End timer()
    
}   // End state default

// ------------------------------------------------------------
// ------------------------------------------------------------
Link to comment
Share on other sites

As I suggested in a recent thread,, the modulus operator, %, comes in very useful here.

It returns the remainder after you've divided one integer by another,  which makes it very easy to loop through a list, starting at the beginning again once you reach the end.

integer iMax;integer iCounter;list lTest = ["A","B","C","D"];default {	state_entry() {		iMax = llGetListLength(lTest);//make a note of the number of items in the list	}	touch_start(integer num_detected) {		integer n = iCounter % iMax; //n is the remainder after you divide the counter by iMax		llOwnerSay("n is "+(string)n);		llOwnerSay(llList2String(lTest, n)); //say the value of the nth item on the list		++ iCounter; // increase the value of iCounter by one	}}

So in this example, you would need, in state_entry, to build a list of the sounds in your inventory, something like this fragment (which obviously won't work on its own):

integer iMax;integer iCounter;list lSounds;default {	state_entry() {		iMax = llGetInventoryNumber(INVENTORY_SOUND);//get the number of sounds in my inventory		if(iMax){//if there are any			do{//then add them, one by one, to the list lSounds				lSounds +=[llGetInventoryName(INVENTORY_SOUND, iCounter)];			}			while(++iCounter < iMax);		}		iMax = llGetListLength(lSounds);//now use iMax = to store the length of the sound list		iCounter = 0;	}

//[....] timer() { integer n = iCounter % iMax; //do stuff to play llList2String(lSounds,n); ++iCounter//increase the value of iCounter }}

However, unless all the sound clips are eactly the same length, you'll need to find a way of changing the interval at which the timer fires each time you start a new clip.   There's various ways to do that -- the simplest is probably adding the time to the name of the sound clip, and then reading it and using it to reset the timer each time the script selects a new sound.

Link to comment
Share on other sites

yep, modulous is what you are lookin for :)

so something like...

 

 if ( playing == TRUE )
 {   llSetTimerEvent( SOUND_LENGTH );
     llPlaySound( llGetInventoryName( INVENTORY_SOUND, actNum ), SOUND_VOLUME );
     ++actNum;
     actNum = actNum % numNames;
     llPreloadSound( llGetInventoryName( INVENTORY_SOUND, actNum ) );
 }
 else
 {  actNum = 0;
    llStopSound();
    if ( ANIMATION != "" )
    { llStopAnimation( ANIMATION );
    }
     llSetTimerEvent( 0.0 );
 }

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2658 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...