Author Topic: play random sounds at certain viewframes using an audiochannel*...?  (Read 305 times)  Share 

hey guys,

another day another question:P

i'm creating audio for a character who's animating in the background.
what i want is to play one of a couple of random sounds at certain frames in the characters view.
this is what i have:

Code: Adventure Game Studio
  1. void PlayMomSounds(){
  3.   int RandomHackSound=Random(2);
  5.   ViewFrame* MomHackFrame1 = Game.GetViewFrame(26, 0, 2);
  6.   ViewFrame* MomHackFrame2 = Game.GetViewFrame(26, 0, 6);
  7.   ViewFrame* MomHackFrame3 = Game.GetViewFrame(26, 0, 10);
  9.   if (RandomHackSound==0){
  10.     MomHackFrame1.LinkedAudio = aKnifeHack1;
  11.     MomHackFrame2.LinkedAudio = aKnifeHack1;
  12.     MomHackFrame3.LinkedAudio = aKnifeHack1;
  13.   }
  14.   else if (RandomHackSound==1){
  15.     MomHackFrame1.LinkedAudio = aKnifeHack2;
  16.     MomHackFrame2.LinkedAudio = aKnifeHack2;
  17.     MomHackFrame3.LinkedAudio = aKnifeHack2;
  18.   }
  19.   else if (RandomHackSound==2){
  20.     MomHackFrame1.LinkedAudio = aKnifeHack3;
  21.     MomHackFrame2.LinkedAudio = aKnifeHack3;
  22.     MomHackFrame3.LinkedAudio = aKnifeHack3;
  23.   }
  24. }

this actually works nicely but:

a) is there a smarter way to assign multiple frames to the same group of random sounds? (i.e. if i have a really long animation with 30 frames that all use the same random sounds i would end up with a huge big block of code and lots of copy/paste...)
b) i can't set the ViewFrame.LinkedAudio to an Audiochannel* can i control panning, volume, position, etc. of the random sounds?



  • been programming computers since 1981
    • I can help with AGS tutoring
    • I can help with proof reading
    • I can help with scripting
    • I can help with story design
Whenever you have a lot of code that you think will be repeated see if you can do it in a function.
With the below you could create a function that you pass in a sound and it gets applied to all 3 frames.

I am not sure of how to correctly define the parameter. I think it is "AudioClip*"
Code: Adventure Game Studio
  1. void AssignSounds(AudioClip* Snd){
  2.   ViewFrame* MomHackFrame1 = Game.GetViewFrame(26, 0, 2);
  3.   ViewFrame* MomHackFrame2 = Game.GetViewFrame(26, 0, 6);
  4.   ViewFrame* MomHackFrame3 = Game.GetViewFrame(26, 0, 10);
  5.   MomHackFrame1.LinkedAudio = Snd;
  6.   MomHackFrame2.LinkedAudio = Snd;
  7.   MomHackFrame3.LinkedAudio = Snd;
  8. }
  10. void PlayMomSounds(){
  11.   int RandomHackSound=Random(2);
  12.   if (RandomHackSound==0){ AssignSounds(aKnifeHack1); }
  13.   if (RandomHackSound==1){ AssignSounds(aKnifeHack2); }
  14.   if (RandomHackSound==2){ AssignSounds(aKnifeHack3); }
  15. }
Pro is the opposite of Con
This fact can clearly be seen,
If progress means to move forward
Then what does congress mean?  --Charles Nipsey Russell

thanks dayowlron,

i'll give this a try later. any ideas on how i could achieve stuff like "Panning", "SetRoomLocation" etc. for the sounds?
the character is all the way to the right on a scrolling background and i don't want the sounds to play when the play walks over to the left (at least not at full volume). also the sounds playing slightly from the right the right would make sense...


  • slasher
    • I can help with AGS tutoring
    • I can help with scripting
    • I can help with story design
  • Slasher worked on a game that was nominated for an AGS Award!
You could use the players x position to determine sound level.

You could use the players x position to determine sound level.

how? i can't find any function to adjust the volume for an audioclip directly...only for audiochannels. ???


  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    • I can help with translating
The AudioChannel/AudioClip model was discussed in another thread not too long ago. The way I like to think of it is that each AudioChannel is an mp3 player with a speaker, and each AudioClip is a track. You have to adjust volume etc. on the player, not the track.

The general pattern (taken directly from the manual entries on AudioChannel.Panning and AudioChannel.Volume) is to store the channel when you play the clip, and immediately adjust the channel properties:

Code: Adventure Game Studio
  1.   AudioChannel *channel = Snd.Play();
  2.   channel.Panning = 20; // Slightly to the right
  3.   channel.Volume = (player.x * 100 / Room.Width); // Goes from 0 at left edge to 100 at right edge of room

Note that this will not automatically update if the character moves while the sounds are playing, so if the sound-effects are long, you may want to update them dynamically (which means storing the audio channel and updating it in one of the RepExec/repeatedly_execute functions; then there's extra work making sure that the audio channel is still playing the sound effect in question).

As for controlling the properties of the clips played by ViewFrame.LinkedAudio, I don't think that's possible. I believe there is a game-wide option somewhere for whether they should be panned according to the player position (they're basically meant to be used for footsteps), but I don't think there's any configurability beyond that. (Edit: It's a character property called Character.ScaleVolume, and apparently if it's turned on the sound will be panned depending on the character x-position and the volume set depending on the character scaling.)

For a solution to your issue, you can adapt the code here:
« Last Edit: 20 May 2017, 17:35 by Snarky »

thanks snarky,

it seems the Character.ScaleVolume property only adjusts the volume but not the panning of the sounds. which is too bad because it would be awesome if it could do that.

i simply worked around the problem by setting the default volume of the clips in the client and using stereo files that are already panned slightly to the right.
i also set the LinkedAudio to "null" if "GetViewport<=100" the sounds only play when the screen is scrolled to the right. not perfect but it's good enough for now.