Sounds are easiest to explain by going through a simple example. In this example, we will create a button that the user can click on. When the button is pressed, it will play a sound. This example will also be divided up into two parts. In the first part of the
example, the sound won’t have any effects; however, in the second part, we will use a sound emitter to alter the pitch (how high or low the sound is on the musical scale) and the gain (the loudness of the sound).
The first thing that we need to do for this example is to add a new sprite. I’ve named my sprite spr_button. It is 32 x 32 pixels in size, its origin is centered, and it is a simple black box.
● spr_button
Here is an image of my sprite:
Once the sprite has been created, we need to create its accompanying object. I’ve named my object obj_button. For now, we won’t add any events to this object. Let’s first create the sound we will be using.
● obj_button
Add a new sound to the game. If you like, you can use bfxr.net to create a sound. I’ve named my sound snd_click.
● snd_click
I also made the sound a “Stereo” sound using the dropdown option in the “Target Options” section of the sound properties. This gives the sound two audio channels. You could use this to play a sound only in the right speaker if the sound is coming from the right in the game or to play it only in the left speaker if the sound is coming from the left.
Here is a screenshot of my sound’s properties:
The sound is added and ready to go! Open up the button object and add a new Mouse
> Left Pressed Event to it. Inside this event, we will be executing the following code.
obj_button: Left Pressed Event /// Play the sound
audio_play_sound(snd_click,10,false);
This short bit of code will use the audio_play_sound function to play our click sound when the user clicks on the button. This function takes three arguments. This first argument is the id of the sound to play. The second argument is the priority of the sound. The priority can be from 0 to 100; it helps GameMaker decided which sounds to play if there are too many playing at the same time. Higher numbers have a higher priority. The final argument tells GameMaker whether the sound should loop or not. We don’t want our sound to loop, so we pass a value of false.
Create a simple room, add the button object to the room, run the game, and test the project. The sound should play whenever the button is clicked.
So far, so good. Now that you know how to create a sound and play it when an event occurs, we have a problem: If you press the button multiple times in a row, the sound can get annoying. This is normal. No matter how good your sound is, if you hear it too many times in a row, it will become annoying. I’m going to teach you a trick that you can use to help mitigate this problem.
Open up the button object again, and this time, add a new Create Event to it.
obj_button: Create Event /// Create a sound emitter
audio_em=audio_emitter_create();
In the code above, we are creating a new audio emitter and assigning its id to the audio_em variable.
You probably remember from the introduction to this chapter that audio emitters can be used to alter the properties of a sound that is being played. The two most common properties that are altered are the pitch and the gain. We will be altering both of these properties of our sound; this will create a nice variance and prevent the sound from becoming annoying. First though, we need to make sure to destroy the emitter when the game ends. Add a new Game End Event to the button object.
obj_button: Game End Event
/// Destroy the sound emitter audio_emitter_free(audio_em);
In this code, we pass the audio_em variable into the audio_emitter_free function in order to destroy our audio emitter.
Now, we need to jump back to our Mouse > Left Pressed Event and change the way that we play our sound. Make sure that you completely remove the code that was
previously in the event before adding this new code.
obj_button: Left Pressed Event /// Play the sound
audio_emitter_pitch(audio_em,random_range(.5,1.5));
audio_emitter_gain(audio_em,random_range(.1,1));
audio_play_sound_on(audio_em,snd_click,false,10);
We start by calling the audio_emitter_pitch function. We tell it what emitter we want to use and the pitch that we want our emitter to be set to. We pass in a random range from .5 to 1.5. A normal pitch is set at 1. Next, we use the audio_emitter_gain function. We also pass in the emitter and use a random range of .1 to 1. Again, the normal gain for a sound is 1. Finally, we play the sound using the
audio_play_sound_on function. The first argument for this function is the emitter to be used, the second is the id of the sound, the third is whether or not the sound should loop, and the fourth is the priority of the sound. It is unfortunate that the last two arguments are swapped in this function from how they are ordered in the
audio_play_sound function, but if you know that, it makes this function easier to manage.
Run the game again and listen to the difference. I tried to use fairly drastic numbers in this example so that you will be able to hear the difference. If you still don’t notice a difference, you might try changing the random ranges until you do.
Great job! Now, you know how to use sounds in GameMaker, and you even know how to alter their pitch and gain during the game to create a better audio feedback
experience.