GM:S 1.4 Limiting the repetition of a sound

Discussion in 'Programming' started by Gabriel, Jun 29, 2017.

  1. Gabriel

    Gabriel Member

    Joined:
    Jun 20, 2016
    Posts:
    141
    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).
     
  2. Pixelated_Pope

    Pixelated_Pope Member

    Joined:
    Jun 20, 2016
    Posts:
    784
    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.
     
  3. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,948
    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].
     
    Pixelated_Pope likes this.
  4. Murzy

    Murzy Member

    Joined:
    Jul 28, 2016
    Posts:
    24
    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.
     
  5. Gabriel

    Gabriel Member

    Joined:
    Jun 20, 2016
    Posts:
    141
    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: Jun 30, 2017
  6. Gabriel

    Gabriel Member

    Joined:
    Jun 20, 2016
    Posts:
    141
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice