Monahan McMillan Posted April 10, 2019 Share Posted April 10, 2019 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 rotvector 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 hereif (sittingAvatar == NULL_KEY) return;string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);llPlaySound("UUID", 1.0);}}} Link to comment Share on other sites More sharing options...
Xiija Posted April 10, 2019 Share Posted April 10, 2019 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 More sharing options...
Monahan McMillan Posted April 13, 2019 Author Share Posted April 13, 2019 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 More sharing options...
steph Arnott Posted April 13, 2019 Share Posted April 13, 2019 The script is a mess. This looks wrong 'if (sittingAvatar == NULL_KEY)' should be sufficient to use this if (NULL_KEY) which will evaluate as true. Link to comment Share on other sites More sharing options...
Qie Niangao Posted April 13, 2019 Share Posted April 13, 2019 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 More sharing options...
steph Arnott Posted April 13, 2019 Share Posted April 13, 2019 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 More sharing options...
Rolig Loon Posted April 13, 2019 Share Posted April 13, 2019 (edited) 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 April 13, 2019 by Rolig Loon 1 Link to comment Share on other sites More sharing options...
steph Arnott Posted April 13, 2019 Share Posted April 13, 2019 (edited) 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 April 13, 2019 by steph Arnott I messed up 1 Link to comment Share on other sites More sharing options...
Monahan McMillan Posted April 14, 2019 Author Share Posted April 14, 2019 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. Link to comment Share on other sites More sharing options...
steph Arnott Posted April 14, 2019 Share Posted April 14, 2019 (edited) 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 April 14, 2019 by steph Arnott syntax error 1 Link to comment Share on other sites More sharing options...
Rolig Loon Posted April 14, 2019 Share Posted April 14, 2019 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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