• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Limiting the repetition of a sound

G

Gabriel

Guest
I have an object in my game which is a box that when touched, it activates a countdown and then explodes (Crash Bandicoot style). If there are other boxes close, they will explode immediately too.

Now, whenever a box explodes, an explosion sound is played. But what if, let's say, eight boxes explode at the same time? The sound will play eight times, making it extremely loud!

Since I do want the sound of multiple explosions to be slightly louder than the one from a single explosion, how can I limit it, so the other boxes won't play the sound if it is already playing 4 times (I want 4 to be the maximum amount, despite the number of multiple explosions).
 
You can check to see if a specific sound is already playing and then decide what to do. Check the documentation on audio to find out how.

1. You can either stop the current sound and start the new sound
2. Not play the next sound.

Most prefer number 1 over number 2, but give each a try and make your own decision.
 

NightFrost

Member
Stopping and restarting the sound would be the way to go. It gives you a nice B-B-B-BOOM when multiple targets go off near instantaneously, instead of a BOOM [meanwhile, more stuff blows up silently].
 

Murzy

Member
The above posts are sound advice :)

The first step of doing some kind of modification / limiting on the sound effects being played is to create your own script to play the sounds. This also allows you to play positional sounds with a simple shorthand. The footprint of the scripts for playing sounds could be something like this:

Code:
///sound_effect_play(index, loop, pitch)
// OR
///sound_effect_play_positional(snd_index, xx, yy, pitch);
Then you can, for example, stop the existing sound if it already being played, by doing a check based on the sound index inside the script.

If you want more detailed control over how many instances of the sound is being played, I'm afraid that you will need a more sophisticated sound controller of some sort. There you could have an array, or some other sort of data structure that keeps count of the number of instances of a certain sound that are being played at the current time. Something like this gets tricky real fast though, because when you increment the count, you will need some sort of independent timer that will decrement the counter after each of the sounds has been finished.
 
G

Gabriel

Guest
You can check to see if a specific sound is already playing and then decide what to do. Check the documentation on audio to find out how.

1. You can either stop the current sound and start the new sound
2. Not play the next sound.

Most prefer number 1 over number 2, but give each a try and make your own decision.
Stopping and restarting the sound would be the way to go. It gives you a nice B-B-B-BOOM when multiple targets go off near instantaneously, instead of a BOOM [meanwhile, more stuff blows up silently].
I like the idea of stopping the current sound and starting a new one (creating the B-B-B-BOOM effect), but as all explosion sounds play with a difference of mere milliseconds, no sound plays instead.

I've tried this, but no sound is heard:
Code:
if instance_exists(tutorial_interface)
    {
        if audio_is_playing(snd_box_explode) audio_stop_sound(snd_box_explode);
        audio_play_sound(snd_box_explode, 2, false);
    }
[EDIT] Ok, forget it... I missed the "not" in the beginning of the code: if !instance_exists(tutorial_interface). Now it's sounding awesomely to me!
 
Last edited by a moderator:
G

Gabriel

Guest
The above posts are sound advice :)

The first step of doing some kind of modification / limiting on the sound effects being played is to create your own script to play the sounds. This also allows you to play positional sounds with a simple shorthand. The footprint of the scripts for playing sounds could be something like this:

Code:
///sound_effect_play(index, loop, pitch)
// OR
///sound_effect_play_positional(snd_index, xx, yy, pitch);
Then you can, for example, stop the existing sound if it already being played, by doing a check based on the sound index inside the script.

If you want more detailed control over how many instances of the sound is being played, I'm afraid that you will need a more sophisticated sound controller of some sort. There you could have an array, or some other sort of data structure that keeps count of the number of instances of a certain sound that are being played at the current time. Something like this gets tricky real fast though, because when you increment the count, you will need some sort of independent timer that will decrement the counter after each of the sounds has been finished.
That sounds a bit too complicated, specially because this new update to my game is already finished and I'm only tweaking up some details.
Guess I'll just be lazy and leave it the way it is, haha.
 
Top