Jump to content


Cooldown for playing sounds

  • Please log in to reply
1 reply to this topic

#1 Motorsheep


    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 28 March 2016 - 02:17 PM

Disclaimer: I know how countdowns work, but I am looking for a systematic solution that directly and permanently ties a specific cool-down timer to each sound, instead of setting the timer in every single case where I want the sound to be played.


So I've got a few cases where I don't want certain sounds to play over and over..


E.g. upon spotting the player, my little robot enemies announce their actions based on their current state.

But when I hover at the limit of their sight range, they keep spotting and losing me, and that makes them a little too chatty.


Right now I only check if a sound is already playing before I play it.
My next best idea would be to let a small cooldown timer run every time a sound has been played.


But that would require writing that code everywhere I want the game to play a sound.

So I wonder if there's a smarter way to do it. Maybe I can use a sound object that has an event for each sound with the relevant code in it, so that, from my step events and scripts, I'd only trigger the events?
The problem here is that I've never really used anything except the step and draw events because I write everything else directly in GML. And as far as I understand it, a non-step event for my sound would only run its code a single time when triggered, so a cooldown timer would never actually run out. Now if I set an alarm in this sound event, it would get reset every time the event is triggered, right? 


I can't seem to get my head around this at the moment. Help from anyone with experience would be appreciated :)

Edited by Motorsheep, 28 March 2016 - 02:20 PM.

  • 0

#2 cdrdub8


    GMC Member

  • GMC Member
  • 518 posts
  • Version:GM:Studio

Posted 28 March 2016 - 10:27 PM

You could create a script.

Something like "audio_timer_play_sound" I dunno haha.

And the script could look like this:


/// audio_timer_play_sound( sound, priority, loop );
    In the create event of the object that calls this script, make sure you create the following variables:
    audio_timer_time = 0;
    audio_timer_delay = 500; // The number of milliseconds to wait before playing the sound
// Check if delay has passed
if( current_time - audio_timer_time >= audio_timer_delay ) {   
    // Delay has passed, we play sound and update timer
    audio_play_sound( argument[0], argument[1], argument[2] );
    audio_timer_time += audio_time_delay;


This means that you just need to create 2 variable in the create event of your objects.

You could even put those variables inside a script too "audio_timer_init( delay )" ?


Then, instead of calling "audio_play_sound( sound_id, priority, loop )" you just need to call "audio_timer_play_sound( sound_id, priority, loop )" :D


Hope this helps ~

- Cdrdub8

  • 0

I am building awesomeness, wait for it!