Jump to content

Ramdom sounds on sit


Monahan McMillan
 Share

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

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

Recommended Posts

Greetings all,

I'm trying to "mix" two scripts to get random sounds to play whenever someone sits on an object. Still stuck somewhere in the compilation. Could anyone help please ?

Thanks.

Both scripts were found around here, the random sounds :

 

float MinimumSeconds = 10.0; // Minimum time between successive sounds (seconds)
float MaximumSeconds = 30.0; // Maximum time between successive sounds (seconds)
float volume = 0.5; // How loud? Between 0 and 1.
integer sounds = 0; // # sounds in inventory
list soundnames;
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();       }   }}

 

and the sound on sit

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 (sittingAvatar == NULL_KEY) return;

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

Link to comment
Share on other sites

something like this in your changed event?... (untested)

if (sittingAvatar == NULL_KEY)
{ llStopSound();
  return;
}

 string sound = llList2String( soundnames, llFloor(llFrand( sounds )) );
 llPlaySound(sound, volume);

Link to comment
Share on other sites

Thanks for the tip. Sorry for the delay.

Did that but doesn't work as expected, sounds are playing even if nobody is sitting...

vector sit_position = <0.0, 0.0, -0.1>; // here an euler rot
vector sit_rotation = <0.0, 0.0, 0.0>;
float MinimumSeconds = 10.0; // Minimum time between successive sounds (seconds)
float MaximumSeconds = 30.0; // Maximum time between successive sounds (seconds)
float volume = 0.5; // How loud? Between 0 and 1.
integer sounds = 0; // # sounds in inventory
list soundnames;
LoadSounds() {   
soundnames = [];   
sounds = llGetInventoryNumber( INVENTORY_SOUND );   
integer n;   for ( n=0; n < sounds; ++n )   {       soundnames += llGetInventoryName( INVENTORY_SOUND, n );   }} 
default {   
state_entry()   {       
llSitTarget(sit_position, llEuler2Rot(sit_rotation * DEG_TO_RAD));
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)   {    key sittingAvatar = llAvatarOnSitTarget();
 if (sittingAvatar == NULL_KEY)
{ llStopSound();
  return;
}
 string sound = llList2String( soundnames, llFloor(llFrand( sounds )) );
 llPlaySound(sound, volume);      }}

Link to comment
Share on other sites

Yeah, this script really needs to be pasted into a code block to preserve formatting. But trying to forge ahead nonetheless: Is there anywhere that the script calls llSetTimerEvent(0.0)? I'm not seeing it, in which case the timer() event will just keep triggering sounds whether anybody is seated or not.

Link to comment
Share on other sites

Because it was doing my head in.

vector sit_position = <0.0, 0.0, -0.1>; // here an euler rot
vector sit_rotation = <0.0, 0.0, 0.0>;
float MinimumSeconds = 10.0; // Minimum time between successive sounds (seconds)
float MaximumSeconds = 30.0; // Maximum time between successive sounds (seconds)
float volume = 0.5; // How loud? Between 0 and 1.
integer sounds = 0; // # sounds in inventory
list soundnames;

LoadSounds()
{
	soundnames = [];
	sounds = llGetInventoryNumber( INVENTORY_SOUND );
	integer n;   for ( n=0; n < sounds; ++n )
	{
		soundnames += llGetInventoryName( INVENTORY_SOUND, n );
	}
}
default
{
	state_entry()
	{
		llSitTarget(sit_position, llEuler2Rot(sit_rotation * DEG_TO_RAD));
		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)
	{
		key sittingAvatar = llAvatarOnSitTarget();
		if (sittingAvatar == NULL_KEY)
		{
			llStopSound();
			return;
		}
		string sound = llList2String( soundnames, llFloor(llFrand( sounds )));
		llPlaySound(sound, volume);
	}
}

 

Link to comment
Share on other sites

So, what's being suggested here is that the changed event should look more like this:

	changed (integer change)
	{
		if ( change & CHANGED_LINK )
		{
			if ( llAvatarOnSitTarget() )
			{
				llSetTimerEvent( MinimumSeconds + (llFrand( MaximumSeconds - MinimumSeconds)) );
			}
			else
			{
				llSetTimerEvent(0.0);
			}
		}
	}

Personally, I would remove the code in the state_entry event that starts the timer at that point.  There's no reason to start it if there's no one seated.  So, remove

		if ( sounds > 0 )
			llSetTimerEvent( 0.1 );
		else
			llOwnerSay("No sounds.");
Edited by Rolig Loon
  • Like 1
Link to comment
Share on other sites

1 hour ago, Rolig Loon said:

 

Basically yes. Trying to read it in the original awfull format hid all the errors. On an addition note at least the OP is actually working at the problem. Your change event block was what i inteded to post. I also added the user created function to it.

if ( change & INVENTORY_SOUND )
	{
		LoadSounds();
	}
vector sit_position = <0.0, 0.0, -0.1>; // here an euler rot
vector sit_rotation = <0.0, 0.0, 0.0>;
float MinimumSeconds = 10.0; // Minimum time between successive sounds (seconds)
float MaximumSeconds = 30.0; // Maximum time between successive sounds (seconds)
float volume = 0.5; // How loud? Between 0 and 1.
integer sounds = 0; // # sounds in inventory
list soundnames;

LoadSounds()
{
	soundnames = [];
	sounds = llGetInventoryNumber( INVENTORY_SOUND );
	integer n;   for ( n=0; n < sounds; ++n )
	{
		soundnames += llGetInventoryName( INVENTORY_SOUND, n );
	}
}
default
{
	state_entry()
	{
		llSitTarget(sit_position, llEuler2Rot(sit_rotation * DEG_TO_RAD));
	}
	on_rez(integer start_param)
	{
		//soundnames = [];
		LoadSounds();
	}
	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_LINK )
		{
			if ( llAvatarOnSitTarget() )
			{
				llSetTimerEvent( MinimumSeconds + (llFrand( MaximumSeconds - MinimumSeconds)) );
			}
			else
			{
				llSetTimerEvent(0.0);
			}
		}
		if ( change & CHANGED_INVENTORY )
		{
			//soundnames = [];
			LoadSounds();
		}

	}
}

 

Edited by steph Arnott
I messed up
  • Like 1
Link to comment
Share on other sites

1 hour ago, Monahan McMillan said:

Thanks to you all for your suggestions and corrections. I might indeed aim a bit high for my poor scripting skills, but it is thanks to your help that I can improve them.

I am still looking to make it work with a do... while loop, but still can't make it work yet.

You could simply use a strided list and use an randomized integer then call that from the list. say, [ 1, "this sound",  2, "that song" .......]. Assuming that is what you was refering to.

Edited by steph Arnott
syntax error
  • Like 1
Link to comment
Share on other sites

54 minutes ago, Monahan McMillan said:

I am still looking to make it work with a do... while loop, but still can't make it work yet.

I hesitate to use the phrase "doomed to fail," but a do ... while loop is not likely to be as satisfying as you might imagine.  The problem is that a loop like that executes in milliseconds.  You want a process that changes on a much longer time scale.  You want to change sounds every few seconds, not every few milliseconds. That's what timer events are for. 

Incidentally, you may be tempted to use the llSleep function as a proxy for a timer, but I strongly recommend against it for this sort of application -- not because you can't make it work, but because it's like using a sledgehammer to open your jewelry box. A llSleep stops the entire script for a specified period of time, essentially like cutting the power to an entire house when all you really wanted to do was flip the porch light on and off.

  • Like 1
Link to comment
Share on other sites

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