How to stop the same sound playing multiple times at once


Picture this, you have a collectable item, which spawns in your world, like a coin, and it can spawn in great numbers in a pile, say 30 at once. The player collects them all at once and there is a sound that plays when each one is collected. The problem is those 30 sound files play all at once and it is quite distracting, loud and obnoxious. This kind of situation might happen for any of your sounds and this solution will limit them so they all don’t play at once.


How it works is you need an object that is persistent and always in your game, personally I use a persistent object called o_game. In it’s create event, you create an enum for every sound you want to limit, then you set how many of that sound is allowed at once, and you can also set a time that the sound can play before before defining as finished, as some sounds have a fade out trail, so this caters for that. 

enum sound_limits {

//how many sounds we allow at once
global.sound_limits[sound_limits.COIN] = array_create(3, noone);

//how long (secs) until we can remove this and allow another sound, set to 0 if not needed
global.sound_limits_time[sound_limits.COIN] = 0.2;

Then you use this function to play the sound.  Note that gain and pitch_arr  are optional values. If you are not sure where to create this, just create it in a script called play_sound_limited.

function play_sound_limited(_snd, _priority, _loop, _enum, _gain, _pitch_arr) {
	///@desc		plays the passed sound if there is no sound playing
	///@arg	snd		real	sound file
	///@arg	priority	real	sound priority, 100 is prioritiezed over 1, i.e the higher the number the higher the priority
	///@arg	loop		bool	loop the sound?
	///@arg	enum		real	enum value from sound_limits enum array
	///@arg	[gain]		array	set sounds gain value 0 - 1
	///@arg	[pitch_arr]	array	min, max values [0.8, 1.2]
	var _size = array_length(global.sound_limits[_enum]);
	for (var i = 0; i < _size; ++i) {
		if global.sound_limits[_enum][i] == noone {
			var _id = audio_play_sound(_snd, _priority, _loop, _gain);
			if _id >= 0 {
				if _pitch_arr != undefined {
					audio_sound_pitch(_id, random_range(_pitch_arr[0], _pitch_arr[1]));
				global.sound_limits[_enum][i] = _id;
				return _id;
	return -1;

Then in the step event of o_game we use this to check if there is a sound in the sound_limits array and if it’s finished playing or has past the set time, it’s then removed from the array.

//sound limit checks
for (var j = 0; j < sound_limits.TOTAL; ++j) {
	var _size = array_length(global.sound_limits[j]);
	for (var i = 0; i < _size; ++i) {
		if global.sound_limits[j][i] != noone {
			var _snd = global.sound_limits[j][i];
			var _playing = audio_is_playing(_snd);
			if !_playing or (_playing and global.sound_limits_time[j] > 0 and audio_sound_get_track_position(_snd) > global.sound_limits_time[j]) {
				global.sound_limits[j][i] = noone;

Lastly when you want to play the sound you can call the function with this line :

play_sound_limited(snd_coin_collect, 15, false, sound_limits.COIN, , [0.8, 1.2]);

Hope this helps. Any questions, just let me know.



Official Gamemaker Tutorial

I’ve been working with Opera recently to produce more content for their Youtube channel. The latest video is a dive into designing your AI to make them more challenging to play against. It’s the kind of code that could be added to our Top Down series to make the enemy smarter, or add bot support for the player. Check out the video below.

New Udemy Course Available

G’day Gamers!

Hope you are enjoying your gamemaker journey!

If you still need help, I’ve just released a new course over on Udemy, aimed at beginners learning Gamemaker through Visual. The landing page has more info, but basically it’s 8+ hours of content showing how to make a simple platform game from scratch. Did I mention you can jump up and bonk mystery blocks? Probably shouldn’t, Nintendo might knock on my door.

You can also watch a Youtube trailer here showing what the course offers.

If you’re interested, here’s a coupon offering it for a price that should be lower than Udemy’s already applied discount.



One of the Highest rated Gamemaker 2 Courses on Udemy!

I started out making a course on implementing tiles into a game, hoping to show people how to use the fastest collision system in Gamemaker. I also wanted to show a simple method of both controlling your character, and setting up your animations. I ended up with a complete course that shows you how to build an entire game from scratch. Controllable player, enemies, tiles, sounds, lighting, special effects, title pages, end screen and everything in between.

No animals were harmed during the making of this course

People really enjoyed the course, and I’m absolutely humbled, yet honoured to know many found this a stepping stone to releasing their own games. Gamemaker is always evolving, and I keep the content up to date by editing videos, and adding helpful hints, which cater for these changes. I’m also very active on the Q&A section, answering questions daily, and ensuring no one is stuck within the course.

The content is hosted on Udemy, and if you are unsure if this course is for you, they offer a 30 day money back guarantee, so there is nothing to lose by enrolling and trying it out.

To view the course content, and to get a large discount on the regular price, use either the coupon code or referral below. Udemy prices can fluctuate, so if they are the same price, just use the bottom link.

In life time is short, so the time to start making your dream come true is not tomorrow, it’s today! As a wise man once said …

Hope to see you in the course,