How To Quickly Set Up 3D Sound In GameMaker
November 2, 2013
Recent updates to GameMaker Studio added a new audio system to the engine, one that is more code-orientated than previously. To add special effects or utilize 3D sound, developers now have to add a few lines of code to get things working.
Don’t be put off: setting up 3D sound in GameMaker is actually extremely easy.
If you go searching for help on the GameMaker Community forum, it isn’t very easy to find simply-worded documentation on how to achieve this. Fortunately, all you need to know is here.
First we need to give a pair of ears to our player. This is called the ‘listener’ and it lets the game know where we are in the room, and how far we are from active sounds.
In a platform game, the listener could be the character the player controls. In a space game, it could be the space ship. In a game without a controllable player, we might set the listener to be in the middle of the view or the middle of the room, to create directional sound.
To create a listener, we use a simple piece of code in the step event of the object that we want to give ears to:
For those of you that need reminding, if X is left and right, and Y is up and down, Z is forwards and backwards – the Z axis indicates depth (and therefore is more relevant to 3D or fake 3D games).
In a simple 2D game you probably won’t need Z, so you can set it’s value to 0. Set X and Y to the object’s own position. Here is how the listener code might look at this stage:
We now need to create our first sound. Import a sound into your project by dragging it from the desktop and dropping it onto the GameMaker IDE, or by manually creating a new sound asset and importing the file. Edit the sound’s properties in the GameMaker window, so the box to the left is no longer set to ‘Mono’ but instead is set to ‘3D’. Finally, name your sound.
Find (or create) the object you want to play the sound. Add some basic movement code so that you can manipulate the sound’s origin.
Once your object can move, you’ll need to create an ’emitter’ for the sound. Emitters are used for applying real-time audio effects, and are used to position 3D sounds.
In the create event, create the emitter and assign it to a local variable so you can access it later. In the following example, ’emitter_id’ is the name of the variable but you can use a different name.
emitter_id = audio_emitter_create();
Creating the emitter is just a matter of calling a single function.
Below this, the sound is played using using a function with four arguments. The first argument contains the emitter identifier that was just assigned. The second argument is the name of the sound you want to play. The third argument is whether or not the sound loops and the fourth argument indicates sound priority. Simply set a high priority for vital sound effects, and a low priority for less important ones.
Your sound will now play, however there is more to do. As the sound-playing object can move, you need to update the emitter’s position to match the object’s position. Use the following code in the step event:
The first argument contains the emitter identifier. The next two arguments indicate the position to move the emitter to, which should be set to the location of the object that created the emitter. The final argument denotes the Z value which can be set to 0 by default if you don’t require it.
There is one final piece of the puzzle. The emitter should be destroyed when you no longer need it. You can use the following code to achieve this:
You can add this code in the destroy event if you are deleting an object, or the room end event if you are changing levels. It is important to free your emitter once you no longer need it.
Your game should now play the sound in 3D, appearing distant as the listener object gets further away, and closer as the listener object gets closer. If you find the direction of the sound is not working correctly, use the following line of code in the create event of your listener object.
audio_listener_orientation(lookat_x, lookat_y, lookat_z, up_x, up_y, up_z);
There is no one way to configure this, so refer to the GameMaker help files for further information on how orientation works. For the lazy, you could try something like the following for a quick fix:
As you can see, it is easy to quickly set up 3D sound in GameMaker. Position the listener, create an emitter, play the sound, and be sure to update the emitter’s location and free it when it’s no longer in use.