2012-04-10

Play sounds when walking (updated)

I've just uploaded an update to the simple script I wrote that plays a sound when you walk. This change takes running to be the same as walking. So, no matter if you're running or walking, and no matter how often you transition between the two states, the sound keeps on playing. Only when you're neither running nor walking does the sound stop.

Of course, as before, it's not a very sophisticated approach -- doubtless most people would have a need for different sounds for different movement speeds -- but it works for me as I don't have a visible run. I've got my AO configured so I'm either walking or walking a bit faster.

Here's the updated code:

//////////////////////////////////////////////////////////////////////
// Z&A Simple Walking Sound -- Play a sound when an avatar walks
// By Antony Fairport.
//
// Written as an experiment in reacting to llGetAnimation() return
// values (and to give me a barefoot padding sound when I'm not wearing
// any boots or shoes).
//
// I don't claim this is the best way, most efficient way, or most elegent
// way of doing this. But it's a way of doing it.
//
// Revision history:
// -----------------
//
// 2012-04-10
// Changed the code so that running is considered to be the same as
// walking. Before running was just ignored and no sound was played.
//
// 2012-03-26
// Initial revision.
//////////////////////////////////////////////////////////////////////
// Globals.
key g_kOwner;
string g_sWalking;
string g_sLastAnimation;
//////////////////////////////////////////////////////////////////////
// Default state.
default
{
//////////////////////////////////////////////////////////////////
// State entry.
state_entry()
{
// Grab the owner's key. Might as well hold it in a global
// vs call llGetOwner() evert single timer tick.
g_kOwner = llGetOwner();
// Grab the sound.
g_sWalking = llGetInventoryName( INVENTORY_SOUND, 0 );
// If we've got a sound...
if ( g_sWalking != "" )
{
// ...kick off the timer.
llSetTimerEvent( 0.2 );
}
}
//////////////////////////////////////////////////////////////////
// React to a timer event.
timer()
{
// Work out what we're doing right now.
string sAnimation = llGetAnimation( g_kOwner );
// Walking or running?
if ( ( sAnimation == "Walking" ) || ( sAnimation == "Running" ) )
{
// Wasn't walking or running last we checked?
if ( ( g_sLastAnimation != "Walking" ) && ( g_sLastAnimation != "Running" ) )
{
// Start the sound.
llLoopSound( g_sWalking, 1.0 );
}
}
// Not walking or running now but was a moment ago?
else if ( ( g_sLastAnimation == "Walking" ) || ( g_sLastAnimation == "Running" ) )
{
// Stop the sound.
llStopSound();
}
// Remember what we were doing.
g_sLastAnimation = sAnimation;
}
//////////////////////////////////////////////////////////////////
// React to changes.
changed( integer change )
{
// Has the owner, or inventory, changed?
if ( ( change & CHANGED_OWNER ) || ( change & CHANGED_INVENTORY ) )
{
// Yes. Reset the script.
llResetScript();
}
}
}

No comments:

Post a Comment