Random Sound...

Author Topic: Random Sound...  (Read 2873 times)

Would would you create a firing sound that picked a random sound out of, 50 options? Thanks.

If you were creating 50 new sounds, I'd say that's a waste of datablocks.

You could hijack the onFire method for the weapon, then loop through the datablock group looking for audio profiles, and then play that sound as it fires.

Or just create a fat array with the sounds you'd like to have it select from, and randomly choose one.

Don't forget you that you do not have to make a datablock for each sound.
« Last Edit: April 25, 2008, 12:19:12 PM by exidyne »

I think that was generally assumed in this thread.


Sorry, misread. I thought you said "Don't forget you have to make a datablock"

And I think you do need a datablock for most sounds... Unless you hax it clientside and alter the path of the audio, but you certainly need a datablock per sound on the server.

If I understand correctly, you could do something like this:
Code: [Select]
MyExistingAudioProfile.filename = SomeFunctionToGetRandomFilePathString();
ServerPlay2D(MyExistingAudioProfile);
« Last Edit: April 25, 2008, 12:17:00 PM by exidyne »

I'm not sure if that change will be networked since dataBlocks are only sent as the client joins. So they'd hear whatever it was set to initially, while the host would hear whatever the dataBlock had been set to.

That did occur to me. You could always send a command to each client to force them to change the filename (this requires client-side code), or you could just make fifty AudioProfile datablocks...
« Last Edit: April 25, 2008, 01:24:12 PM by exidyne »

GameConnection::TransmitDatablocks(%value) (Use as %client.transmitDatablocks(%value))

Resends all (0) or new (1) datablocks to clients. This could cause lag for everyone when sending and I don't know how you'd get a changed filename recognised as 'new' if it isn't automatically...

[How] would you create a firing sound that picked a random sound out of 50 options?
It's looking like your only option is to create an AudioProfile for each of your sounds. Then in your weapon's onFire() method, select one randomly (you could put all of your sounds in a SimGroup to make this easier).


Otherwise, this may work (but it's untested):
Code: [Select]
new AudioDescription(DefaultAudioDesc)
{
volume = ....
isLooping = ....
is3D = ....
type = $DefaultAudioType;
};

new AudioProfile(DefaultAudioProfile)
{
filename = "";
description = "DefaultAudioDesc";
preload = false;
};

function CustomServerPlay2D(%filepath)
{
   DefaultAudioProfile.delete(); // Necessary?
   new AudioProfile(DefaultAudioProfile)
   {
      filename = %filepath;
      description = "DefaultAudioDesc";
      preload = false;
   };

   for(%i = 0; %i < ClientGroup.getCount(); %i++)
   {
      %client = ClientGroup.getObject(%i);
      %client.transmitDataBlocks(true);
      %client.play2D("DefaultAudioProfile");
   }
}

$MyWeaponSoundCount = 5;
$MyWeaponSound[ 1 ] = "Path/To/Music.wav";
$MyWeaponsSound[ 2 ] = "Path/To/OtherMusic.wav";
....
$MyWeaponSound[ 50 ] = "Path/To/OtherMusic.ogg";

function MyWeapon::onFire(%this, ....)
{
   ....
   CustomServerPlay2D( getRandom(1, $MyWeaponSoundCount) );
}
« Last Edit: April 26, 2008, 02:57:44 PM by exidyne »

The Client-side part wouldn't work because object names aren't transmitted server->client so it wouldn't be able to identify the audio profile.


.isDefaultAudioProfile is not a networked object field.  :cookieMonster: