Difference between revisions of "Event Driven Sounds"
Silent Mole (talk | contribs) |
Kireji Haiku (talk | contribs) m (AGENT is deprecated, use AGENT_BY_LEGACY_NAME instead) |
||
Line 9: | Line 9: | ||
default | default | ||
{ | { | ||
touch_start(integer | touch_start(integer num_detected) | ||
{ | { | ||
string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0); | |||
llPlaySound(firstSoundInPrim, 1.0); | |||
} | } | ||
} | } | ||
Line 21: | Line 23: | ||
<lsl> | <lsl> | ||
float | float distanceInMeters = 3.0; | ||
float | float timeBetweenSweeps = 15.0; | ||
default | 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); | |||
} | } | ||
} | } | ||
Line 53: | Line 58: | ||
default | default | ||
{ | { | ||
state_entry() | state_entry() | ||
{ | { | ||
if ( llGetStatus(STATUS_PHANTOM) | if (llGetStatus(STATUS_PHANTOM)) | ||
// { | |||
llVolumeDetect(TRUE); | llVolumeDetect(TRUE); | ||
else llVolumeDetect(FALSE); | // } | ||
else | |||
// { | |||
llVolumeDetect(FALSE); | |||
// } | |||
} | } | ||
collision_start(integer | collision_start(integer num_detected) | ||
{ | { | ||
string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0); | |||
} | |||
llPlaySound(firstSoundInPrim, 1.0); | |||
} | |||
} | } | ||
</lsl> | </lsl> | ||
Line 72: | Line 84: | ||
state_entry() | state_entry() | ||
{ | { | ||
string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0); | |||
llCollisionSound(firstSoundInPrim, 1.0); | |||
} | } | ||
} | } | ||
Line 84: | Line 98: | ||
state_entry() | state_entry() | ||
{ | { | ||
llCollisionSound( "", 0.0); | llCollisionSound("", 0.0); | ||
} | } | ||
} | } | ||
Line 93: | Line 107: | ||
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> | <lsl> | ||
vector sit_position = < | 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 | key sittingAvatar = llAvatarOnSitTarget(); | ||
if ( avatar | |||
// 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); | |||
} | } | ||
} | } | ||
Line 123: | Line 139: | ||
<lsl> | <lsl> | ||
string | string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0); | ||
</lsl> | </lsl> | ||
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. |
Revision as of 09:55, 2 November 2012
Event Driven Sounds
Touched
Would you like your creation to make noise when someone clicks on it?
<lsl> default {
touch_start(integer num_detected) { string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
llPlaySound(firstSoundInPrim, 1.0); }
} </lsl>
Near
If someone is nearby, this script will play a sound.
<lsl> 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); }
} </lsl>
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.
<lsl> 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); }
} </lsl>
<lsl> default {
state_entry() { string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
llCollisionSound(firstSoundInPrim, 1.0); }
} </lsl>
If you are hearing an annoying "thud" sound when things collide, add this to the objects to silence the default collision sound.
<lsl> default {
state_entry() { llCollisionSound("", 0.0); }
} </lsl>
Sat on
Would you like your horse to neigh, or your chair to creak when someone sits on it? <lsl> 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); } }
} </lsl>
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.
<lsl>
string firstSoundInPrim = llGetInventoryName(INVENTORY_SOUND, 0);
</lsl>
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.