• Hey! Guest! The 37th GMC Jam will take place between May 28th, 12:00 UTC and June 1st, 12:00 UTC. Why not join in! Click here to find out more!

[SOLVED] Random audio collisions with moving car. If !audio_is_playing...

C

Crispywrists

Guest
Hello!

So I'm trying to make it so when my main character bumps into a moving car it plays one of 3 audio files I have made. I got it so the car stops when colliding with the main character and plays the audio but the audio played multiple times, layering infinitely to become a robotic mess of nothingness.

So I checked the internet and came across:

if !audio_is_playing(snd_1) { audio_play_sound(snd_1,1,0) }

which works nicely, but I want it to play a random choice of audio, so I tried:

if !audio_is_playing(snd_1) {audio_play_sound(choose(snd_outtheway1, snd_outtheway2,snd_outtheway3), 0, false); }

But of course it's only checking for snd_1, and not snd_2, or snd_3 - I was wondering if there was a way to add snd_2 & snd_3 to the list of audio files to check whether they're playing or not....or if there is a much better way of coding this.

Many thanks in advance.
 

Bingdom

Googledom
What you can do is
Code:
sound = audio_play_sound();
if !audio_is_playing(sound) sound = audio_play_sound(choose(snd_outtheway1, snd_outtheway2,snd_outtheway3), 0, false);
Here is a quote from the manual that will explain much better than me. ;)
audio_play_sound
This function will also return a unique index number for the sound being played which can then be stored in a variable so that you can then pause it or stop it with the appropriate functions. This means that if you have multiple instances of the same sound playing at any one time you can target just one instance of that sound to deal with using the audio functions.
 
C

Crispywrists

Guest
What you can do is
Code:
sound = audio_play_sound();
if !audio_is_playing(sound) sound = audio_play_sound(choose(snd_outtheway1, snd_outtheway2,snd_outtheway3), 0, false);
Here is a quote from the manual that will explain much better than me. ;)

Thanks for the response - I've just tried this and when I put in

sound = audio_play_sound();

it says wrong number of arguments to function or script, ie. it doesn't like that I've left it blank - am I missing something / was I supposed to put something in the brackets?
 
C

Crispywrists

Guest
Essentially what I want is:

if !audio_is_playing(snd_outtheway1, snd_outtheway2, snd_outtheway3) { audio_play_sound(choose(snd_outtheway1, snd_outthway2, snd_outtheway3), 0,0);

but it wont let me add multiple sound IDs after audio_is_playing, if that makes sense?
 
C

Crispywrists

Guest
Found my answer! It was a simple one but one I shall never forget - putting the && and asking each question before each check, rather than grouping them all together like above:

if !audio_is_playing(snd_outtheway1) && !audio_is_playing(snd_outtheway2) && !audio_is_playing(snd_outtheway3) { audio_play_sound(choose(snd_outtheway1,snd_outtheway2,snd_outtheway3),1,0) }
 

Bingdom

Googledom
Oh, definitely fill the values for audio_play_sound! I was lazy so I left it blank.
First line should be
Code:
audio_play_sound(choose(snd_outtheway1, snd_outtheway2,snd_outtheway3), 0, false);
 
Top